Created
December 10, 2010 18:05
-
-
Save cfinke/736538 to your computer and use it in GitHub Desktop.
A method for using Google Chrome-style locales in Firefox extensions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ""; | |
} | |
}; | |
if ("@mozilla.org/extensions/manager;1" in Components.classes) { | |
// Pre-Firefox 4 | |
(function (extension_id, string_object) { | |
var extensionManager = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager); | |
var installLocation = extensionManager.getInstallLocation(extension_id); | |
try { | |
var defaultLocale = JSON.parse(FileIO.read(installLocation.getItemFile(extension_id, "chrome/content/manifest.json"))).default_locale; | |
} catch (e) { | |
var defaultLocale = "en_US"; | |
} | |
// Get the user's Firefox locale. | |
var userLocale = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("general.useragent.").getCharPref("locale"); | |
// Convert the userLocale to Chrome style. | |
var userLocaleParts = userLocale.split("-"); | |
if (userLocaleParts.length > 1) userLocaleParts[1] = userLocaleParts[1].toUpperCase(); | |
userLocale = userLocaleParts.join("_"); | |
var localeOrder = [defaultLocale]; | |
if (userLocale != defaultLocale) { | |
if ((underscoreIndex = userLocale.indexOf("_")) != -1) { | |
localeOrder.push(userLocale.substr(0, underscoreIndex)); | |
} | |
localeOrder.push(userLocale); | |
} | |
// Starting with the default, pull in all of the strings for each locale, | |
// overwriting previous ones if necessary. | |
localeOrder.forEach(function (locale) { | |
var messagesFile = installLocation.getItemFile(extension_id, "chrome/content/_locales/" + locale + "/messages.json"); | |
var messagesText = FileIO.read(messagesFile); | |
if (messagesText) { | |
var messages = {}; | |
try { | |
messages = JSON.parse(messagesText); | |
} catch (e) { | |
// Invalid JSON. | |
} | |
for (var i in messages) { | |
string_object[i] = messages[i]; | |
} | |
} | |
}); | |
})(MY_EXTENSION_ID, MY_EXTENSION_STRINGS.strings); | |
} | |
else { | |
// Post-Firefox 4 | |
// Make sure that you've specified <em:unpack>true</em:unpack> in your install.rdf | |
(function (extension_id, string_object) { | |
var defaultLocale = "en_US"; | |
Components.utils["import"]("resource://gre/modules/AddonManager.jsm"); | |
AddonManager.getAddonByID(extension_id, function (addon) { | |
var addonLocation = addon.getResourceURI("").QueryInterface(Components.interfaces.nsIFileURL).file; | |
// Get the user's Firefox locale. | |
var userLocale = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("general.useragent.").getCharPref("locale"); | |
// Convert the userLocale to Chrome style. | |
var userLocaleParts = userLocale.split("-"); | |
if (userLocaleParts.length > 1) userLocaleParts[1] = userLocaleParts[1].toUpperCase(); | |
userLocale = userLocaleParts.join("_"); | |
var localeOrder = [defaultLocale]; | |
if (userLocale != defaultLocale) { | |
if ((underscoreIndex = userLocale.indexOf("_")) != -1) { | |
localeOrder.push(userLocale.substr(0, underscoreIndex)); | |
} | |
localeOrder.push(userLocale); | |
} | |
// Starting with the default, pull in all of the strings for each locale, | |
// overwriting previous ones if necessary. | |
localeOrder.forEach(function (locale) { | |
var messagesFile = addon.getResourceURI("chrome/content/_locales/" + locale + "/messages.json").QueryInterface(Components.interfaces.nsIFileURL).file; | |
var messagesText = FileIO.read(messagesFile, "utf-8"); | |
if (messagesText) { | |
var messages = {}; | |
try { | |
messages = JSON.parse(messagesText); | |
} catch (e) { | |
// Invalid JSON. | |
} | |
for (var i in messages) { | |
string_object[i] = messages[i]; | |
} | |
} | |
}); | |
// Because this process is asynchronous in Firefox 4, you'll want to re-run | |
// any localization scripts now that you run on document load, since the document may | |
// have finished loading before this function ran, and if it did, all of your | |
// locale strings would have been empty during the first call to your localizing | |
// function. | |
}); | |
})(MY_EXTENSION_ID, MY_EXTENSION_STRINGS.strings); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment