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.
firstname.lastname@example.org/ chrome/ content/ helloworld/ helloWorld.xul helloWorld.js locale/ en-US/ helloWorld.dtd helloWorld.properties defaults/ preferences/ prefs.js chrome.manifest install.rdf
To let Firefox know about these files, we need to modify our chrome.manifest file and append following line to it.
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.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.
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).
helloworld.option1=Option1 helloworld.option2=Option2 helloworld.paneltitle=Hello World! helloworld.message=Hello World!
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.
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:
["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.