Webpack i18n extraction plugin
extract i18n text into json from webpack bundles.
install
npm i -D i18n-plugin
usage
webpack.config.js
const I18nExtract = ;...plugins: ... locale: 'zh-cn' localeFilePath: path pattern: /__locale\(\)/g ......
options
locale
target locale to extract, e.g.zh-cn
- type:
string
- defaut:
en-us
- type:
pattern
pattern for text searching- type:
RegExp
- defaut:
/__locale\((.+?)\)/g
- type:
locale
target locale to extract- type:
string
- defaut:
en-us
- type:
open
open the generated locale file when finish build- type:
bool
- defaut:
false
- type:
how does it work
First, the plugin will read locale files from the localeFilePath
and load all existing translations. Create one if there's no such file exists.
The plugin will replace all matches with existing translation content for specific locale. Extract into the json file if there's no translation present.
The point is: code the i18n text as normal text within your code. No need to wrap into a function call like webpack-contrib/i18n-webpack-plugin did. For that sake we can 'i18n' any where other than js.
example
in your code:
Component { return <p className="square"> </p> ; }
en-us
is the default locale, so your bundle with this plugin will generate the following output at initial.
en-us.json
Component { return <p className="square"> Hello World! </p> ; }
$ webpack --env.locale=zh-cn
the output:
zh-cn.json
Translate the new locale file and rebuild to get the localized bundle.
zh-cn.json
Component { return <p className="square"> 中国制造,惠及全球 </p> ; }
TODOs
- unescape for chinese characters in jsx
- filter files to minimize the match process
- buffer file test