Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 566377 of L10n.get

  • Revision slug: Web/API/L10n.get
  • Revision title: L10n.get
  • Revision id: 566377
  • Created:
  • Creator: stasm
  • Is current revision? No
  • Comment

Revision Content

{{ non-standard_header() }}

Summary

The get method is used to retrieve translations from the localization resources, optionally interpolating them with additional variable data. If the translation is not found in the first supported locale, the L10n context will try the next locale in the fallback chain until it finds an available translation.

The translations may use the double brace syntax (\{{ placeable }}) to introduce variables which will be resolved on runtime and interpolated into the final string return value. The values of the variables must be specified in the second argument to L20n.get() and myust be strings or numbers. If a variable fails to resolve, the final translation will contain the raw \{{ placeable }} syntax.

When no translation is found, or, in rare cases, when the translation contains unrecoverable errors (e.g. a cyclic reference in a placeable), L10n.get() will return an empty string.  This behavior allows the developers to safely assume that the return value is always a string.

Syntax

var translation = navigator.mozL10n.get(identifier[, data]);

Parameters

identifier
The string identifier of the translation to be retrieved.
data
An object with variables which will be interpolated into the translation. All members' values must be strings or numbers.

Returns

A translated string (in one of the supported locales) or an empty string if the translation was not found or contained errors.

Example

dialer.en-US.properties:
from-contact = Missed call from \{{ contact }}
dialer.js:
var _ = navigator.mozL10n.get;
var body = _('from-contact', {contact: primaryInfo.toString()});

Specification

Not part of any specification.

See also

  • {{domxref("L10n")}}

Revision Source

<p>{{ non-standard_header() }}</p>
<h2 id="Summary">Summary</h2>
<p>The <code>get</code> method is used to retrieve translations from the localization resources, optionally interpolating them with additional variable data. If the translation is not found in the first supported locale, the <a href="/en-US/docs/Web/API/L10n">L10n</a> context will try the next locale in the fallback chain until it finds an available translation.</p>
<p>The translations may use the double brace syntax (<code>\{{ placeable }}</code>) to introduce variables which will be resolved on runtime and interpolated into the final string return value. The values of the variables must be specified in the second argument to <code>L20n.get()</code> and myust be strings or numbers. If a variable fails to resolve, the final translation will contain the raw <code>\{{ placeable }}</code> syntax.</p>
<p>When no translation is found, or, in rare cases, when the translation contains unrecoverable errors (e.g. a cyclic reference in a placeable), <code>L10n.get()</code> will return an empty string.&nbsp; This behavior allows the developers to safely assume that the return value is always a string.</p>
<h2 id="Syntax">Syntax</h2>
<pre>
var translation = navigator.mozL10n.get(<em>identifier[</em>, <em>data]</em>);</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
 <dt>
  <code>identifier</code></dt>
 <dd>
  The string identifier of the translation to be retrieved.</dd>
 <dt>
  <code>data</code></dt>
 <dd>
  An object with variables which will be interpolated into the translation. All members' values must be strings or numbers.</dd>
</dl>
<h3 id="Returns">Returns</h3>
<p>A translated string (in one of the supported locales) or an empty string if the translation was not found or contained errors.</p>
<h2 id="Example">Example</h2>
<dl>
 <dt>
  dialer.en-US.properties:</dt>
</dl>
<dl>
 <dd>
  <pre>
from-contact = Missed call from \{{ contact }}</pre>
 </dd>
</dl>
<dl>
 <dt>
  dialer.js:</dt>
</dl>
<dl>
 <dd>
  <pre class="brush: js">
var _ = navigator.mozL10n.get;
var body = _('from-contact', {contact: primaryInfo.toString()});
</pre>
 </dd>
</dl>
<h2 id="Specification" name="Specification">Specification</h2>
<p>Not part of any specification.</p>
<h2 id="See_also">See also</h2>
<ul>
 <li>{{domxref("L10n")}}</li>
</ul>
Revert to this revision