Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Use system language as default content language (#2518)
Browse files Browse the repository at this point in the history
* Use system language as default content language

* Fix checked available language when resetting
  • Loading branch information
keianhzo authored and bluemarvin committed Jan 1, 2020
1 parent e9e6260 commit d0f8d7a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ private void refreshLanguages() {

@Override
protected boolean reset() {
SettingsStore.getInstance(getContext()).setContentLocales(Collections.singletonList(LocaleUtils.getSystemLocale()));
SessionStore.get().setLocales(Collections.singletonList(LocaleUtils.getSystemLocale()));
SettingsStore.getInstance(getContext()).setContentLocales(Collections.singletonList(LocaleUtils.getDeviceLanguage().getId()));
SessionStore.get().setLocales(Collections.singletonList(LocaleUtils.getDeviceLanguage().getId()));
LocaleUtils.resetLanguages();
refreshLanguages();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ private void initialize(Context aContext) {

@Override
protected boolean reset() {
String systemLocale = LocaleUtils.getSystemLocale();
String systemLocale = LocaleUtils.getClosestSupportedLocale(getContext(), LocaleUtils.getDeviceLanguage().getId());
String currentLocale = LocaleUtils.getCurrentLocale();
if (!currentLocale.equalsIgnoreCase(systemLocale)) {
setLanguage(LocaleUtils.getIndexForSupportedLocale(systemLocale), true);
return true;

} else {
if (currentLocale.equalsIgnoreCase(systemLocale)) {
setLanguage(LocaleUtils.getIndexForSupportedLocale(systemLocale), false);
return false;

} else {
setLanguage(LocaleUtils.getIndexForSupportedLocale(systemLocale), true);
return true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ private void initialize(Context aContext) {

@Override
protected boolean reset() {
String systemLocale = LocaleUtils.getClosestSupportedLocale(getContext(), LocaleUtils.getDeviceLanguage().getId());
String value = LocaleUtils.getSupportedLocaleForIndex(mBinding.languageRadio.getCheckedRadioButtonId());
if (!value.equals(LocaleUtils.getSystemLocale())) {
if (!value.equals(systemLocale)) {
setLanguage(LocaleUtils.getIndexForSupportedLocale(LocaleUtils.getSystemLocale()), true);
}

Expand Down
49 changes: 43 additions & 6 deletions app/src/common/shared/org/mozilla/vrbrowser/utils/LocaleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,18 @@ private static HashMap<String, Language> getAllLanguages() {
}

public static void resetLanguages() {
String currentLocale = getCurrentLocale();
mLanguagesCache.values().stream().forEach((language) -> {
language.setPreferred(language.getId().equals(currentLocale));
mLanguagesCache.values().forEach((language) -> {
if (language == getDeviceLanguage()) {
language.setPreferred(true);

} else {
language.setPreferred(false);
}
});
}

public static Language getCurrentLocaleLanguage() {
return mLanguagesCache.get(getCurrentLocale());
public static Language getDeviceLanguage() {
return mLanguagesCache.get(Resources.getSystem().getConfiguration().getLocales().get(0).toLanguageTag());
}

public static List<String> getLocalesFromLanguages(@NonNull final List<Language> languages) {
Expand Down Expand Up @@ -99,7 +103,7 @@ public static List<Language> getPreferredLanguages(@NonNull Context aContext) {
}

} else {
Language currentLanguage = getCurrentLocaleLanguage();
Language currentLanguage = getDeviceLanguage();
currentLanguage.setPreferred(true);
preferredLanguages.add(currentLanguage);
}
Expand Down Expand Up @@ -262,4 +266,37 @@ public static String getDefaultSupportedLocale() {
return locale;
}

public static String getClosestSupportedLocale(@NonNull Context context, @NonNull String languageTag) {
Locale locale = Locale.forLanguageTag(languageTag);
Optional<LocalizedLanguage> language = LocaleUtils.localizedSupportedLanguages.stream().filter(item ->
item.locale.equals(locale)
).findFirst();

if (!language.isPresent()) {
language = LocaleUtils.localizedSupportedLanguages.stream().filter(item ->
item.locale.getLanguage().equals(locale.getLanguage()) &&
item.locale.getScript().equals(locale.getScript()) &&
item.locale.getCountry().equals(locale.getCountry())
).findFirst();
}
if (!language.isPresent()) {
language = LocaleUtils.localizedSupportedLanguages.stream().filter(item ->
item.locale.getLanguage().equals(locale.getLanguage()) &&
item.locale.getCountry().equals(locale.getCountry())
).findFirst();
}
if (!language.isPresent()) {
language = LocaleUtils.localizedSupportedLanguages.stream().filter(item ->
item.locale.getLanguage().equals(locale.getLanguage())
).findFirst();
}

if (language.isPresent()) {
return language.get().locale.toLanguageTag();

} else {
return getDisplayLocale(context);
}
}

}

0 comments on commit d0f8d7a

Please sign in to comment.