Bug 1446202 - 1. Return correct locale string to Gecko; r=droeh
authorJim Chen <nchen@mozilla.com>
Mon, 02 Apr 2018 23:25:27 -0400
changeset 776560 ae4d46495f5e78721e9cd68dc46e11b13592b875
parent 776559 35f5e13e1098d838580b83f63bc3a52547ebd213
child 776561 9ca671c7ec5bb860d67fdfe2dafdc0a6daef48f9
push id104907
push userbmo:ato@sny.no
push dateTue, 03 Apr 2018 10:28:20 +0000
reviewersdroeh
bugs1446202
milestone61.0a1
Bug 1446202 - 1. Return correct locale string to Gecko; r=droeh Locale.toString() doesn't return the correct locale format that Gecko expects, so reformat the locale string before returning. (for example, Locale.toString() returns "en_US_POSIX#Latn" whereas Gecko expects "en-Latn-US-POSIX"). The incorrect locale format was preventing intl code (and the datetime box) from initializing. MozReview-Commit-ID: JmDYkDj31pL
mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java
@@ -71,16 +71,17 @@ import android.hardware.SensorManager;
 import android.location.Criteria;
 import android.location.Location;
 import android.location.LocationListener;
 import android.location.LocationManager;
 import android.media.AudioManager;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
 import android.net.Uri;
+import android.os.Build;
 import android.os.Bundle;
 import android.os.Environment;
 import android.os.Looper;
 import android.os.ParcelFileDescriptor;
 import android.os.PowerManager;
 import android.os.SystemClock;
 import android.os.Vibrator;
 import android.provider.Settings;
@@ -1867,11 +1868,26 @@ public class GeckoAppShell
         if (prop == null) {
             return 0;
         }
         return Integer.parseInt(prop);
     }
 
     @WrapForJNI
     public static String getDefaultLocale() {
-        return Locale.getDefault().toString();
+        final Locale locale = Locale.getDefault();
+        if (Build.VERSION.SDK_INT >= 21) {
+            return locale.toLanguageTag();
+        }
+
+        final StringBuilder out = new StringBuilder(locale.getLanguage());
+        final String country = locale.getCountry();
+        final String variant = locale.getVariant();
+        if (!TextUtils.isEmpty(country)) {
+            out.append('-').append(country);
+        }
+        if (!TextUtils.isEmpty(variant)) {
+            out.append('-').append(variant);
+        }
+        // e.g. "en", "en-US", or "en-US-POSIX".
+        return out.toString();
     }
 }