L10n.get

我们的志愿者还没有将这篇文章翻译为 中文 (简体)加入我们帮助完成翻译!
您也可以阅读此文章的English (US)版。

Deprecated
This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Avoid using it and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Non-standard
This feature is not on a current W3C standards track, but it is supported on the Firefox OS platform. Although implementations may change in the future and it is not supported widely across browsers, it is suitable for use in code dedicated to Firefox OS apps.

Deprecated 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 L10n.setAttributes().

This method has also potential to generate race conditions. Please the asynchronous L10n.formatValue() method or manually make sure that any code that uses this method is not fired before l10n resources are loaded using L10n.once() or 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 (synchronously!) 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

文档标签和贡献者