... |
... |
@@ -22,7 +22,8 @@ import androidx.annotation.NonNull; |
22
|
22
|
import androidx.annotation.Nullable;
|
23
|
23
|
import java.lang.annotation.Retention;
|
24
|
24
|
import java.lang.annotation.RetentionPolicy;
|
25
|
|
-import java.util.ArrayList;
|
|
25
|
+import java.util.Collection;
|
|
26
|
+import java.util.HashMap;
|
26
|
27
|
import java.util.Locale;
|
27
|
28
|
import org.mozilla.gecko.EventDispatcher;
|
28
|
29
|
import org.mozilla.gecko.GeckoSystemStateListener;
|
... |
... |
@@ -455,6 +456,16 @@ public final class GeckoRuntimeSettings extends RuntimeSettings { |
455
|
456
|
return this;
|
456
|
457
|
}
|
457
|
458
|
|
|
459
|
+ public @NonNull Builder supportedLocales(final Collection<String> locales) {
|
|
460
|
+ getSettings().mSupportedLocales.clear();
|
|
461
|
+ for (String tag : locales) {
|
|
462
|
+ Locale locale = Locale.forLanguageTag(tag);
|
|
463
|
+ getSettings().mSupportedLocales.put(locale, locale);
|
|
464
|
+ getSettings().mSupportedLocales.put(new Locale(locale.getLanguage()), locale);
|
|
465
|
+ }
|
|
466
|
+ return this;
|
|
467
|
+ }
|
|
468
|
+
|
458
|
469
|
/**
|
459
|
470
|
* Sets whether we should spoof locale to English for webpages.
|
460
|
471
|
*
|
... |
... |
@@ -546,6 +557,7 @@ public final class GeckoRuntimeSettings extends RuntimeSettings { |
546
|
557
|
/* package */ int mScreenHeightOverride;
|
547
|
558
|
/* package */ Class<? extends Service> mCrashHandler;
|
548
|
559
|
/* package */ String[] mRequestedLocales;
|
|
560
|
+ /* package */ HashMap<Locale, Locale> mSupportedLocales = new HashMap<>();
|
549
|
561
|
/* package */ RuntimeTelemetry.Proxy mTelemetryProxy;
|
550
|
562
|
|
551
|
563
|
/**
|
... |
... |
@@ -602,6 +614,7 @@ public final class GeckoRuntimeSettings extends RuntimeSettings { |
602
|
614
|
mRequestedLocales = settings.mRequestedLocales;
|
603
|
615
|
mConfigFilePath = settings.mConfigFilePath;
|
604
|
616
|
mTelemetryProxy = settings.mTelemetryProxy;
|
|
617
|
+ mSupportedLocales = settings.mSupportedLocales;
|
605
|
618
|
}
|
606
|
619
|
|
607
|
620
|
/* package */ void commit() {
|
... |
... |
@@ -810,30 +823,39 @@ public final class GeckoRuntimeSettings extends RuntimeSettings { |
810
|
823
|
EventDispatcher.getInstance().dispatch("GeckoView:SetLocale", data);
|
811
|
824
|
}
|
812
|
825
|
|
|
826
|
+ private Locale getLocaleIfSupported(String tag) {
|
|
827
|
+ Locale exact = Locale.forLanguageTag(tag);
|
|
828
|
+ if (mSupportedLocales.containsKey(exact)) {
|
|
829
|
+ return exact;
|
|
830
|
+ }
|
|
831
|
+ Locale fallback = new Locale(exact.getLanguage());
|
|
832
|
+ return mSupportedLocales.get(fallback);
|
|
833
|
+ }
|
|
834
|
+
|
813
|
835
|
private String computeAcceptLanguages() {
|
814
|
|
- final ArrayList<String> locales = new ArrayList<String>();
|
815
|
|
-
|
816
|
|
- // In Desktop, these are defined in the `intl.accept_languages` localized property.
|
817
|
|
- // At some point we should probably use the same values here, but for now we use a simple
|
818
|
|
- // strategy which will hopefully result in reasonable acceptLanguage values.
|
819
|
|
- if (mRequestedLocales != null && mRequestedLocales.length > 0) {
|
820
|
|
- String locale = mRequestedLocales[0].toLowerCase(Locale.ROOT);
|
821
|
|
- // No need to include `en-us` twice.
|
822
|
|
- if (!locale.equals("en-us")) {
|
823
|
|
- locales.add(locale);
|
824
|
|
- if (locale.contains("-")) {
|
825
|
|
- String lang = locale.split("-")[0];
|
826
|
|
- // No need to include `en` twice.
|
827
|
|
- if (!lang.equals("en")) {
|
828
|
|
- locales.add(lang);
|
829
|
|
- }
|
|
836
|
+ Locale locale = null;
|
|
837
|
+ if (mRequestedLocales != null) {
|
|
838
|
+ for (String tag : mRequestedLocales) {
|
|
839
|
+ locale = getLocaleIfSupported(tag);
|
|
840
|
+ if (locale != null) {
|
|
841
|
+ break;
|
830
|
842
|
}
|
831
|
843
|
}
|
832
|
844
|
}
|
833
|
|
- locales.add("en-us");
|
834
|
|
- locales.add("en");
|
835
|
|
-
|
836
|
|
- return TextUtils.join(",", locales);
|
|
845
|
+ if (locale == null) {
|
|
846
|
+ for (final String tag : getDefaultLocales()) {
|
|
847
|
+ locale = getLocaleIfSupported(tag);
|
|
848
|
+ if (locale != null) {
|
|
849
|
+ break;
|
|
850
|
+ }
|
|
851
|
+ }
|
|
852
|
+ }
|
|
853
|
+ String acceptLanguages = locale != null ? locale.toString().replace('_', '-') : "en-US";
|
|
854
|
+ if (acceptLanguages.equals("en-US")) {
|
|
855
|
+ // For consistency with spoof English.
|
|
856
|
+ acceptLanguages += ", en";
|
|
857
|
+ }
|
|
858
|
+ return acceptLanguages;
|
837
|
859
|
}
|
838
|
860
|
|
839
|
861
|
private static String[] getDefaultLocales() {
|