Browser Add-ons, Google Chrome, Mozilla, Mozilla Firefox, Programming

How to use Google Chrome extension locales in Firefox 4 extensions

Update: Don’t use this code. Use this new version.

A few months ago, I posted a snippet of code that provides an API for using locale files from Google Chrome extensions in extensions for Firefox 3.6 and older. I’ve now added support for Firefox 4, and the updated code is shown below:

View the code at GitHub.

The same usage rules still apply:

  • Replace “MY_EXTENSION_ID” with the ID of your extension.
  • Rename “MY_EXTENSION_STRINGS” to something that won’t interfere with another extension.
  • The _locales directory from your Chrome extension should be in the chrome/content/ directory of your Firefox extension (or update my code to point to wherever you put it).
  • Include the excellent io.js library in your extension.
  • NEW: For Firefox 4, you’ll need to specify <em:unpack>true</em:unpack> in your install.rdf.

I’ve been using this solution in ScribeFire in Firefox 3.5, 3.6, and 4.0 (and 5 and 6) for a while now with no complaints. Let me know if you implement this in your extension, and I’d love any feedback you have on the code or its performance.

Standard

2 comments on “How to use Google Chrome extension locales in Firefox 4 extensions

  1. For Firefox 4, you’ll need to specify <em:unpack>true</em:unpack> in your install.rdf.

    This solution isn’t really recommendable then. Two suggestions:

    1) I realized at some point that it is easier to work with chrome URLs than to deal with two extension manager APIs. In this case you don’t even need to resolve the URLs, simply use XMLHttpRequest to read from chrome://:

    var request = new XMLHttpRequest();
    request.open(“GET”, “chrome://” + MY_NAMESPACE + “/content/_locales/” + locale + “/messages.json”);
    request.onload = function()
    {
    var messageText = request.responseText;

    }
    request.onerror = function()
    {
    // File doesn’t exist

    }
    request.send(null);

    2) Reading the locale from the prefs will fail on Linux because intl.locale.matchOS pref will be set. Which is why I use nsIXULChromeRegistry.getSelectedLocale(“global”) instead. This has its own disadvantages of course, your extension will never switch to a language that the browser itself doesn’t provide. Not sure how much this is of concern to you.

Leave a Reply

Your email address will not be published. Required fields are marked *