webpack-contrib / i18n-webpack-plugin

[DEPRECATED] Embed localization into your bundle
MIT License
318 stars 74 forks source link

How can i change default lang in app? #61

Open dinfyru opened 7 years ago

dinfyru commented 7 years ago

How can i change default lang in app (setLocale or other)? webpack.config.js

const i18nEN = require('./src/config/lang/en.json');
const i18nRU = require('./src/config/lang/ru.json');

const languages = {
  en: i18nEN,
  ru: i18nRU
};

module.exports = {
...
plugins: [
    new I18nPlugin(languages.en),
    new I18nPlugin(languages.ru)
]
};

app.js __('login_form.emailField')

en.js

{
  "login_form.emailField": "Email",
}

ru.js

{
  "login_form.emailField": "Имэйл",
}
mightyaleksey commented 7 years ago

~~You should load necessary javascript bundle for the chosen language. The simple way is to make a html page for each locale and redirect to the necessary one via link.~~

You should create a configuration for each language, for example:

module.exports = [
  {
    ...
    plugins: [
      new I18nPlugin(languages.en)
    ],
    output: {
      ...
      filename: 'en.js'
    }
  },
  {
    ...
    plugins: [
      new I18nPlugin(languages.ru)
    ],
    output: {
      ...
      filename: 'ru.js'
    }
  }
];

It will produce a javascript bundles with corresponding translations (two for your example). And you'll have to load the necessary one depending on the chosen language.

There is a small example here: https://github.com/webpack/webpack/blob/master/examples/i18n/webpack.config.js