<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

# Localization #

The SDK supports basic localization of strings appearing in your
main add-on's JavaScript code. It doesn't, yet, support localization
of HTML, CSS, or content scripts.

## Writing Localizable Code ##

To write localizable code, you do this:

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

<span class="aside">Assigning to "`_`" in particular is not required, but
is a convention from the
[gettext](https://www.gnu.org/software/gettext/gettext.html) tools
and will make it possible to work with existing tools that expect "`_`"
to indicate localizable strings.</span>

1. Import the `l10n` module, and assign its `get` function to
"`_`" (underscore).
2. Wrap all references to localizable strings with the `_()`
function.

That's a fully functional add-on as it stands. If you run it
you'll see the expected output:

<pre>
info: Hello!
</pre>

## Localizing Your Code ##

To localize the code, create a directory called "locale" under
your main add-on directory. In it, create one file for each locale
you need to support. The files:

* use the [`.properties` format](http://en.wikipedia.org/wiki/.properties)
* are named "xx-YY.properties", where "xx-YY" is the [name of the locale](https://wiki.mozilla.org/L10n:Locale_Codes) in question
* contain one entry for each string you want to localize.

For example, if you want to add a French localization for the
add-on above, you'd add a file called "fr-FR.properties" in the
"locale" directory, and add the following to it:

<pre>
Hello!= Bonjour!
</pre>

Now if you run the add-on with Firefox switched to the French
locale, you'll see:

<pre>
info: Bonjour!
</pre>

## Using Identifiers ##

If `l10n.get()` can't find a localization of a string using the current
locale, then it just returns the string you passed in. This is why the
example above displayed "Hello!" when there were no locale files present.

This has the nice property that you can write localizable, fully
functional code without having to write any locale files. You can just
use the default language in your code, and subsequently supply
`.properties` files for all the additional locales you want to support.

However, this approach makes it difficult to maintain an add-on which
has many localizations, because you're using the default language strings
both as user interface strings and as keys to look up your translations.
This means that if you want to change the wording of a string in the default
language, or fix a typo, then you break all your locale files.

To avoid this you can use identifiers in your code, and supply
localizations for all the languages you intend to support. For example,
in your main.js:

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

Then you can create `.properties` files for both `en-US` and `fr-FR`:

<pre>
# en-US translations
hello_string= Hello!
</pre>

<pre>
# fr-FR translations
hello_string= Bonjour!
</pre>

## Plurals ##

The `l10n` module supports plural forms. Different languages have
different rules for the formation of plurals. For example,
English has two forms: a singular form for "one", and a plural form
for "everything else, including zero":

<pre>
one tomato
no tomatoes
two tomatoes
</pre>

But Russian has different forms for numbers ending in 1 (except 11),
numbers ending in 2-4 (except 12-14) and other numbers:

<pre>
один помидор     // one tomato
два помидора     // two tomatoes
пять помидоров   // five tomatoes
</pre>

The SDK uses the [Unicode CLDR](http://cldr.unicode.org/index) data
to describe the different plural forms used by different languages.

### Unicode CLDR Plural Forms ###

The Unicode CLDR project defines a scheme for describing a particular
language's plural rules. In this scheme a language maps each distinct
range of numbers on to one of up to six forms, identified by the
following categories: *zero*, *one*, *two*, *few*, *many*, and *other*.

English has two forms, which can be described by mapping "1" to "one"
and "everything else" to "other":

<pre>
one   → n is 1;
other → everything else
</pre>

Russian uses four forms, that can be described as follows:

<pre>
one   → n mod 10 is 1 and n mod 100 is not 11;
few   → n mod 10 in 2..4 and n mod 100 not in 12..14;
many  → n mod 10 is 0 or n mod 10 in 5..9 or n mod 100 in 11..14;
other → everything else
</pre>

Plural rules for all languages can be found in the CLDR
[Language Plural Rules](http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html)
page (although this table is out of date compared to the
[CLDR XML source](http://unicode.org/repos/cldr/trunk/common/supplemental/plurals.xml)).

### Plural Forms in the SDK ###

In the code, you supply an extra parameter alongside the identifier,
describing how many items there are:

    var _ = require("l10n").get;
    console.log(_("tomato_id"));
    console.log(_("tomato_id", 1));
    console.log(_("tomato_id", 2));
    console.log(_("tomato_id", 5));
    console.log(_("tomato_id", .5));

In the `.properties` file for each language you can define a different
localization for each plural form possible in that language, using the
CLDR keywords. So in English we could have two plural localizations
(note that the "other" category does **not** take the CLDR keyword):

<pre>
# en-US translations
tomato_id[one]= %d tomato
tomato_id= %d tomatoes
</pre>

In Russian we could have four plural localizations:

<pre>
# ru-RU translations
tomato_id[one]= %d помидор
tomato_id[few]= %d помидора
tomato_id[many]= %d помидоров
tomato_id= %d помидоры
</pre>

The localization module itself understands the CLDR definitions for each
language, enabling it to map between, for example, "2" in the code and
"few" in the `ru-RU.properties` file. Then it retrieves and returns
the localization appropriate for the count you supplied.

## Placeholders ##

The `l10n` module supports placeholders, allowing you to
insert a string which should not be localized into one which is.
The following `en-US` and `fr-FR` `.properties` files include
placeholders:

<pre>
# en-US translations
hello_id= Hello %s!
</pre>

<pre>
# fr-FR translations
hello_id= Bonjour %s!
</pre>

To use placeholders, supply the placeholder string after the identifier:

    var _ = require("l10n").get;
    console.log(_("hello_id", "Bob"));
    console.log(_("hello_id", "Alice"));

In the `en-US` locale, this gives us:

<pre>
info: Hello Bob!
info: Hello Alice!
</pre>

In `fr-FR` we get:

<pre>
info: Bonjour Bob!
info: Bonjour Alice!
</pre>

## Ordering Placeholders ##

When a localizable string can take two or more placeholders, translators
can define the order in which placeholders are inserted, without affecting
the code.

Primarily, this is important because different languages have different
rules for word order. Even within the same language, though, translators
should have the freedom to define word order.

For example, suppose we want to include a localized string naming a
person's home town. There are two placeholders: the name of the person
and the name of the home town:

    var _ = require("l10n").get;
    console.log(_("home_town_id", "Bob", "London"));

An English translator might want to choose between the following:

<pre>
"&lt;town_name> is &lt;person_name>'s home town."
</pre>

<pre>
"&lt;person_name>'s home town is &lt;town_name>"
</pre>

To choose the first option, the `.properties` file can order the
placeholders as follows:

<pre>
home_town_id= %2s is %1s's home town.
</pre>

This gives us the following output:

<pre>
info: London is Bob's home town.
</pre>

## Locale Updater ##

The [locale updater](https://github.com/downloads/ochameau/locale-updater/locale-updater.xpi)
add-on makes it easier to update locale files. Once you've installed it,
open the Add-on Manager, and you'll see a see a new button labeled
"Update l10n" next to each add-on you've installed:

<img class="align-center" src="static-files/media/screenshots/locale-updater.png"
alt="Add-on manager with locale updater installed" />

Click the button and you'll be prompted for a new `.properties` file
for that add-on. If you provide a new file, the add-on's locale data
will be updated with the new file.

## <a name="limitations">Limitations</a> ##

The current localization support is a first step towards full support,
and contains a number of limitations.

* There's no support for content scripts, HTML files, or CSS files: at
the moment, you can only localize strings appearing in JavaScript files
that can `require()` SDK modules.

* The set of locale files is global across an add-on. This means that
a module isn't able to override a more general translation: so a module
`informal.js` can't specify that "hello_string" occurring in its code
should be localized to "Hi!".

* The SDK tools compile the locale files into a JSON format when
producing an XPI. This means that translators can't localize an add-on
given the XPI alone, but must be given access to the add-on source.

* The add-on developer must manually assemble the set of localizable
strings that make up the locale files. In a future release we'll add
a command to `cfx` that scans the add-on for localizable strings and
builds a template `.properties` file listing all the strings that need
to be translated.

