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

Commit 5b6c98e

Browse files
keianhzobluemarvin
authored andcommitted
Fixes #2670 Language fixes (#2672)
* Fix for Pico Taiwanese Chinese locale * Language fixes * Fallback to en-US for Display/Voice when OS language is not supported
1 parent f6e5ffa commit 5b6c98e

File tree

6 files changed

+115
-45
lines changed

6 files changed

+115
-45
lines changed

app/src/common/shared/org/mozilla/vrbrowser/browser/SettingsStore.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.mozilla.vrbrowser.telemetry.GleanMetricsService;
1818
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
1919
import org.mozilla.vrbrowser.utils.DeviceType;
20-
import org.mozilla.vrbrowser.utils.LocaleUtils;
2120
import org.mozilla.vrbrowser.utils.StringUtils;
2221
import org.mozilla.vrbrowser.utils.SystemUtils;
2322

@@ -419,9 +418,6 @@ public void setAudioEnabled(boolean isEnabled) {
419418
public String getVoiceSearchLocale() {
420419
String language = mPrefs.getString(
421420
mContext.getString(R.string.settings_key_voice_search_language), null);
422-
if (language == null) {
423-
return LocaleUtils.getDefaultSupportedLocale();
424-
}
425421
return language;
426422
}
427423

@@ -434,9 +430,6 @@ public void setVoiceSearchLocale(String language) {
434430
public String getDisplayLocale() {
435431
String language = mPrefs.getString(
436432
mContext.getString(R.string.settings_key_display_language), null);
437-
if (language == null) {
438-
return LocaleUtils.getDefaultSupportedLocale();
439-
}
440433
return language;
441434
}
442435

app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/ContentLanguageOptionsView.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.mozilla.vrbrowser.utils.LocaleUtils;
2626

2727
import java.util.Collections;
28+
import java.util.List;
2829

2930
public class ContentLanguageOptionsView extends SettingsView {
3031

@@ -129,10 +130,14 @@ private void refreshLanguages() {
129130

130131
@Override
131132
protected boolean reset() {
132-
SettingsStore.getInstance(getContext()).setContentLocales(Collections.singletonList(LocaleUtils.getDeviceLanguage().getId()));
133-
SessionStore.get().setLocales(Collections.singletonList(LocaleUtils.getDeviceLanguage().getId()));
134-
LocaleUtils.resetLanguages();
135-
refreshLanguages();
133+
String systemLocale = LocaleUtils.getClosestAvailableLocale(LocaleUtils.getDeviceLanguage().getId());
134+
List<Language> preferredLanguages = LocaleUtils.getPreferredLanguages(getContext());
135+
if (preferredLanguages.size() > 1 || !preferredLanguages.get(0).getId().equals(systemLocale)) {
136+
SettingsStore.getInstance(getContext()).setContentLocales(Collections.emptyList());
137+
SessionStore.get().setLocales(Collections.emptyList());
138+
LocaleUtils.resetLanguages();
139+
refreshLanguages();
140+
}
136141

137142
return false;
138143
}

app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/DisplayLanguageOptionsView.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import androidx.databinding.DataBindingUtil;
1313

1414
import org.mozilla.vrbrowser.R;
15+
import org.mozilla.vrbrowser.browser.SettingsStore;
1516
import org.mozilla.vrbrowser.browser.engine.SessionStore;
1617
import org.mozilla.vrbrowser.databinding.OptionsLanguageDisplayBinding;
1718
import org.mozilla.vrbrowser.ui.views.settings.RadioGroupSetting;
@@ -65,6 +66,7 @@ protected boolean reset() {
6566

6667
} else {
6768
setLanguage(LocaleUtils.getIndexForSupportedLocale(systemLocale), true);
69+
SettingsStore.getInstance(getContext()).setDisplayLocale(null);
6870
return true;
6971
}
7072
}

app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/LanguageOptionsView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected void onDismiss() {
9292
};
9393

9494
private void setVoiceLanguage() {
95-
mBinding.voiceSearchLanguageDescription.setText(getSpannedLanguageText(LocaleUtils.getVoiceSearchLanguage(getContext())), TextView.BufferType.SPANNABLE);
95+
mBinding.voiceSearchLanguageDescription.setText(getSpannedLanguageText(LocaleUtils.getVoiceSearchLanguage(getContext()).getName()), TextView.BufferType.SPANNABLE);
9696
}
9797

9898
private void setContentLanguage() {
@@ -105,7 +105,7 @@ private void setContentLanguage() {
105105
}
106106

107107
private void setDisplayLanguage() {
108-
mBinding.displayLanguageDescription.setText(getSpannedLanguageText(LocaleUtils.getDisplayLanguage()));
108+
mBinding.displayLanguageDescription.setText(getSpannedLanguageText(LocaleUtils.getDisplayLanguage(getContext()).getName()));
109109
}
110110

111111
private int getLanguageIndex(@NonNull String text) {

app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/VoiceSearchLanguageOptionsView.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import androidx.databinding.DataBindingUtil;
1313

1414
import org.mozilla.vrbrowser.R;
15+
import org.mozilla.vrbrowser.browser.SettingsStore;
1516
import org.mozilla.vrbrowser.browser.engine.SessionStore;
1617
import org.mozilla.vrbrowser.databinding.OptionsLanguageVoiceBinding;
1718
import org.mozilla.vrbrowser.ui.views.settings.RadioGroupSetting;
@@ -58,16 +59,25 @@ private void initialize(Context aContext) {
5859
@Override
5960
protected boolean reset() {
6061
String systemLocale = LocaleUtils.getClosestSupportedLocale(getContext(), LocaleUtils.getDeviceLanguage().getId());
61-
String value = LocaleUtils.getSupportedLocaleForIndex(mBinding.languageRadio.getCheckedRadioButtonId());
62-
if (!value.equals(systemLocale)) {
63-
setLanguage(LocaleUtils.getIndexForSupportedLocale(LocaleUtils.getSystemLocale()), true);
62+
String currentLocale = LocaleUtils.getCurrentLocale();
63+
if (currentLocale.equalsIgnoreCase(systemLocale)) {
64+
setLanguage(LocaleUtils.getIndexForSupportedLocale(systemLocale), false);
65+
return false;
66+
67+
} else {
68+
setLanguage(LocaleUtils.getIndexForSupportedLocale(systemLocale), true);
69+
SettingsStore.getInstance(getContext()).setVoiceSearchLocale(null);
70+
return true;
6471
}
65-
66-
return false;
6772
}
6873

6974
private RadioGroupSetting.OnCheckedChangeListener mLanguageListener = (radioGroup, checkedId, doApply) -> {
70-
setLanguage(checkedId, true);
75+
String currentLocale = LocaleUtils.getCurrentLocale();
76+
String locale = LocaleUtils.getSupportedLocaleForIndex(mBinding.languageRadio.getCheckedRadioButtonId());
77+
78+
if (!locale.equalsIgnoreCase(currentLocale)) {
79+
setLanguage(checkedId, true);
80+
}
7181
};
7282

7383
private OnClickListener mResetListener = (view) -> {
@@ -79,7 +89,10 @@ private void setLanguage(int checkedId, boolean doApply) {
7989
mBinding.languageRadio.setChecked(checkedId, doApply);
8090
mBinding.languageRadio.setOnCheckedChangeListener(mLanguageListener);
8191

82-
LocaleUtils.setVoiceSearchLocale(getContext(), LocaleUtils.getSupportedLocaleForIndex(checkedId));
92+
if (doApply) {
93+
String locale = LocaleUtils.getSupportedLocaleForIndex(checkedId);
94+
LocaleUtils.setVoiceSearchLocale(getContext(), locale);
95+
}
8396
}
8497

8598
@Override

app/src/common/shared/org/mozilla/vrbrowser/utils/LocaleUtils.java

Lines changed: 82 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public static void saveSystemLocale() {
3737
}
3838

3939
@NonNull
40-
public static String getSystemLocale() {
41-
return mSystemLocale.toLanguageTag();
40+
public static Locale getSystemLocale() {
41+
return mSystemLocale;
4242
}
4343

4444
@NonNull
@@ -60,6 +60,14 @@ private static HashMap<String, Language> getAllLanguages() {
6060
mLanguagesCache.put(languageId, locale);
6161
}
6262

63+
Locale locale = Locale.forLanguageTag(getDeviceLocale().toLanguageTag());
64+
String languageId = locale.toLanguageTag();
65+
if (!mLanguagesCache.containsKey(languageId)) {
66+
String displayName = locale.getDisplayName().substring(0, 1).toUpperCase() + locale.getDisplayName().substring(1);
67+
Language language = new Language(languageId, displayName + " [" + languageId + "]");
68+
mLanguagesCache.put(languageId, language);
69+
}
70+
6371
return mLanguagesCache;
6472
}
6573

@@ -75,7 +83,11 @@ public static void resetLanguages() {
7583
}
7684

7785
public static Language getDeviceLanguage() {
78-
return mLanguagesCache.get(Resources.getSystem().getConfiguration().getLocales().get(0).toLanguageTag());
86+
return mLanguagesCache.get(getDeviceLocale().toLanguageTag());
87+
}
88+
89+
public static Locale getDeviceLocale() {
90+
return Resources.getSystem().getConfiguration().getLocales().get(0);
7991
}
8092

8193
public static List<String> getLocalesFromLanguages(@NonNull final List<Language> languages) {
@@ -97,15 +109,21 @@ public static List<Language> getPreferredLanguages(@NonNull Context aContext) {
97109
List<Language> preferredLanguages = new ArrayList<>();
98110
if (savedLanguages != null) {
99111
for (String language : savedLanguages) {
100-
Language lang = languages.get(language);
112+
Language lang = languages.get(getClosestAvailableLocale(language));
113+
if (lang != null) {
114+
lang.setPreferred(true);
115+
preferredLanguages.add(lang);
116+
}
117+
}
118+
119+
}
120+
121+
if (savedLanguages == null || savedLanguages.isEmpty()) {
122+
Language lang = languages.get(getClosestAvailableLocale(getDeviceLocale().toLanguageTag()));
123+
if (lang != null) {
101124
lang.setPreferred(true);
102125
preferredLanguages.add(lang);
103126
}
104-
105-
} else {
106-
Language currentLanguage = getDeviceLanguage();
107-
currentLanguage.setPreferred(true);
108-
preferredLanguages.add(currentLanguage);
109127
}
110128

111129
return preferredLanguages;
@@ -118,24 +136,64 @@ public static List<Language> getAvailableLanguages() {
118136
.collect(Collectors.toList());
119137
}
120138

139+
@NonNull
140+
public static String getClosestAvailableLocale(@NonNull String languageTag) {
141+
List<Locale> locales = Stream.of(Locale.getAvailableLocales()).collect(Collectors.toList());
142+
Locale inputLocale = Locale.forLanguageTag(languageTag);
143+
Optional<Locale> outputLocale = locales.stream().filter(item ->
144+
item.equals(inputLocale)
145+
).findFirst();
146+
147+
if (!outputLocale.isPresent()) {
148+
outputLocale = locales.stream().filter(item ->
149+
item.getLanguage().equals(inputLocale.getLanguage()) &&
150+
item.getScript().equals(inputLocale.getScript()) &&
151+
item.getCountry().equals(inputLocale.getCountry())
152+
).findFirst();
153+
}
154+
if (!outputLocale.isPresent()) {
155+
outputLocale = locales.stream().filter(item ->
156+
item.getLanguage().equals(inputLocale.getLanguage()) &&
157+
item.getCountry().equals(inputLocale.getCountry())
158+
).findFirst();
159+
}
160+
if (!outputLocale.isPresent()) {
161+
outputLocale = locales.stream().filter(item ->
162+
item.getLanguage().equals(inputLocale.getLanguage())
163+
).findFirst();
164+
}
165+
166+
if (outputLocale.isPresent()) {
167+
return outputLocale.get().toLanguageTag();
168+
}
169+
170+
return getDeviceLocale().toLanguageTag();
171+
}
172+
121173
@NonNull
122174
public static String getVoiceSearchLocale(@NonNull Context aContext) {
123-
return SettingsStore.getInstance(aContext).getVoiceSearchLocale();
175+
String locale = SettingsStore.getInstance(aContext).getVoiceSearchLocale();
176+
if (locale == null) {
177+
locale = LocaleUtils.getDefaultSupportedLocale(aContext);
178+
}
179+
return locale;
124180
}
125181

126182
public static void setVoiceSearchLocale(@NonNull Context context, @NonNull String locale) {
127183
SettingsStore.getInstance(context).setVoiceSearchLocale(locale);
128184
}
129185

130186
@NonNull
131-
public static String getVoiceSearchLanguage(@NonNull Context aContext) {
132-
String language = LocaleUtils.getVoiceSearchLocale(aContext);
133-
return getAllLanguages().get(language).getName();
187+
public static Language getVoiceSearchLanguage(@NonNull Context aContext) {
188+
return mLanguagesCache.get(getClosestAvailableLocale(getVoiceSearchLocale(aContext)));
134189
}
135190

136191
@NonNull
137192
public static String getDisplayLocale(Context context) {
138193
String locale = SettingsStore.getInstance(context).getDisplayLocale();
194+
if (locale == null) {
195+
locale = LocaleUtils.getDefaultSupportedLocale(context);
196+
}
139197
return mapOldLocaleToNew((locale));
140198
}
141199

@@ -144,12 +202,16 @@ public static void setDisplayLocale(@NonNull Context context, @NonNull String lo
144202
}
145203

146204
@NonNull
147-
public static String getDisplayLanguage() {
148-
return getAllLanguages().get(getCurrentLocale()).getName();
205+
public static Language getDisplayLanguage(@NonNull Context aContext) {
206+
return mLanguagesCache.get(getClosestAvailableLocale(getDisplayLocale(aContext)));
149207
}
150208

151209
public static Context setLocale(@NonNull Context context) {
152-
return updateResources(context, SettingsStore.getInstance(context).getDisplayLocale());
210+
String locale = SettingsStore.getInstance(context).getDisplayLocale();
211+
if (locale == null) {
212+
locale = LocaleUtils.getDefaultSupportedLocale(context);
213+
}
214+
return updateResources(context, locale);
153215
}
154216

155217
private static Context updateResources(@NonNull Context context, @NonNull String language) {
@@ -256,14 +318,8 @@ public static String getSupportedLocaleForIndex(int index) {
256318
return localizedSupportedLanguages.get(index).locale.toLanguageTag();
257319
}
258320

259-
public static String getDefaultSupportedLocale() {
260-
String locale = getCurrentLocale();
261-
List<String> supportedLocales = getSupportedLocales();
262-
if (!supportedLocales.contains(locale)) {
263-
return supportedLocales.get(0);
264-
}
265-
266-
return locale;
321+
public static String getDefaultSupportedLocale(@NonNull Context context) {
322+
return getClosestSupportedLocale(context, getDeviceLocale().toLanguageTag());
267323
}
268324

269325
public static String getClosestSupportedLocale(@NonNull Context context, @NonNull String languageTag) {
@@ -295,7 +351,8 @@ public static String getClosestSupportedLocale(@NonNull Context context, @NonNul
295351
return language.get().locale.toLanguageTag();
296352

297353
} else {
298-
return getDisplayLocale(context);
354+
// If there is no closest supported locale we fallback to en-US
355+
return "en-US";
299356
}
300357
}
301358

0 commit comments

Comments
 (0)