Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Method for using Google Chrome JSON locale files in Firefox extensions.

View gist:959868
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
var MY_EXTENSION_STRINGS = {
"strings" : {},
 
get : function (key, substitutions) {
if (key in this.strings) {
var bundle = this.strings[key];
 
var message = this.strings[key].message;
 
if ("placeholders" in bundle) {
for (var i in bundle.placeholders) {
var regex = new RegExp("\\$" + i + "\\$", "g");
message = message.replace(regex, bundle.placeholders[i].content);
}
}
 
if (typeof substitutions != 'undefined') {
if (typeof substitutions != 'object') {
substitutions = [ substitutions ];
}
}
 
if (substitutions) {
for (var i = 0, _len = substitutions.length; i < _len; i++) {
var regex = new RegExp("\\$" + (i+1), "g");
message = message.replace(regex, substitutions[i]);
}
}
 
return message;
}
 
return "";
}
};
 
(function (extension_namespace, string_object) {
var localeOrder = ["en-US"];
 
// Get the user's Firefox locale.
var chromeRegService = Components.classes["@mozilla.org/chrome/chrome-registry;1"].getService();
var xulChromeReg = chromeRegService.QueryInterface(Components.interfaces.nsIXULChromeRegistry);
// The "official" locale, especially on Linux.
var browserLocale = xulChromeReg.getSelectedLocale("global");
 
if (browserLocale != localeOrder[0]) {
localeOrder.push(browserLocale);
}
 
// The user-specified locale from prefs.
var userLocale = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("general.useragent.").getCharPref("locale");
 
if (userLocale != localeOrder[localeOrder.length - 1]) {
localeOrder.push(userLocale);
}
 
var finalLocaleOrder = [];
 
// Convert the locale codes to Chrome style.
for (var i = 0, _len = localeOrder.length; i < _len; i++) {
var localeParts = localeOrder[i].split("-");
localeParts[0] = localeParts[0].toLowerCase();
 
if (localeParts.length > 1) {
localeParts[1] = localeParts[1].toUpperCase();
 
// e.g., If the locale code is pt_BR, use pt as a backup.
if (finalLocaleOrder.length == 0 || finalLocaleOrder[finalLocaleOrder.length - 1] != localeParts[0]) {
finalLocaleOrder.push(localeParts[0]);
}
}
 
var locale = localeParts.join("_");
 
if (finalLocaleOrder.length == 0 || finalLocaleOrder[finalLocaleOrder.length - 1] != locale) {
finalLocaleOrder.push(locale);
}
}
 
function readNextLocale() {
if (finalLocaleOrder.length > 0) {
var locale = finalLocaleOrder.shift();
 
var req = new XMLHttpRequest();
req.open("GET", "chrome://" + extension_namespace + "/content/_locales/" + locale + "/messages.json", true);
req.overrideMimeType("text/plain;charset=UTF-8");
 
req.onload = function () {
var messagesText = req.responseText;
 
try {
var messages = JSON.parse(messagesText);
} catch (e) {
// Invalid JSON.
var messages = {};
}
 
for (var i in messages) {
string_object[i] = messages[i];
}
 
readNextLocale();
};
 
req.onerror = function () {
readNextLocale();
};
 
try {
req.send(null);
} catch (e) {
// Most likely the file doesn't exist.
readNextLocale();
}
}
else {
// At this point, you can run any localization functions now that
// all of the locale files have been read.
}
}
 
readNextLocale();
})(MY_EXTENSION_NAMESPACE, MY_EXTENSION_STRINGS);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.