Infocatcher / Custom_Buttons

Buttons for Custom Buttons extension for Firefox (and other Gecko-based applications)
38 stars 11 forks source link

Localizations may not work on Linux #20

Closed Infocatcher closed 2 years ago

Infocatcher commented 11 years ago

See https://forum.mozilla-russia.org/viewtopic.php?id=54385

So we should use

Components.classes["@mozilla.org/chrome/chrome-registry;1"]
    .getService(Components.interfaces.nsIXULChromeRegistry)
    .getSelectedLocale("global");

or

Components.classes["@mozilla.org/preferences-service;1"]
    .getService(Components.interfaces.nsIPrefBranch)
    .getComplexValue("general.useragent.locale", Components.interfaces.nsIPrefLocalizedString)
    .data;

But with getSelectedLocale() we need to have installed language pack to test localization (or change code each time).

Infocatcher commented 11 years ago

getComplexValue() seems to work fine on Linux, but doesn't work for default value on Windows:

Exception: Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED) [nsIPrefBranch.getComplexValue]

getCharPref(): always works on Windows, but returns chrome://global/locale/intl.properties on Linux for default value.

getSelectedLocale(): seems to always works, but only if installed corresponding language pack for browser.

Also with intl.locale.matchOS = true general.useragent.locale isn't used at all.

Infocatcher commented 11 years ago

D'oh...

(function() {
    //var prefs = Services.prefs; 
    var prefs = Components.classes["@mozilla.org/preferences-service;1"]
        .getService(Components.interfaces.nsIPrefBranch);
    if(!prefs.getBoolPref("intl.locale.matchOS")) {
        var locale = prefs.getCharPref("general.useragent.locale");
        if(locale.substr(0, 9) != "chrome://")
            return locale;
    }
    return Components.classes["@mozilla.org/chrome/chrome-registry;1"]
        .getService(Components.interfaces.nsIXULChromeRegistry)
        .getSelectedLocale("global");
})().match(/^[a-z]*/)[0];
Infocatcher commented 11 years ago

Also for OS locale:

Components.classes["@mozilla.org/intl/nslocaleservice;1"]
    .getService(Components.interfaces.nsILocaleService)
    .getSystemLocale()
    .getCategory("NSILOCALE_COLLATE");

https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsILocaleService https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsILocale

Infocatcher commented 11 years ago

Alternative way:

Services.urlFormatter.formatURL("%LOCALE%")

https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIURLFormatter http://mxr.mozilla.org/mozilla-central/source/toolkit/components/urlformatter/nsURLFormatter.js#90

  _defaults: {
    LOCALE:           function() Cc["@mozilla.org/chrome/chrome-registry;1"].
                                 getService(Ci.nsIXULChromeRegistry).
                                 getSelectedLocale('global'),