i18n properties message loader for webpack
When import / require i18n.properties
, import all other properties files in the same directory, and output as json.
Usage
npm i message-loader -D
message-loader
into webpack config.
Add module: rules: test: /i18n[a-zA-Z_]*\.properties$/ use: 'message-loader'
src/i18n/i18n.properties
default properties
hello = 你好
invoice.title = 发票抬头
app.name = 测试 App
src/i18n/i18n_en.properties
hello = hello
invoice.title = invoice title
src/i18n/i18n_fr.properties
fr = fr
src/index.js
; console; // should output following code/* { default: { hello: '你好', 'invoice.title': '发票抬头', 'app.name': '测试 App' }, en: { hello: 'hello', 'invoice.title': 'invoice title', 'app.name': '测试 App' }, fr: { hello: '你好', 'invoice.title': '发票抬头', 'app.name': '测试 App', fr: 'fr' }}*/
options
Override default options with following loader config.
test: /message?\.properties$/ use: loader: 'message-loader' options: filePattern: /message?\.properties$/ locales: 'en'
options.filePattern
default to: /i18n(?:_([a-zA-Z_]+))?\.properties$/
.
Means will search following files in the same directory, i18n.properties
, i18n_en.properties
, i18n_zh_CN.properties
, and so on.
when use file name to match pattern, the result at index 1
will be the locale.
options.locales
Should be an array with locale string, e.g. ['en', 'zh_CN']
.
Means only specific locale messages will be returned, Notice: default message will be return if exist.