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 703817 of L10n.get

  • Revision slug: Web/API/L10n.get
  • Revision title: L10n.get
  • Revision id: 703817
  • Created:
  • Creator: stasm
  • Is current revision? No
  • Comment Mention theasync formatValue method

Revision Content

{{ non-standard_header() }}

Synchronous method for retrieving localization strings from app's resources. This function should be used only when direct manipulation on the strings is needed (etc. date/time formatting) and in all other UI related cases should be replaced with DOM attributes setting or {{ domxref("L10n.setAttributes()") }}.

This method has also potential to generate race conditions. Please the asynchronous {{ domxref("L10n.formatValue()") }} method or manually make sure that any code that uses this method is not fired before l10n resources are loaded using {{ domxref("L10n.once()") }} or {{ domxref("L10n.ready()") }}.

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.

Translations may use the double brace syntax (\{{ placeable }}) to introduce variables that will be resolved on runtime and interpolated into the final string return value. The variable values must be specified in the second argument to L20n.get() and must 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 to 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>
<p>Synchronous method for retrieving localization strings from app's resources. This function should be used only when direct manipulation on the strings is needed (etc. date/time formatting) and in all other UI related cases should be replaced with DOM attributes setting or {{ domxref("L10n.setAttributes()") }}.</p>
<p>This method has also potential to generate race conditions. Please the asynchronous {{ domxref("L10n.formatValue()") }} method or manually make sure that any code that uses this method is not fired before l10n resources are loaded using {{ domxref("L10n.once()") }} or {{ domxref("L10n.ready()") }}.</p>
<div class="summary">
 <p><span class="seoSummary">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.</span></p>
</div>
<p>Translations may use the double brace syntax (<code>\{{ placeable }}</code>) to introduce variables that will be resolved on runtime and interpolated into the final string return value. The variable values must be specified in the second argument to <code>L20n.get()</code> and must 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 to 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>
<section id="Quick_Links">
 <ol>
  <li data-closed="" data-default-state="open"><strong><a href="#">Guides</a></strong>
   <ol>
    <li><a href="/en-US/Apps/Build/Localization">Localization</a></li>
   </ol>
  </li>
  <li data-closed="" data-default-state="open"><strong><a href="#">Properties</a></strong>
   <ol>
    <li>{{ domxref("L10n.language.code") }}</li>
    <li>{{ domxref("L10n.language.direction") }}</li>
    <li>{{ domxref("L10n.readyState") }}</li>
   </ol>
  </li>
  <li data-closed="" data-default-state="open"><strong><a href="#">Methods</a></strong>
   <ol>
    <li>{{ domxref("L10n.get") }}</li>
    <li>{{ domxref("L10n.formatValue") }}</li>
    <li>{{ domxref("L10n.ready") }}</li>
    <li>{{ domxref("L10n.once") }}</li>
   </ol>
  </li>
 </ol>
</section>
Revert to this revision