我已经成功实现了i18next,顺便说一句,它是一个很棒的库!虽然我仍在寻找"最佳实践".这是我现在的设置,一般来说我喜欢:
var userLanguage = 'en'; // set at runtime
i18n.init({
lng : userLanguage,
shortcutFunction : 'defaultValue',
fallbackLng : false,
load : 'unspecific',
resGetPath : 'locales/__lng__/__ns__.json'
});
在DOM中我做这样的事情:
My first example
在JS中我做这样的事情:
return i18n.t('demo.mySecondExample', 'My second example');
这意味着我在代码本身内维护英文翻译.但是我translation.json
使用i18next-parser 使用单独的文件维护其他语言:
gulp.task('i18next', function()
{
gulp.src('app/**')
.pipe(i18next({
locales : ['nl','de'],
output : '../locales'
}))
.pipe(gulp.dest('locales'));
});
这一切都很棒.唯一的问题是,当我设置'en'
为userLanguage
,i18next
坚持提取/locales/en/translation.json
文件,即使它不包含任何翻译.为了防止404,我当前{}
在该文件中提供了一个空的json对象.
有没有办法防止加载空的.json文件?