immutable version

This commit is contained in:
goapunk
2018-06-13 12:32:38 +02:00
parent fc99dedb53
commit 9743255ce9

View File

@@ -21,21 +21,32 @@ public class Localizer {
@Nullable @Nullable
private static Localizer INSTANCE; private static Localizer INSTANCE;
private final Locale systemLocale; private final Locale systemLocale;
// Locking: this
@Nullable @Nullable
private Locale locale; private final Locale userLocale;
private Localizer(SharedPreferences sharedPreferences) { private Localizer(SharedPreferences sharedPreferences) {
systemLocale = Locale.getDefault(); systemLocale = Locale.getDefault();
locale = getLocaleFromTag( userLocale = getLocaleFromTag(
sharedPreferences.getString(LANGUAGE, "default")); sharedPreferences.getString(LANGUAGE, "default"));
} }
private Localizer(Locale systemLocale, @Nullable Locale userLocale) {
this.systemLocale = systemLocale;
this.userLocale = userLocale;
}
// Instantiate the Localizer.
public static synchronized void initialize(SharedPreferences prefs) { public static synchronized void initialize(SharedPreferences prefs) {
if (INSTANCE == null) if (INSTANCE == null)
INSTANCE = new Localizer(prefs); INSTANCE = new Localizer(prefs);
} }
// Reinstantiate the Localizer with the system locale
private static synchronized void reinitialize(Locale systemLocale) {
INSTANCE = new Localizer(systemLocale, null);
}
// Get the current instance.
public static synchronized Localizer getInstance() { public static synchronized Localizer getInstance() {
if (INSTANCE == null) if (INSTANCE == null)
throw new IllegalStateException("Localizer not initialized"); throw new IllegalStateException("Localizer not initialized");
@@ -44,7 +55,7 @@ public class Localizer {
// Reset to the system locale // Reset to the system locale
public synchronized void reset() { public synchronized void reset() {
locale = systemLocale; reinitialize(systemLocale);
} }
// Get Locale from BCP-47 tag // Get Locale from BCP-47 tag
@@ -66,19 +77,18 @@ 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 currentLocale; Locale 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;
synchronized (this) { if (userLocale == null) {
if (locale == null) { // Detect if the user changed the system language
// Detect if the user changed the system language if (systemLocale.equals(currentLocale))
if (systemLocale.equals(currentLocale)) return context;
return context; locale = systemLocale;
locale = systemLocale; } else
} locale = userLocale;
}
if (locale.equals(currentLocale)) if (locale.equals(currentLocale))
return context; return context;
Locale.setDefault(locale); Locale.setDefault(locale);