i18n (英语)

异形翻译引擎. mics Rails的i18n接口.
使用量
const I18n = require('@fiverr/i18n');
const translations = require('./translations.json');
const i18n = new I18n({translations});
| 选项 | 类型 | 说明 |
|---|---|---|
translations | 对象 | 翻译结构的代表性 一定是JSON服从 否则会被当成空物体对待 |
missing | 函数 | 缺少密钥时调用此函数 。 函数接受参数: 密钥、 范围、 翻译 对象 |
empty | 函数 | 当值为空时调用此函数 。 函数接受参数:密钥、值、范围、翻译 对象 |
$scope | 字符串 | 缩写前缀. 见 范围 基础是翻译密钥根 |
const i18n = new I18n({
translations: {...},
missing: key => logMissingKeyEvent({key: `missing_translation.${key.replace(/\W/g, '_')}`}),
empty: key => logEmptyValueEvent({key: `empty_translation.${key.replace(/\W/g, '_')}`}),
$scope: 'my_app.en'
});
翻译
i18n.t('my.key'); // I'm a sentence
寻找替代品
i18n.t(['my.missing.key', 'my.key']); // I'm a sentence
处理缺失
默认情况下, 缺少的密钥或空值返回密钥的最后一部分
i18n.t('this.is.a.missing_key'); // missing key
但是,从“ 丢失” 和“ 空” 调回返回一个真实值, 将允许自定义回落
const i18n = new I18n({
translations: {...},
empty: (key, value, scope) => {
if (scope.startsWith('en-US')) {
return; // default fallback
}
return i18n.t(key, { $scope: 'en-US' }); // Try English
},
$scope: 'en-US'
});
即时处理后添加更多翻译
i18n.add({yet: {another: {key: 'I\'m here, too!'}}});
Use:
i18n.translate('yet.another.key'); // I'm here, too!
Or:
i18n.t('yet.another.key'); // I'm here, too!
特征
带有数据的内插
const i18n = new I18n({
translations: {
my: { string: 'a dynamic %{thing} in a static string' }
}
});
i18n.t('my.string', {thing: 'value'}); // a dynamic value in a static string
一个/另一个
const i18n = new I18n({
translations: {
it_will_take_me_days: {
one: 'It\'ll take me one day',
other: 'It\'ll take me %{count} days'
}
}
});
i18n.t('it_will_take_me_days', {count: 1}); // It'll take me one day
i18n.t('it_will_take_me_days', {count: 3}); // It'll take me 3 days
i18n.t('it_will_take_me_days', {count: 'a lot of'}); // It'll take me a lot of days
有范围的案例
优先级 :
- 发现结果,范围已过(如适用)
- 确定实例范围的结果(如适用)
- 发现结果w/o 范围
// Global scope setup
const i18n = new I18n({
translations: {
key: 'Top',
child: {
key: 'Child'
},
something: {
key: 'Something'
}
}
});
const child = i18n.spawn('child');
i18n.t('key'); // Top
child.t('key'); // Child
child.t('key', { $scope: 'something' }); // Something
范围较大的儿童案件
这对应用程序的封闭部分来说是一个很好的简称选择.
译名商店是共享的,这样如果在命名空间前加了前缀,则父母可以找到密钥,而孩子不需要.
孩子还可以找到"全球"译名(一个在它的命名空间之外)
const usersI18n = i18n.spawn('users.get');
// Add translations under the scope
usersI18n.add({introduction: 'Hi, my name is %{username}'});
// Use translations
usersI18n.t('introduction', {username: 'Martin'}); // Hi, my name is Martin
i18n.t('users.get.introduction', {username: 'Martin'}); // Hi, my name is Martin
实例
显示一个 i18n 的空实例
const i18n = require('@fiverr/i18n/instance');
i18n.add({...});
特别用作外部网络包装
externals: {
'@fiverr/i18n/instance': 'i18n'
}
名称可以替换 :
import phraser from '@fiverr/i18n/instance';externals: { '@fiverr/i18n/instance': 'phraser' }
单子(i18n)
确定你在全球范围内只有一次I18n
const i18n = I18n.singleton;
// Optional:
i18n.$scope = 'my.scope';
i18n.onmiss((key, scope) => console.error(`Missing key "${key}" ${scope ? `In scope: "${scope}"`}`));
i18n.onempty((key, value, scope) => console.warn(`Empty value "${value}" for key "${key}" ${scope ? `In scope: "${scope}"`}`));
i18n.add({...});
快捷键 :
const i18n = require('@fiverr/i18n/singleton');
或者简单
require('@fiverr/i18n/singleton');
// i18n is defined globally
辅助功能
检查是否有可用的密钥
已经
i18n.has('key');
i18n.has(['key', 'other_key']);
i18n.has('namespace.key');
i18n.has('key', { $scope: 'namespace' });