switchLocale.js
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25  */
26 
34 Components.utils.import("resource://app/jsmodules/StringUtils.jsm");
35 
37 
38 try {
39  // Module specific global for auto-init/deinit support
40  var menubar_locales_bundle_module = {};
42 
43  menubar_locales_bundle_module.init_once = 0;
44  menubar_locales_bundle_module.deinit_once = 0;
45  menubar_locales_bundle_module.onLoad = function()
46  {
47  if (menubar_locales_bundle_module.init_once++) { dump("WARNING: menubar_locales_bundle double init!!\n"); return; }
49  }
50  menubar_locales_bundle_module.onUnload = function()
51  {
52  if (menubar_locales_bundle_module.deinit_once++) { dump("WARNING: menubar_locales_bundle double deinit!!\n"); return; }
53  window.removeEventListener("load", menubar_locales_bundle_module.onLoad, false);
54  window.removeEventListener("unload", menubar_locales_bundle_module.onUnload, false);
56  }
57 
58  // Auto-init/deinit registration
59  window.addEventListener("load", menubar_locales_bundle_module.onLoad, false);
60  window.addEventListener("unload", menubar_locales_bundle_module.onUnload, false);
61 
62 
64 
69  function initLocalesBundle() {
70  try {
71  var sbIBundle = new Components.Constructor("@songbirdnest.com/Songbird/Bundle;1", "sbIBundle");
72  menubar_locales_bundle = new sbIBundle();
73  menubar_locales_bundle.bundleId = "locales";
74  menubar_locales_bundle.bundleURL = Application.prefs.getValue("songbird.url.locales", "default");
75  menubar_locales_bundle.addBundleDataListener(menubarLocalesBundleCB);
76  menubar_locales_bundle.retrieveBundleData(MENUBAR_LOCALESBUNDLE_TIMEOUT);
77  } catch ( err ) {
78  SB_LOG("initLocalesBundle", "" + err );
79  }
80  }
81 
86  function resetLocalesBundle() {
87  if (menubar_locales_bundle) menubar_locales_bundle.removeBundleDataListener(menubarLocalesBundleCB);
88  menubar_locales_bundle = null;
89  }
90 
97  {
98  onDownloadComplete: function(bundle) { menubarLocalesBundleDataReady(); },
99  onError: function(bundle) { menubarLocalesBundleDataReady(); },
100  QueryInterface : function(aIID)
101  {
102  if (!aIID.equals(Components.interfaces.sbIBundleDataListener) &&
103  !aIID.equals(Components.interfaces.nsISupports))
104  {
105  throw Components.results.NS_ERROR_NO_INTERFACE;
106  }
107  return this;
108  }
109  }
110 
115  function menubarLocalesBundleDataReady() {
116  if (menubar_locales_bundle.bundleExtensionCount > 0) {
117  if (_lastmenu) fillLocaleList(_lastmenu);
118  }
119  }
120 
125  function menubarLoadBundledLocales(menu) {
126 
127  if (menubar_locales_bundle && menubar_locales_bundle.bundleExtensionCount > 0) {
128 
129  var className = menu.parentNode.getAttribute("class");
130 
131  var separator = null;
132 
133  var sbs = Components.classes["@mozilla.org/intl/stringbundle;1"]
134  .getService(Components.interfaces.nsIStringBundleService);
135  var langNames = sbs.createBundle("chrome://global/locale/languageNames.properties");
136  var regNames = sbs.createBundle("chrome://global/locale/regionNames.properties");
137 
138  var elements = Array();
139 
140  for (var i=0; i < menubar_locales_bundle.bundleExtensionCount; i++) {
141  menubar_locales_bundle.setExtensionInstallFlag(i, false);
142 
143  var locale = menubar_locales_bundle.getExtensionAttribute(i, "languageTag");
144  if (!locale) {
145  // Umm, bad server response?
146  var localeID = menubar_locales_bundle.getExtensionAttribute(i, "id");
147  var localeMatch = /langpack-([^@]+)/(localeID);
148  if (localeMatch) {
149  locale = localeMatch[1];
150  } else {
151  // really bad server response; ignore this locale
152  continue;
153  }
154  }
155 
156  if (menubar_locales_installed.indexOf(locale) != -1) {
157  continue;
158  }
159 
160  var displayName = menubar_locales_bundle.getExtensionAttribute(i, "name");
161  if (!displayName) {
162  var parts = locale.split(/-/);
163 
164  if (parts.length > 1) {
165  displayName = SBFormattedString("locales.name.format.regional",
166  [SBString(parts[0], parts[0], langNames),
167  SBString(parts[1].toLowerCase(), parts[1], regNames)]);
168  } else {
169  displayName = SBFormattedString("locales.name.format",
170  [SBString(parts[0], parts[0], langNames)]);
171  }
172  }
173 
174  if (!separator) {
175  separator = document.createElement("menuseparator");
176  separator.setAttribute("class", className);
177  menu.appendChild(separator);
178  }
179 
180  var item = document.createElement("menuitem");
181  item.setAttribute("label", displayName);
182  item.setAttribute("name", "locale.switch");
183  item.setAttribute("type", "radio");
184  item.setAttribute("class", className);
185  item.setAttribute("oncommand", "installLocaleFromBundle(\"" + locale + "\", " + i + ")");
186 
187  elements.push(item);
188  }
189  elements.sort(sortLanguages);
190  for (var i =0;i<elements.length;i++) menu.appendChild(elements[i]);
191  }
192  }
193 
194 
201  function switchLocale(locale, wantmessagebox) {
202  try
203  {
204  var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
205  var curLocale = "en-US";
206  try {
207  curLocale = prefs.getCharPref("general.useragent.locale");
208  }
209  catch (e) { }
210 
211  if (locale != curLocale) {
212  prefs.setCharPref("general.useragent.locale", locale);
213  if (wantmessagebox) {
214  sbRestartBox_strings("message.localization", "message.needrestart", "Localization", "This setting will take effect after you restart Songbird");
215  }
216  }
217  }
218  catch ( err )
219  {
220  SB_LOG( "switchLocale - " + err);
221  }
222  }
223 
224  var _lastmenu = null;
225 
230  function fillLocaleList(menu) {
231  try
232  {
233  menubar_locales_installed = Array();
234  _lastmenu = menu;
235  var prefs = Components.classes["@mozilla.org/preferences-service;1"]
236  .getService(Components.interfaces.nsIPrefBranch);
237  var curLocale = "en-US";
238  try {
239  curLocale = prefs.getCharPref("general.useragent.locale");
240  }
241  catch (e) { }
242 
243  var cr = Components.classes["@mozilla.org/chrome/chrome-registry;1"]
244  .getService(Components.interfaces.nsIToolkitChromeRegistry);
245 
246  var sbs = Components.classes["@mozilla.org/intl/stringbundle;1"]
247  .getService(Components.interfaces.nsIStringBundleService);
248  var langNames = sbs.createBundle("chrome://global/locale/languageNames.properties");
249  var regNames = sbs.createBundle("chrome://global/locale/regionNames.properties");
250 
251  /* clear the existing children */
252  var children = menu.childNodes;
253  for (var i = children.length; i > 0; --i) {
254  menu.removeChild(children[i - 1]);
255  }
256 
257  var locales = cr.getLocalesForPackage("songbird");
258  var elements = Array();
259 
260  while (locales.hasMore()) {
261  var locale = locales.getNext();
262  menubar_locales_installed.push(locale);
263 
264  var parts = locale.split(/-/);
265 
266  var displayName;
267  try {
268  displayName = langNames.GetStringFromName(parts[0]);
269  if (parts.length > 1) {
270  try {
271  displayName += " (" + regNames.GetStringFromName(parts[1].toLowerCase()) + ")";
272  }
273  catch (e) {
274  displayName += " (" + parts[1] + ")";
275  }
276  }
277  }
278  catch (e) {
279  displayName = locale;
280  }
281 
282  var item = document.createElement("menuitem");
283  var className = menu.parentNode.getAttribute("class");
284 
285  item.setAttribute("label", displayName);
286  item.setAttribute("name", "locale.switch");
287  item.setAttribute("type", "radio");
288 
289  var itemclass = className;
290  var flag = getFlagFromBundle(locale);
291  if (flag && flag != "") {
292  itemclass = itemclass + " menuitem-iconic";
293  item.setAttribute("image", flag);
294  }
295  item.setAttribute("class", itemclass);
296 
297  if (curLocale == locale) {
298  item.setAttribute("checked", "true");
299  }
300  item.setAttribute("oncommand", "switchLocale(\"" + locale + "\", true)");
301 
302  elements.push(item);
303  }
304 
305  elements.sort(sortLanguages);
306  for (var i =0;i<elements.length;i++) menu.appendChild(elements[i]);
307  if (menubar_locales_bundle && menubar_locales_bundle.bundleDataStatus == Components.interfaces.sbIBundle.BUNDLE_DATA_STATUS_SUCCESS) menubarLoadBundledLocales(menu);
308  }
309  catch ( err )
310  {
311  SB_LOG( "fillLocaleList - " + err);
312  }
313  }
314 
321  function getFlagFromBundle(locale) {
322  for (var i=0; i < menubar_locales_bundle.bundleExtensionCount; i++) {
323  var name = menubar_locales_bundle.getExtensionAttribute(i, "name");
324  name = name.split(" ")[0]; // FIX ! name should not have a trailing " (Country)"
325  if (name == locale) return menubar_locales_bundle.getExtensionAttribute(i, "flag");
326  }
327  return null;
328  }
329 
334  function sortLanguages(a, b)
335  {
336  var aname = a.getAttribute("label");
337  var bname = b.getAttribute("label");
338  if (aname == bname) return 0;
339  if (aname < bname) return -1;
340  return 1;
341  }
342 
349  function installLocaleFromBundle(locale, bundleindex) {
350  if (gPrompt.confirm( window,
351  SBString( "locales.installconfirm.title", "Language Download" ),
352  SBString( "locales.installconfirm.msg", "This language is not installed, would you like to download and install it?" ) ) ) {
353  if (bundleindex != -1 && menubar_locales_bundle && bundleindex < menubar_locales_bundle.bundleExtensionCount) {
354  for (var i = 0; i < menubar_locales_bundle.bundleExtensionCount; i++) {
355  menubar_locales_bundle.setExtensionInstallFlag(i, i == bundleindex);
356  }
357  var res = menubar_locales_bundle.installFlaggedExtensions(window);
358 
359  if (res == menubar_locales_bundle.BUNDLE_INSTALL_SUCCESS) {
360  switchLocale(locale, true);
361  } else {
362  gPrompt.alert( window,
363  SBString( "locales.installfailed.title", "Language Download" ),
364  SBString( "locales.installfailed.msg", "Language installation failed, check your network connectivity!" ) );
365  }
366  }
367  }
368  }
369 
370 }
371 catch (err)
372 {
373  dump("switch_locales.js - " + err);
374 }
var Application
Definition: sbAboutDRM.js:37
function SBFormattedString(aKey, aParams, aDefault, aStringBundle)
sbOSDControlService prototype className
var menu
var menubar_locales_installed
Definition: switchLocale.js:63
sbOSDControlService prototype QueryInterface
function SB_LOG(scopeStr, msg)
Definition: windowUtils.js:244
function SBString(aKey, aDefault, aStringBundle)
Definition: StringUtils.jsm:93
let window
function installLocaleFromBundle(locale, bundleindex)
Install a Locale from the Locales Bundle.
var menubar_locales_bundle
Definition: switchLocale.js:41
function initLocalesBundle()
Initialize the Locales Bundle.
Definition: switchLocale.js:69
function sortLanguages(a, b)
Sort Locale language names.
var menubarLocalesBundleCB
Locales Bundle listener. Handles download completion and errors.
Definition: switchLocale.js:96
var bundle
function resetLocalesBundle()
Reset the Locales Bundle to an uninitialized state.
Definition: switchLocale.js:86
const MENUBAR_LOCALESBUNDLE_TIMEOUT
Definition: switchLocale.js:36
return null
Definition: FeedWriter.js:1143
var prefs
Definition: FeedWriter.js:1169
var gPrompt
Definition: windowUtils.js:64
var cr
function fillLocaleList(menu)
Fill the Locales menu with all available Locales.
function sbRestartBox_strings(titlestring, msgstring, defaulttitle, defaultmsg)
Display a restart message box using strings from the songbird.properties locale file.
Definition: restartBox.js:102
Songbird Bundle Interface This is the main bundle management interface, used to get the bundle data...
Definition: sbIBundle.idl:50
function getFlagFromBundle(locale)
Get the flag icon URL for a Locales from the Locales bundle.
_getSelectedPageStyle s i