summaryrefslogtreecommitdiffstats
path: root/comm/suite/components/pref/content/pref-appearance.js
blob: a54b14786d8e544999f0a1ac1be8afd2538220ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Load spell-checker module to properly determine language strings
var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

function Startup()
{
  SwitchLocales_Load();
  NumberLocales_Load();
}

/**
 * From locale switcher's switch.js:
 * Load available locales into selection menu
 */
function SwitchLocales_Load() {
  var menulist = document.getElementById("switchLocales");

  var cr = Cc["@mozilla.org/chrome/chrome-registry;1"]
             .getService(Ci.nsIToolkitChromeRegistry);

  var langNames = document.getElementById("languageNamesBundle");
  var regNames  = document.getElementById("regionNamesBundle");

  var matched = false;
  var currentLocale = Services.locale.getRequestedLocale() || undefined;
  var locales = cr.getLocalesForPackage("global");

  while (locales.hasMore()) {
    var locale = locales.getNext();

    var parts = locale.split(/-/);

    var displayName;
    try {
      displayName = langNames.getString(parts[0]);
      if (parts.length > 1) {
        try {
          displayName += " (" + regNames.getString(parts[1].toLowerCase()) + ")";
        }
        catch (e) {
          displayName += " (" + parts[1] + ")";
        }
      }
    }
    catch (e) {
      displayName = locale;
    }

    var item = menulist.appendItem(displayName, locale);
    if (!matched && currentLocale && currentLocale == locale) {
      matched = true;
      menulist.selectedItem = item;
    }
  }
  // If somehow we have not found the current locale, select the first in list.
  if (!matched) {
    menulist.selectedIndex = 1;
  }
}

/**
 * Determine the appropriate value to set and set it.
 */
function SelectLocale(aElement) {
  var locale = aElement.value;
  var currentLocale = Services.locale.getRequestedLocale() || undefined;
  if (!currentLocale || (currentLocale && currentLocale != locale)) {
    Services.locale.setRequestedLocales([locale]);
  }
}

/**
 * When starting up, determine application and regional locale settings
 * and add the respective strings to the prefpane labels.
 */
function NumberLocales_Load()
{
  const osprefs =
    Cc["@mozilla.org/intl/ospreferences;1"]
      .getService(Ci.mozIOSPreferences);

  let appLocale = Services.locale.appLocalesAsBCP47[0];
  let rsLocale = osprefs.regionalPrefsLocales[0];
  let names = Services.intl.getLocaleDisplayNames(undefined, [appLocale, rsLocale]);

  let appLocaleRadio = document.getElementById("appLocale");
  let rsLocaleRadio = document.getElementById("rsLocale");
  let prefutilitiesBundle = document.getElementById("bundle_prefutilities");

  let appLocaleLabel = prefutilitiesBundle.getFormattedString("appLocale.label",
                                                              [names[0]]);
  let rsLocaleLabel = prefutilitiesBundle.getFormattedString("rsLocale.label",
                                                             [names[1]]);
  appLocaleRadio.setAttribute("label", appLocaleLabel);
  rsLocaleRadio.setAttribute("label", rsLocaleLabel);
  appLocaleRadio.accessKey = prefutilitiesBundle.getString("appLocale.accesskey");
  rsLocaleRadio.accessKey = prefutilitiesBundle.getString("rsLocale.accesskey");
}