2.6 KiB
globalization
Add language
Take Japanese as an example.
-
Add a language pack local file,
jais the Japanese language code, and a list of languages that support translation 有道智云, thesrc/locales/ja/messages.jsonfile will be generated after running the following command.npm run add-locale ja -
Extract the fields in the code that need to be translated, ie
<Trans>?message</Trans>,t`messagein themessagefield, run the following command aftersrc/locales/ja /messages.jsonwill appear after the extracted field configuration.npm run extractYou will see the following information:
Catalog statistics: ┌─────────────┬─────────────┬─────────┐ │ Language │ Total count │ Missing │ ├─────────────┼─────────────┼─────────┤ │ en (source) │ 52 │ - │ │ ja │ 52 │ 52 │ │ zh │ 52 │ 0 │ └─────────────┴─────────────┴─────────┘ -
At the same time, we have added the relevant configuration in
src/utils/config.js.{ ... i18n: { languages: [ ... { key:'ja', title: '日本語', flag: '/japanese.svg', }, ], }, }Routing related effects, after the configuration
npm run starttakes effect after restart. -
Use the built-in commands for automatic translation. You will see the translated configuration in
src/locales/ja/messages.json.npm run trans:onlyYou will see the following information:
start: en -> ja ... youdao: en -> ja: Unpublished -> 未発表 youdao: en -> ja: Update -> 更新 youdao: en -> ja: Update User -> ユーザーの更新 youdao: en -> ja: Username -> 名 ... All translations have been completed.npm run transwill executenpm run extractandnpm run trans:onlyin order. -
Finally, you can adjust the inaccurate fields in
src/locales/ja/messages.json. Start the development modenpm run start, open http://localhost:7000/ja/login and you will see the Japanese version of the app.