Avoid looking up all locales for system language
This commit is contained in:
parent
251bb10965
commit
362ec885fe
@ -21,8 +21,8 @@ public final class Languages implements SharedPreferences.OnSharedPreferenceChan
|
||||
public static final String SYSTEM_LANGUAGE_CODE = "00";
|
||||
@Nonnull
|
||||
public static final Language SYSTEM_LANGUAGE = new Language(SYSTEM_LANGUAGE_CODE, Locale.getDefault());
|
||||
@Nonnull
|
||||
private static final Locale[] locales = Locale.getAvailableLocales();
|
||||
@Nullable
|
||||
private static Locale[] locales;
|
||||
@Nonnull
|
||||
private final List<Language> list = new ArrayList<>();
|
||||
@Nonnull
|
||||
@ -43,7 +43,7 @@ public final class Languages implements SharedPreferences.OnSharedPreferenceChan
|
||||
|
||||
@Nullable
|
||||
private static Locale findLocaleById(@Nonnull String id) {
|
||||
for (Locale locale : locales) {
|
||||
for (Locale locale : getLocales()) {
|
||||
if (TextUtils.equals(locale.toString(), id)) {
|
||||
return locale;
|
||||
}
|
||||
@ -57,7 +57,7 @@ public final class Languages implements SharedPreferences.OnSharedPreferenceChan
|
||||
language = id;
|
||||
}
|
||||
|
||||
for (Locale locale : locales) {
|
||||
for (Locale locale : getLocales()) {
|
||||
if (TextUtils.equals(locale.getLanguage(), language)) {
|
||||
return locale;
|
||||
}
|
||||
@ -67,6 +67,14 @@ public final class Languages implements SharedPreferences.OnSharedPreferenceChan
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static Locale[] getLocales() {
|
||||
if (locales == null) {
|
||||
locales = Locale.getAvailableLocales();
|
||||
}
|
||||
return locales;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method should be called only when default values have been set to application's preferences
|
||||
*/
|
||||
@ -128,6 +136,10 @@ public final class Languages implements SharedPreferences.OnSharedPreferenceChan
|
||||
|
||||
@Nonnull
|
||||
public Language get(@Nonnull String code) {
|
||||
if (TextUtils.equals(SYSTEM_LANGUAGE.code, code)) {
|
||||
// quick check to avoid list loading
|
||||
return SYSTEM_LANGUAGE;
|
||||
}
|
||||
Language language = findLanguageByCode(code);
|
||||
if (language != null) {
|
||||
return language;
|
||||
|
Loading…
Reference in New Issue
Block a user