javascript - l20n:服务器端 Node resolveValues错误

标签 javascript node.js l20n

我无法从 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/

相关文章:

javascript - codeigniter 中的依赖下拉列表

javascript - DIV 中文本的垂直滚动(循环)

javascript - Firefox 无法与位于 ws ://localhost:8080/的服务器建立连接

node.js - gulp 时加载外部模块@babel/register 失败

localization - L20n 翻译文本区域和输入字段中的占位符/属性

javascript - 以编程方式触发点击处理程序

javascript - 每次点击后元素 ID 加一?

node.js - 如何实现 SailsJS + Phonegap/Cordova 应用程序

javascript - 如何在 l20n.js 中使用 html

javascript - l20n.js : Uncaught (in promise) TypeError: element. querySelectorAll 不是一个函数(…)