Take Japanese as an example.
-
Add a language pack local file,
ja
is the Japanese language code, and a list of languages that support translation 有道智云, thesrc/locales/ja/messages.json
file 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>
,i18n.t`message
in themessage
field, run the following command aftersrc/locales/ja /messages.json
will appear after the extracted field configuration.npm run extract
You 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 start
takes 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:only
You 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 trans
will executenpm run extract
andnpm run trans:only
in 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.