我无法在l20n节点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导入语法,这实际上不是由节点应用的。我编辑了一下:
var Env = require('l20n').Env;
var fetchResource = require('l20n').fetchResource;
var env = new Env('ru', fetchResource);但是还有另一个问题:function resolveValues不存在。是否有人为l20n提供了实现良好的l20n片段?急需它
发布于 2016-02-25 21:39:24
这是个文档错误,很抱歉麻烦你了。Node支持是实验性的,而Env API是内部的,并且它没有对文档进行相应的更改。现在的文档是最新的
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']发布于 2016-02-28 00:26:39
创建了一个Node.js +聚合物+ L20n集成的“案例研究”教程。
https://stackoverflow.com/questions/35254302
复制相似问题