Bug 1379905 - Switch mozIntl, datetimebox and DateTimeFormat to use GetRegionalPrefsLocales. r=jfkthame,smaug
authorZibi Braniecki <zbraniecki@mozilla.com>
Fri, 14 Jul 2017 16:47:23 -0700
changeset 419338 bfdd14469c9906eaa81c7d6a62ec7525aa109456
parent 419337 b31b1c1d8b12a15fcc7cc36819c679503298ba69
child 419339 07052313bf75f6a1fd9f460f146b998824302d64
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjfkthame, smaug
bugs1379905
milestone56.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1379905 - Switch mozIntl, datetimebox and DateTimeFormat to use GetRegionalPrefsLocales. r=jfkthame,smaug MozReview-Commit-ID: IqGbT6ZUfuN
dom/base/nsGlobalWindow.cpp
dom/base/nsGlobalWindow.h
dom/tests/mochitest/chrome/chrome.ini
dom/tests/mochitest/chrome/test_intlUtils_getLocaleInfo.html
dom/tests/mochitest/chrome/test_window_getAppLocales.html
dom/tests/mochitest/chrome/test_window_getRegionalPrefsLocales.html
dom/webidl/Window.webidl
intl/locale/DateTimeFormat.cpp
toolkit/components/mozintl/mozIntl.js
toolkit/content/widgets/datetimebox.xml
--- a/dom/base/nsGlobalWindow.cpp
+++ b/dom/base/nsGlobalWindow.cpp
@@ -15490,23 +15490,23 @@ nsGlobalWindow::GetPaintWorklet(ErrorRes
 
     mPaintWorklet = new Worklet(AsInner(), principal, Worklet::ePaintWorklet);
   }
 
   return mPaintWorklet;
 }
 
 void
