mozilla

Revision 514765 of l10n

  • Revision slug: Mozilla/Add-ons/SDK/High-Level_APIs/l10n
  • Revision title: l10n
  • Revision id: 514765
  • Created:
  • Creator: olsh
  • Is current revision? No
  • Comment
Tags: 

Revision Content

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.

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.

Revision Source

<div class="note">
 <p>Stable</p>
</div>
<p><span class="seoSummary">Localize strings appearing in the add-on's JavaScript code.</span></p>
<h2 id="Usage">Usage</h2>
<p>To learn how to use this module to write localizable code, read the <a href="/en-US/Add-ons/SDK/Tutorials/l10n">Localization tutorial</a>.</p>
<p>Note that you can't currently use localize strings appearing in content scripts or HTML files.</p>
<h2 id="Globals">Globals</h2>
<h3 id="Functions">Functions</h3>
<pre class="addon-sdk-api-name" id="get(identifier.2C_count.2C_placeholder1...n)">
<code class="brush: js">get(identifier, count, placeholder1...n)</code></pre>
<p>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 <a href="http://en.wikipedia.org/wiki/.properties"><code>.properties</code></a> files stored in the add-ons "locale" directory.</p>
<p>The <a href="https://www.gnu.org/software/gettext/gettext.html">gettext</a> 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 "_":</p>
<pre class="brush: js">
var _ = require("sdk/l10n").get;</pre>
<p>Given a <code>.properties</code> file for the current locale containing an entry like:</p>
<pre>
hello_string= Hello!</pre>
<p>and the following code:</p>
<pre class="brush: js">
var _ = require("sdk/l10n").get;
console.log(_("hello_string"));</pre>
<p>the following output will be logged:</p>
<pre>
info: Hello!
</pre>
<p>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:</p>
<pre class="brush: js">
var _ = require("sdk/l10n").get;
console.log(_("Hello!"));</pre>
<p>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 <code>.properties</code> files.</p>
<p>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 <code>get()</code> takes a second integer parameter which indicates the number of items there are.</p>
<p>You can supply one or more placeholders to <code>get()</code>, which are strings, such as proper names, that should not be translated themselves but instead should be inserted into the translated string.</p>
<p>You can't use plurals and placeholders in the same expression: if you do, the placeholders will be ignored.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>identifier : string</strong><br />
 A identifier for the localization of a particular string in the current locale.</p>
<p><strong>count : integer</strong><br />
 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.</p>
<pre class="brush: js">
var _ = require("sdk/l10n").get;
console.log(_("child_id", 1));
console.log(_("child_id", 2));</pre>
<p>See the tutorial on <a href="/en-US/Add-ons/SDK/Tutorials/l10n#Plurals">plural support</a> for more information.</p>
<p>Note that if you use this parameter, you can't supply any placeholders.</p>
<p><strong>placeholder1...n : string</strong><br />
 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.</p>
<p>If you supply multiple placeholders, each one is a separate string parameter.</p>
<pre class="brush: js">
var _ = require("sdk/l10n").get;
console.log(_("home_town", "Alan", "Norwich"));</pre>
<p>See the tutorial on <a href="/en-US/Add-ons/SDK/Tutorials/l10n#Placeholders">placeholder support</a> for more information.</p>
<h5 id="Returns">Returns</h5>
<p><strong>string</strong> : The localized string referenced by the identifier parameter passed in, or the identifier itself if no referent for the identifier can be found.</p>
Revert to this revision