2016-02-07 95 views
1

我無法從l20n-node Github頁面運行沒有錯誤的示例代碼。l20n:服務器端節點resolveValues錯誤

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 node.js片段?需要它很差

回答

1

這是一個文檔錯誤,我很抱歉的麻煩。節點支持是實驗性的,並且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']