Internationalization plug-in, used to solve i18n problem.
Configure locale
to enable.
Contains the following functions,
For example, in the following directory, the project has the internationalized language switch between zh-CN
and en-US
:
+ src+ locales-zh-CN.ts-en-US.ts+ pages
Naming convention for multi-language files: <lang><separator (configured by baseSeparator)><COUNTRY>.js
The content specification of multilingual files: key-value literal, as follows:
// src/locales/zh-CN.jsexport default {WELCOME_TO_UMI_WORLD: '{name}, welcome to the world of umi',};
// src/locales/en-US.jsexport default {WELCOME_TO_UMI_WORLD: "{name}, welcome to umi's world",};
If the project is configured with
singular: true
,locales
should be changed tolocale
@umijs/plugin-locale is based on the react-intl package and supports all its apis. For details, please see here. In order to facilitate the use, we also added some other functions, here will list all the api, and show its functions.
Add languages dynamically. After adding languages, you can get the list through getAllLocales. Three parameters of addLocale.
name
language. E.g. zh-TWmessage
language. For example:{ name:'Hello, {name}',}
momentLocale
and antd
configurationimport zhTW from 'antd/es/locale/zh_TW';// Dynamically add new languagesaddLocale('zh-TW',{// id listname: 'Hello, {name}',},{momentLocale: 'zh-tw',antd: zhTW,},);
Get a list of all internationalized files currently obtained. By default, it will look for files similar to en-US.(js|json|ts)
in the locales
folder.
import { getAllLocales } from 'umi';console.log(getAllLocales()); // [en-US,zh-CN,...]
getLocale
will get the currently selected language.
import { getLocale } from 'umi';console.log(getLocale()); // en-US | zh-CN
UseIntl
is the most commonly used api, and it can obtain apis such as formatMessage
for specific value binding.
// en-US.jsonexport default {name: 'Hi, {name}',};
//page/index.tsximport React, { useState } from 'react';import { useIntl } from 'umi';export default function () {const intl = useIntl();return (<button type="primary">{intl.formatMessage({id: 'name',defaultMessage: 'Hello, traveler',},{name: 'Traveler',},)}</button>);}
Set the switching language, the page will be refreshed by default. You can achieve dynamic switching without refresh by setting the second parameter to false
.
import { setLocale } from 'umi';// refresh pagesetLocale('zh-TW', true);// Do not refresh the pagesetLocale('zh-TW', false);
object
Directory convention:
After enabling locale: {}
, the default configuration is as follows:
export default {locale: {default: 'zh-CN',antd: false,title: false,baseNavigator: true,baseSeparator: '-',},};
string
-
The separator between country (lang) and language (language).
By default, it is -
, and the returned language and catalog files are zh-CN
, en-US
,sk
, etc.
string
-Default: zh-CN
Default language, when no specific language is detected, the language specified in default
is displayed.
If
baseNavigator
is specified as_
,default
defaults tozh_CN
.
boolean
After opening, support antd internationalization.
boolean
Title internationalization.
The title
configured in the project and the title
in the route can directly use the internationalization key and automatically be converted into the corresponding language copy, for example:
Under the locales
directory:
// src/locales/zh-CN.jsexport default {'site.title': 'Site-Title','about.title': 'About-Title',};// src/locales/en-US.jsexport default {'site.title': 'English Title','about.title': 'About-Title',};
The project configuration is as follows:
// .umirc.jsexport default {title: 'site.title',routes: [{path: '/',component: 'Index',},{path: '/about',component: 'About',title: 'about.title',},],};
When visiting the page:
/
route, title is site-title
in Chinese, English Title
in English/about
route, the title is About-Title
in Chinese, and About Title
in Englishboolean
Turn on browser language detection.
By default, the current locale is recognized as follows: umi_locale
value inlocalStorage
> browser detection> default language set by default> Chinese
Support some extensions and customizations to internationalization at runtime, such as custom language recognition.
Customize language acquisition logic, such as identifying the link ?locale=${lang}
as the language of the current page.
// src/app.jsimport qs from 'qs';export const locale = {getLocale() {const { search } = window.location;const { locale = 'zh-CN' } = qs.parse(search, { ignoreQueryPrefix: true });return locale;},};
Custom language switching logic. There are three parameters:
setLocale(lang, true)
.For example, according to the language to be switched, jump to the corresponding url:
// src/app.jsexport const locale = {setLocale({ lang, realReload, updater }) {history.push(`/?locale=${lang}`);updater();},};
Although formatMessage is very convenient to use, it is out of the life cycle of react. The most serious problem is that it cannot trigger dom to re-render when switching languages. In order to solve this problem, we refresh the browser when switching languages. The user experience is very poor, so we recommend you to use useIntl
or injectIntl
, can achieve the same function.