1

有没有办法在 javascript 中快速获取翻译后的字符串?

例如像这样: var s = L20n.get("hello");

我正在使用来自 npm 的 3.5。

以下不起作用:

document.l10n.ready.then(function(context) {
    document.l10n.formatValue('myItemKey').then(function(result){
        console.log(result);
    });
});

它导致打印项目键而不是翻译值。

4

1 回答 1

1

如果您使用的是 3.x 版,请使用formatValue

document.l10n.formatValue('hello').then(console.log);

您可以在这里阅读更多相关信息:https ://github.com/l20n/l20n.js/blob/v3.x/docs/view.md#viewformatvalueid-args 。 是页面加载时自动为您创建document.l10n的类的实例。View

该方法是异步的,因此您不必担心与尚未加载的语言文件相关的竞争条件。

于 2016-05-09T15:40:01.480 回答