Node.js i18n 国际化实现语言切换

安装
npm install i18n --save


i18n.js

var i18n = require('i18n');

i18n.configure({
  locales: ['en-US', 'zh-CN'],  // setup some locales - other locales default to en_US silently
  // defaultLocale: 'zh-CN',
  defaultLocale: 'en-US',
  directory: __dirname + '/locales',  
  updateFiles: false,
  indent: "\t",
  extension: '.json'
});

module.exports = function (app) {
  // default: using 'accept-language' header to guess language settings
  app.use(i18n.init);
};

zh-CN.js

{
  "Hello":"你好,世界!"
}

en-US.js

{
  "Hello":"Hello, world!"
}

app.js

[...]
require('./i18n.js')(app);
[...]


test.controller.js

console.log("test...", i18n.__('Hello'));


参考:
https://www.npmjs.com/package/i18n
https://github.com/mashpie/i18n-node

声明:本站所有文章和图片,如无特殊说明,均为原创发布,转载请注明出处。
随机推荐
Node.js http 模块
WordPress 添加 Favicon 图标的方法
Web 自定义组件
Land 主题
JavaScript 字符串详细介绍
阿里云内容安全 API 签名机制
Node.js 内置模块
Notifications API