l10n

Stable

Localize strings appearing in the add-on's JavaScript code.

Usage

To learn how to use this module to write localizable code, read the Localization tutorial.

Note that you can't currently use localize strings appearing in content scripts or HTML files, but you can share the localized strings you want by assigning it's values to a JSON serializable object.

Globals

Functions

get(identifier, count, placeholder1...n)

This function takes a string parameter which it uses as an identifier to look up and return a localized string in the locale currently set for Firefox. Localized strings are supplied by the add-on developer in .properties files stored in the add-ons "locale" directory.

The gettext tools uses "_" for the name of the function that retrieves localized strings. For compatibility with tools that expect this syntax, you can assign this function to "_":

var _ = require("sdk/l10n").get;

Given a .properties file for the current locale containing an entry like:

hello_string= Hello!

and the following code:

var _ = require("sdk/l10n").get;
console.log(_("hello_string"));

the following output will be logged:

info: Hello!

If this function can't find the string referenced by the identifier parameter, it returns the identifier itself. This enables you to write functional, localizable code without localizing any strings - just make the identifiers the default language:

var _ = require("sdk/l10n").get;
console.log(_("Hello!"));

However, this will make it more difficult to maintain your code if you have many localizations, because any changes to the identifier values break all your .properties files.

If you're supplying different localizations for a string depending on the number of items (that is, whether to use a singular or plural form) then get() takes a second integer parameter which indicates the number of items there are.

You can supply one or more placeholders to get(), which are strings, such as proper names, that should not be translated themselves but instead should be inserted into the translated string.

You can't use plurals and placeholders in the same expression: if you do, the placeholders will be ignored.

Parameters

identifier : string
A identifier for the localization of a particular string in the current locale.

count : integer
Optional parameter. If you're supplying different localizations for a string for singular or plural forms, this parameter is the number of items there are in this case.

var _ = require("sdk/l10n").get;
console.log(_("child_id", 1));
console.log(_("child_id", 2));

See the tutorial on plural support for more information.

Note that if you use this parameter, you can't supply any placeholders.

placeholder1...n : string
Optional parameters. If you do not include the count parameter, you can supply one or more placeholder strings that are to be inserted into the translated string at locations defined by the translator.

If you supply multiple placeholders, each one is a separate string parameter.

var _ = require("sdk/l10n").get;
console.log(_("home_town", "Alan", "Norwich"));

See the tutorial on placeholder support for more information.

Returns

string : The localized string referenced by the identifier parameter passed in, or the identifier itself if no referent for the identifier can be found.