-nsGlobalWindow::GetAppLocalesAsBCP47(nsTArray<nsString>& aLocales)
-{
-  nsTArray<nsCString> appLocales;
-  mozilla::intl::LocaleService::GetInstance()->GetAppLocalesAsBCP47(appLocales);
-
-  for (uint32_t i = 0; i < appLocales.Length(); i++) {
-    aLocales.AppendElement(NS_ConvertUTF8toUTF16(appLocales[i]));
+nsGlobalWindow::GetRegionalPrefsLocales(nsTArray<nsString>& aLocales)
+{
+  AutoTArray<nsCString, 10> rpLocales;
+  mozilla::intl::LocaleService::GetInstance()->GetRegionalPrefsLocales(rpLocales);
+
+  for (const auto& loc : rpLocales) {
+    aLocales.AppendElement(NS_ConvertUTF8toUTF16(loc));
   }
 }
 
 #ifdef ENABLE_INTL_API
 IntlUtils*
 nsGlobalWindow::GetIntlUtils(ErrorResult& aError)
 {
   MOZ_RELEASE_ASSERT(IsInnerWindow());
--- a/dom/base/nsGlobalWindow.h
+++ b/dom/base/nsGlobalWindow.h
@@ -964,17 +964,17 @@ public:
 
   mozilla::dom::Worklet*
   GetAudioWorklet(mozilla::ErrorResult& aRv);
 
   mozilla::dom::Worklet*
   GetPaintWorklet(mozilla::ErrorResult& aRv);
 
   void
-  GetAppLocalesAsBCP47(nsTArray<nsString>& aLocales);
+  GetRegionalPrefsLocales(nsTArray<nsString>& aLocales);
 
 #ifdef ENABLE_INTL_API
   mozilla::dom::IntlUtils*
   GetIntlUtils(mozilla::ErrorResult& aRv);
 #endif
 
 protected:
   bool AlertOrConfirm(bool aAlert, const nsAString& aMessage,
--- a/dom/tests/mochitest/chrome/chrome.ini
+++ b/dom/tests/mochitest/chrome/chrome.ini
@@ -86,9 +86,9 @@ skip-if = os == 'linux'
 [test_sandbox_postMessage.html]
 [test_selectAtPoint.html]
 [test_sizemode_attribute.xul]
 tags = fullscreen
 skip-if = os != 'win'
 [test_subscript_bindings.xul]
 [test_xray_event_constructor.xul]
 [test_clipboard_events_chrome.html]
-[test_window_getAppLocales.html]
+[test_window_getRegionalPrefsLocales.html]
--- a/dom/tests/mochitest/chrome/test_intlUtils_getLocaleInfo.html
+++ b/dom/tests/mochitest/chrome/test_intlUtils_getLocaleInfo.html
@@ -15,17 +15,17 @@ https://bugzilla.mozilla.org/show_bug.cg
 <script>
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const localeService =
   Cc["@mozilla.org/intl/localeservice;1"].getService(Ci.mozILocaleService);
 const mozIntl = Cc["@mozilla.org/mozintl;1"].getService(Ci.mozIMozIntl);
 
-let appLocale = localeService.getAppLocalesAsBCP47()[0];
+let rpLocale = localeService.getRegionalPrefsLocales()[0];
 
 let testData = [
   {
     locales: ["en-US"],
     expected: {
       locale: "en-US",
       direction: "ltr",
     }
@@ -39,22 +39,22 @@ let testData = [
   },
   {
     locales: ["ar"],
     expected: {
       locale: "ar",
       direction: "rtl",
     }
   },
-  // IntlUtils uses current app locales if locales is not provided.
+  // IntlUtils uses current regional prefs locales if locales is not provided.
   {
     locales: [],
     expected: {
-      locale: appLocale,
-      direction: mozIntl.getLocaleInfo(appLocale).direction,
+      locale: rpLocale,
+      direction: mozIntl.getLocaleInfo(rpLocale).direction,
     }
   },
 ];
 
 let intlUtils = window.intlUtils;
 ok(intlUtils, "window.intlUtils should exist");
 
 for (let { locales, expected } of testData) {
rename from dom/tests/mochitest/chrome/test_window_getAppLocales.html
rename to dom/tests/mochitest/chrome/test_window_getRegionalPrefsLocales.html
--- a/dom/tests/mochitest/chrome/test_window_getAppLocales.html
+++ b/dom/tests/mochitest/chrome/test_window_getRegionalPrefsLocales.html
@@ -9,15 +9,15 @@ https://bugzilla.mozilla.org/show_bug.cg
   <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1337234">Mozilla Bug 1337234</a>
 <p id="display"></p>
 <div id="content" style="display: none">
 <script>
 
-let appLocales = window.getAppLocalesAsBCP47();
-ok(appLocales.length > 0, "getAppLocales returns at least one locale.");
-is(appLocales[0], "en-US", "The first app locale should be en-US.");
+let rpLocales = window.getRegionalPrefsLocales();
+ok(rpLocales.length > 0, "getRegionalPrefsLocales returns at least one locale.");
+is(rpLocales[0], "en-US", "The first regional prefs locale should be en-US.");
 
 </script>
 </body>
 </html>
--- a/dom/webidl/Window.webidl
+++ b/dom/webidl/Window.webidl
@@ -514,27 +514,32 @@ callback IdleRequestCallback = void (Idl
  * particular do not use this to gate access to JavaScript APIs.
  */
 partial interface Window {
   [ChromeOnly] readonly attribute boolean isSecureContextIfOpenerIgnored;
 };
 
 partial interface Window {
   /**
-   * Returns a list of locales that the application should be localized to.
+   * Returns a list of locales that the internationalization components
+   * should be localized to.
+   *
+   * The function name refers to Regional Preferences which can be either
+   * fetched from the internal internationalization database (CLDR), or
+   * from the host environment.
    *
    * The result is a sorted list of valid locale IDs and it should be
    * used for all APIs that accept list of locales, like ECMA402 and L10n APIs.
    *
    * This API always returns at least one locale.
    *
    * Example: ["en-US", "de", "pl", "sr-Cyrl", "zh-Hans-HK"]
    */
   [Func="IsChromeOrXBL"]
-  sequence<DOMString> getAppLocalesAsBCP47();
+  sequence<DOMString> getRegionalPrefsLocales();
 
 #ifdef ENABLE_INTL_API
   /**
    * Getter funcion for IntlUtils, which provides helper functions for
    * localization.
    */
   [Throws, Func="IsChromeOrXBL"]
   readonly attribute IntlUtils intlUtils;
--- a/intl/locale/DateTimeFormat.cpp
+++ b/intl/locale/DateTimeFormat.cpp
@@ -20,19 +20,19 @@ nsCString* DateTimeFormat::mLocale = nul
 /*static*/ nsresult
 DateTimeFormat::Initialize()
 {
   if (mLocale) {
     return NS_OK;
   }
 
   mLocale = new nsCString();
-  nsAutoCString locale;
-  intl::LocaleService::GetInstance()->GetAppLocaleAsBCP47(locale);
-  mLocale->Assign(locale);
+  AutoTArray<nsCString, 10> regionalPrefsLocales;
+  intl::LocaleService::GetInstance()->GetRegionalPrefsLocales(regionalPrefsLocales);
+  mLocale->Assign(regionalPrefsLocales[0]);
 
   return NS_OK;
 }
 
 // performs a locale sensitive date formatting operation on the time_t parameter
 /*static*/ nsresult
 DateTimeFormat::FormatTime(const nsDateFormatSelector aDateFormatSelector,
                            const nsTimeFormatSelector aTimeFormatSelector,
--- a/toolkit/components/mozintl/mozIntl.js
+++ b/toolkit/components/mozintl/mozIntl.js
@@ -11,32 +11,22 @@ const mozIntlHelper =
   Cc["@mozilla.org/mozintlhelper;1"].getService(Ci.mozIMozIntlHelper);
 const localeSvc =
   Cc["@mozilla.org/intl/localeservice;1"].getService(Ci.mozILocaleService);
 const osPrefs =
   Cc["@mozilla.org/intl/ospreferences;1"].getService(Ci.mozIOSPreferences);
 
 /**
  * This helper function retrives currently used app locales, allowing
- * all mozIntl APIs to use the current app locales unless called with
- * explicitly listed locales.
+ * all mozIntl APIs to use the current regional prefs locales unless
+ * called with explicitly listed locales.
  */
 function getLocales(locales) {
   if (!locales) {
-    return localeSvc.getAppLocalesAsBCP47();
-  }
-  return locales;
-}
-
-function getLocale(locales) {
-  if (!locales) {
-    return localeSvc.getAppLocale();
-  }
-  if (Array.isArray(locales)) {
-    return [0];
+    return localeSvc.getRegionalPrefsLocales();
   }
   return locales;
 }
 
 function getDateTimePatternStyle(option) {
   switch (option) {
     case "full":
       return osPrefs.dateTimeFormatStyleFull;
--- a/toolkit/content/widgets/datetimebox.xml
+++ b/toolkit/content/widgets/datetimebox.xml
@@ -1197,17 +1197,17 @@
       </html:div>
     </content>
 
     <implementation implements="nsIDateTimeInputArea">
       <constructor>
       <![CDATA[
         this.DEBUG = false;
         this.mInputElement = this.parentNode;
-        this.mLocales = window.getAppLocalesAsBCP47();
+        this.mLocales = window.getRegionalPrefsLocales();
 
         this.mIsRTL = false;
         let intlUtils = window.intlUtils;
         if (intlUtils) {
           this.mIsRTL =
             intlUtils.getLocaleInfo(this.mLocales).direction === "rtl";
         }