forked from signalapp/Signal-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add in-app language selection support.
- Loading branch information
Showing
7 changed files
with
143 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.thoughtcrime.securesms.util; | ||
|
||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.content.res.Configuration; | ||
import android.os.Build; | ||
import android.preference.PreferenceManager; | ||
import android.util.Log; | ||
|
||
import org.thoughtcrime.securesms.ApplicationPreferencesActivity; | ||
|
||
import java.util.Locale; | ||
|
||
public class DynamicLanguage { | ||
|
||
private static final String DEFAULT = "zz"; | ||
|
||
private Locale currentLocale; | ||
|
||
public void onCreate(Activity activity) { | ||
currentLocale = getSelectedLocale(activity); | ||
setActivityLocale(activity, currentLocale); | ||
} | ||
|
||
public void onResume(Activity activity) { | ||
if (!currentLocale.getLanguage().equalsIgnoreCase(getSelectedLocale(activity).getLanguage())) { | ||
Intent intent = activity.getIntent(); | ||
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); | ||
|
||
activity.startActivity(intent); | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) { | ||
OverridePendingTransition.invoke(activity); | ||
} | ||
|
||
activity.finish(); | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) { | ||
OverridePendingTransition.invoke(activity); | ||
} | ||
} | ||
} | ||
|
||
private static void setActivityLocale(Activity activity, Locale selectedLocale) { | ||
Configuration configuration = activity.getResources().getConfiguration(); | ||
|
||
if (!configuration.locale.getLanguage().equalsIgnoreCase(selectedLocale.getLanguage())) { | ||
configuration.locale = selectedLocale; | ||
activity.getResources().updateConfiguration(configuration, | ||
activity.getResources().getDisplayMetrics()); | ||
} | ||
} | ||
|
||
private static Locale getActivityLocale(Activity activity) { | ||
return activity.getResources().getConfiguration().locale; | ||
} | ||
|
||
private static Locale getSelectedLocale(Activity activity) { | ||
String language = PreferenceManager.getDefaultSharedPreferences(activity) | ||
.getString(ApplicationPreferencesActivity.LANGUAGE_PREF, DEFAULT); | ||
|
||
if (language.equals(DEFAULT)) return Locale.getDefault(); | ||
else return new Locale(language); | ||
} | ||
|
||
private static final class OverridePendingTransition { | ||
static void invoke(Activity activity) { | ||
activity.overridePendingTransition(0, 0); | ||
} | ||
} | ||
|
||
} |