I have already mentioned a few times that we'll take a look at how to properly localize our extension. Even if the process is nothing new for experienced extension programmers, I think it should be part of this tutorial.
First of all, let's extend our directory structure. We'll need a new place where to put localized strings.
To let Firefox know about these files, we need to modify our chrome.manifest file and append following line to it.
locale helloworld en-US chrome/locale/en-US/
Localizing strings in XUL files
All strings in our helloWorld.xul are localized using helloWorld.dtd locale file. This DTD file contains list of entities with corresponding localized strings. In our extension we have to localize just a toolbar button. So, we need two following entities defined in it.
<!ENTITY helloworld.mybutton.label "Say Hello">
<!ENTITY helloworld.mybutton.tooltip "Push to say hello">
In order to use these entities within the helloWorld.xul file we have to create a reference to the locale file.
<!DOCTYPE helloworldDTD SYSTEM "chrome://helloworld/locale/helloWorld.dtd">
Sometimes you need to reference multiple DTDs from single XUL file. Here is an article that explains how to do it.
Now, we have to replace the strings by the entities that comes from the locale file. Following source code shows how the toolbar button definition looks after the replacement (see label and tooltiptext attributes).
var localeService =
var appLocale = localeService.GetApplicationLocale();
var stringBundleService =
var bundle = stringBundleService.CreateBundle(src, appLocale);
See chapter 11. Localization from Creating Application with Mozilla book for more information about string bundles.
The other way is to define the bundle in helloWorld.xul, using stringbundleset and stringbundle elements. This method is simpler and used in our extension.
function $HW_STRF(name, args)
Following source code shows how the $HW_STR method is used to localize Options menu items defined in the previous part.
If you need to put some dynamic values into the localized string use the $HW_STRF method. This method takes an extra parameter - array of arguments, which are substituted into the string in the bundle. See the following example how to use it.
Here is a localized string with placeholders where dynamic data should be inserted:
helloworld.formattedstring=Here is a value: %S and here is another: %S.
var localizedString = $HW_STRF("helloworld.formattedstring",
["value 1", "value 2"]);
If you are interested in more details how to properly localize Firefox extensions, read this article.
Example extension (helloworld-0.0.4.xpi) can be downloaded here.