[tbb-commits] [tor-launcher/master] Bug 17344: enumerate available langpacks for lang prompt

brade at torproject.org brade at torproject.org
Fri Nov 20 20:45:09 UTC 2015


commit 20f01697b19ce938b894634d3ca4e39218137ea4
Author: Kathy Brade <brade at pearlcrescent.com>
Date:   Fri Nov 20 15:26:56 2015 -0500

    Bug 17344: enumerate available langpacks for lang prompt
    
    Remove the hard-coded list of languages and build the listbox items
    from the set of installed language packs.
---
 src/chrome/content/localePicker.xul    |   19 +------
 src/chrome/content/network-settings.js |   97 +++++++++++++++++++++++++++-----
 2 files changed, 85 insertions(+), 31 deletions(-)

diff --git a/src/chrome/content/localePicker.xul b/src/chrome/content/localePicker.xul
index be82e14..5b93825 100644
--- a/src/chrome/content/localePicker.xul
+++ b/src/chrome/content/localePicker.xul
@@ -35,24 +35,7 @@
     <vbox>
       <label class="question">&torlauncher.localePicker.prompt;</label>
       <separator/>
-      <listbox id="localeList" ondblclick="onLocaleListDoubleClick()">
-        <listitem value="en-US" label="English" selected="true" />
-        <listitem value="ar" label="&#x0627;&#x0644;&#x0639;&#x0631;&#x0628;&#x064a;&#x0629;" />
-        <listitem value="de" label="Deutsch" />
-        <listitem value="es-ES" label="Espa&#x00f1;ol" />
-        <listitem value="fa" label="&#x0641;&#x0627;&#x0631;&#x0633;&#x06cc;" />
-        <listitem value="fr" label="Fran&#x00e7;ais" />
-        <listitem value="it" label="Italiano" />
-        <listitem value="ja" label="日本語" />
-        <listitem value="ko" label="한국어" />
-        <listitem value="nl" label="Nederlands" />
-        <listitem value="pl" label="Polski" />
-        <listitem value="pt-PT" label="Português (Europeu)" />
-        <listitem value="ru" label="&#x0420;&#x0443;&#x0441;&#x0441;&#x043a;&#x0438;&#x0439;" />
-        <listitem value="tr" label="Türkçe" />
-        <listitem value="vi" label="Tiếng Việt" />
-        <listitem value="zh-CN" label="&#x7b80;&#x4f53;&#x5b57;" />
-      </listbox>
+      <listbox id="localeList" ondblclick="onLocaleListDoubleClick()"/>
     </vbox>
   </wizardpage>
 
diff --git a/src/chrome/content/network-settings.js b/src/chrome/content/network-settings.js
index 2382ea6..9d3cb60 100644
--- a/src/chrome/content/network-settings.js
+++ b/src/chrome/content/network-settings.js
@@ -247,27 +247,98 @@ function initLocaleDialog()
   if (nextBtn && doneBtn)
     doneBtn.label = nextBtn.label;
 
-  // Select the current language by default.
+  let { AddonManager } = Cu.import("resource://gre/modules/AddonManager.jsm");
+  AddonManager.getAddonsByTypes(["locale"], function(aLangPackAddons)
+      {
+        populateLocaleList(aLangPackAddons);
+        resizeDialogToFitContent();
+        TorLauncherLogger.log(2, "initLocaleDialog done");
+      });
+}
+
+
+function populateLocaleList(aLangPackAddons)
+{
+  let knownLanguages = {
+    "en-US" : "English",
+    "ar"    : "\u0627\u0644\u0639\u0631\u0628\u064a\u0629",
+    "de"    : "Deutsch",
+    "es-ES" : "Espa\u00f1ol",
+    "fa"    : "\u0641\u0627\u0631\u0633\u06cc",
+    "fr"    : "Fran\u00e7ais",
+    "it"    : "Italiano",
+    "ja"    : "\u65e5\u672c\u8a9e",
+    "ko"    : "\ud55c\uad6d\uc5b4",
+    "nl"    : "Nederlands",
+    "pl"    : "Polski",
+    "pt-PT" : "Portugu\u00eas (Europeu)",
+    "ru"    : "\u0420\u0443\u0441\u0441\u043a\u0438\u0439",
+    "tr"    : "T\u00fcrk\u00e7e",
+    "vi"    : "Ti\u1ebfng Vi\u1ec7t",
+    "zh-CN" : "\u7b80\u4f53\u5b57"
+  };
+
+  // Retrieve the current locale so we can select it within the list by default.
+  let curLocale;
   try
   {
     let chromeRegSvc = Cc["@mozilla.org/chrome/chrome-registry;1"]
                          .getService(Ci.nsIXULChromeRegistry);
-    let curLocale = chromeRegSvc.getSelectedLocale("global").toLowerCase();
-    let localeList = document.getElementById(kLocaleList);
-    for (let i = 0; i < localeList.itemCount; ++i)
+    curLocale = chromeRegSvc.getSelectedLocale("global").toLowerCase();
+  } catch (e) {}
+
+  // Build a list of language info objects (language code plus friendly name).
+  let foundCurLocale = false;
+  let langInfo = [];
+  for (let addon of aLangPackAddons)
+  {
+    let uri = addon.getResourceURI("");
+    // The add-on IDs look like langpack-LANGCODE at firefox.mozilla.org
+    let matchResult = addon.id.match(/^langpack-(.*)@.*\.mozilla\.org/);
+    let code = (matchResult) ? matchResult[1] : addon.id;
+    if (code == "ja-JP-mac")
+      code = "ja";
+    let name = knownLanguages[code];
+    if (!name)
     {
-      let item = localeList.getItemAtIndex(i);
-      if (item.value.toLowerCase() == curLocale)
-      {
-        localeList.selectedIndex = i;
-        break;
-      }
+      // We do not have a name for this language pack. Use some heuristics.
+      name = addon.name;
+      let idx = name.lastIndexOf(" Language Pack");
+      if (idx > 0)
+        name = name.substring(0, idx);
     }
-  } catch (e) {}
+    let isSelected = (curLocale && (code.toLowerCase() == curLocale));
+    langInfo.push({ langCode: code, langName: name, isSelected: isSelected } );
+    if (isSelected && !foundCurLocale)
+      foundCurLocale = true;
+  }
 
-  resizeDialogToFitContent();
+  // Sort by language code.
+  langInfo.sort(function(aObj1, aObj2) {
+      if (aObj1.langCode == aObj2.langCode)
+        return 0;
+
+      return (aObj1.langCode < aObj2.langCode) ? -1 : 1;
+    });
 
-  TorLauncherLogger.log(2, "initLocaleDialog done");
+  // Add en-US to the beginning of the list.
+  let code = "en-US";
+  let name = knownLanguages[code];
+  let isSelected = !foundCurLocale;  // select English if nothing else matched
+  langInfo.splice(0, 0,
+                  { langCode: code, langName: name, isSelected: isSelected });
+
+  // Populate the XUL listbox.
+  let localeList = document.getElementById(kLocaleList);
+  for (let infoObj of langInfo)
+  {
+    let listItem = document.createElement("listitem");
+    listItem.setAttribute("value", infoObj.langCode);
+    listItem.setAttribute("label", infoObj.langName);
+    localeList.appendChild(listItem);
+    if (infoObj.isSelected)
+      localeList.selectedItem = listItem;
+  }
 }
 
 



More information about the tbb-commits mailing list