我无法从 l20n-node Github 页面运行示例代码而不出现错误。
import { Env, fetchResource } from 'l20n';
const env = new Env('en-US', fetchResource);
const ctx = env.createContext(['locales/{locale}.l20n']);
const langs = [
{code: 'es-ES'},
{code: 'en-US'}
];
ctx.resolveValues(langs, ['foo', 'bar']).then(
([foo, bar]) => console.log(foo, bar));
首先它使用 ES6 导入语法,但 Node 实际上并未应用该语法。 我稍微编辑了一下:
var Env = require('l20n').Env;
var fetchResource = require('l20n').fetchResource;
var env = new Env('ru', fetchResource);
但是还有另一个问题:函数resolveValues
不存在。
有人有针对 l20n 的、实现良好的 node.js 片段吗?非常需要
最佳答案
这是一个文档错误,很抱歉给您带来麻烦。 Node 支持是实验性的,Env
API 是内部的,它发生了变化,但没有对文档进行相应的更改。该文档现在为 up-to-date :
const L20n = require('l20n');
const langs = [
{code: 'es-ES'},
{code: 'en-US'}
];
// fetchResource is node-specific, Env isn't
const env = new L20n.Env(L20n.fetchResource);
// helpful for debugging
env.addEventListener('*', e => console.log(e));
// contexts are immutable; if langs change a new context must be created
const ctx = env.createContext(langs, ['./locales/{locale}.l20n']);
// pass string ids or tuples of [id, args]
ctx.formatValues('foo', ['bar', {baz: 'Baz'}]).then(values => {
// values is an array of resolved translations
console.log(values);
});
// -> ['Foo en español', 'Bar only exists in English']
关于javascript - l20n:服务器端 Node resolveValues错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35254302/