... |
... |
@@ -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
|
*
|
... |
... |
@@ -539,6 +550,7 @@ public final class GeckoRuntimeSettings extends RuntimeSettings { |
539
|
550
|
/* package */ int mScreenHeightOverride;
|
540
|
551
|
/* package */ Class<? extends Service> mCrashHandler;
|
541
|
552
|
/* package */ String[] mRequestedLocales;
|
|
553
|
+ /* package */ HashMap<Locale, Locale> mSupportedLocales = new HashMap<>();
|
542
|
554
|
/* package */ RuntimeTelemetry.Proxy mTelemetryProxy;
|
543
|
555
|
|
544
|
556
|
/**
|
... |
... |
@@ -595,6 +607,7 @@ public final class GeckoRuntimeSettings extends RuntimeSettings { |
595
|
607
|
mRequestedLocales = settings.mRequestedLocales;
|
596
|
608
|
mConfigFilePath = settings.mConfigFilePath;
|
597
|
609
|
mTelemetryProxy = settings.mTelemetryProxy;
|
|
610
|
+ mSupportedLocales = settings.mSupportedLocales;
|
598
|
611
|
}
|
599
|
612
|
|
600
|
613
|
/* package */ void commit() {
|
... |
... |
@@ -803,30 +816,39 @@ public final class GeckoRuntimeSettings extends RuntimeSettings { |
803
|
816
|
EventDispatcher.getInstance().dispatch("GeckoView:SetLocale", data);
|
804
|
817
|
}
|
805
|
818
|
|
|
819
|
+ private Locale getLocaleIfSupported(String tag) {
|
|
820
|
+ Locale exact = Locale.forLanguageTag(tag);
|
|
821
|
+ if (mSupportedLocales.containsKey(exact)) {
|
|
822
|
+ return exact;
|
|
823
|
+ }
|
|
824
|
+ Locale fallback = new Locale(exact.getLanguage());
|
|
825
|
+ return mSupportedLocales.get(fallback);
|
|
826
|
+ }
|
|
827
|
+
|
806
|
828
|
private String computeAcceptLanguages() {
|
807
|
|
- final ArrayList<String> locales = new ArrayList<String>();
|
808
|
|
-
|
809
|
|
- // In Desktop, these are defined in the `intl.accept_languages` localized property.
|
810
|
|
- // At some point we should probably use the same values here, but for now we use a simple
|
811
|
|
- // strategy which will hopefully result in reasonable acceptLanguage values.
|
812
|
|
- if (mRequestedLocales != null && mRequestedLocales.length > 0) {
|
813
|
|
- String locale = mRequestedLocales[0].toLowerCase(Locale.ROOT);
|
814
|
|
- // No need to include `en-us` twice.
|
815
|
|
- if (!locale.equals("en-us")) {
|
816
|
|
- locales.add(locale);
|
817
|
|
- if (locale.contains("-")) {
|
818
|
|
- String lang = locale.split("-")[0];
|
819
|
|
- // No need to include `en` twice.
|
820
|
|
- if (!lang.equals("en")) {
|
821
|
|
- locales.add(lang);
|
822
|
|
- }
|
|
829
|
+ Locale locale = null;
|
|
830
|
+ if (mRequestedLocales != null) {
|
|
831
|
+ for (String tag : mRequestedLocales) {
|
|
832
|
+ locale = getLocaleIfSupported(tag);
|
|
833
|
+ if (locale != null) {
|
|
834
|
+ break;
|
823
|
835
|
}
|
824
|
836
|
}
|
825
|
837
|
}
|
826
|
|
- locales.add("en-us");
|
827
|
|
- locales.add("en");
|
828
|
|
-
|
829
|
|
- return TextUtils.join(",", locales);
|
|
838
|
+ if (locale == null) {
|
|
839
|
+ for (final String tag : getDefaultLocales()) {
|
|
840
|
+ locale = getLocaleIfSupported(tag);
|
|
841
|
+ if (locale != null) {
|
|
842
|
+ break;
|
|
843
|
+ }
|
|
844
|
+ }
|
|
845
|
+ }
|
|
846
|
+ String acceptLanguages = locale != null ? locale.toString().replace('_', '-') : "en-US";
|
|
847
|
+ if (acceptLanguages.equals("en-US")) {
|
|
848
|
+ // For consistency with spoof English.
|
|
849
|
+ acceptLanguages += ", en";
|
|
850
|
+ }
|
|
851
|
+ return acceptLanguages;
|
830
|
852
|
}
|
831
|
853
|
|
832
|
854
|
private static String[] getDefaultLocales() {
|