This commit is contained in:
goapunk
2018-06-13 13:38:15 +02:00
parent 0c65ff4783
commit 7a2df3d6cb
2 changed files with 10 additions and 22 deletions

View File

@@ -21,18 +21,17 @@ public class Localizer {
@Nullable @Nullable
private static Localizer INSTANCE; private static Localizer INSTANCE;
private final Locale systemLocale; private final Locale systemLocale;
@Nullable private final Locale locale;
private final Locale userLocale;
private Localizer(SharedPreferences sharedPreferences) { private Localizer(SharedPreferences sharedPreferences) {
systemLocale = Locale.getDefault(); this(Locale.getDefault(), getLocaleFromTag(
userLocale = getLocaleFromTag( sharedPreferences.getString(LANGUAGE, "default")));
sharedPreferences.getString(LANGUAGE, "default"));
} }
private Localizer(Locale systemLocale, @Nullable Locale userLocale) { private Localizer(Locale systemLocale, @Nullable Locale userLocale) {
this.systemLocale = systemLocale; this.systemLocale = systemLocale;
this.userLocale = userLocale; if (userLocale == null) locale = systemLocale;
else locale = userLocale;
} }
// Instantiate the Localizer. // Instantiate the Localizer.
@@ -42,8 +41,9 @@ public class Localizer {
} }
// Reinstantiate the Localizer with the system locale // Reinstantiate the Localizer with the system locale
private static synchronized void reinitialize(Locale systemLocale) { public static synchronized void reinitialize() {
INSTANCE = new Localizer(systemLocale, null); if (INSTANCE != null)
INSTANCE = new Localizer(INSTANCE.systemLocale, null);
} }
// Get the current instance. // Get the current instance.
@@ -53,11 +53,6 @@ public class Localizer {
return INSTANCE; return INSTANCE;
} }
// Reset to the system locale
public void reset() {
reinitialize(systemLocale);
}
// Get Locale from BCP-47 tag // Get Locale from BCP-47 tag
@Nullable @Nullable
public static Locale getLocaleFromTag(String tag) { public static Locale getLocaleFromTag(String tag) {
@@ -77,18 +72,11 @@ public class Localizer {
public Context setLocale(Context context) { public Context setLocale(Context context) {
Resources res = context.getResources(); Resources res = context.getResources();
Configuration conf = res.getConfiguration(); Configuration conf = res.getConfiguration();
Locale locale, currentLocale; Locale currentLocale;
if (SDK_INT >= 24) { if (SDK_INT >= 24) {
currentLocale = conf.getLocales().get(0); currentLocale = conf.getLocales().get(0);
} else } else
currentLocale = conf.locale; currentLocale = conf.locale;
if (userLocale == null) {
// Detect if the user changed the system language
if (systemLocale.equals(currentLocale))
return context;
locale = systemLocale;
} else
locale = userLocale;
if (locale.equals(currentLocale)) if (locale.equals(currentLocale))
return context; return context;
Locale.setDefault(locale); Locale.setDefault(locale);

View File

@@ -106,7 +106,7 @@ public class PasswordActivity extends BaseActivity {
private void deleteAccount() { private void deleteAccount() {
passwordController.deleteAccount(this); passwordController.deleteAccount(this);
Localizer.getInstance().reset(); Localizer.reinitialize();
setResult(RESULT_CANCELED); setResult(RESULT_CANCELED);
Intent i = new Intent(this, SetupActivity.class); Intent i = new Intent(this, SetupActivity.class);
i.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK); i.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);