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

Use system language as default content language #2518

Merged
merged 2 commits into from
Jan 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}

}