Reactive element
Global state management
Routing
Stylized elements
Advance
Use Icon
GemElement more
Theme
Internationalization
# Getting started
# Cache
# Variable substitution
# Language specified in URL
# Sub module
# Localize
# Example
VS other libraries
Development
Building
Testing
Debug
FAQ
Internationalization
Using Gem's i18n module, you can quickly add multi-language support to your application.
Getting started
import { I18n } from '@mantou/gem/helper/i18n';
const en = {
title: 'This is I18n',
};
const i18n = new I18n({
fallbackLanguage: 'en',
resources: {
en,
zh: 'data:text/plain;base64,eyJ0aXRsZSI6Iui/meaYr0kxOG4ifQ==',
},
});
console.log(i18n.get('title'));
When instantiating the I18n object, the appropriate language is automatically selected according to the browser settings. Language packs support remote reading for on-demand loading.
TIP
It is recommended to use Crowdin, Pontoon similar services to maintain your language pack
You can directly use i18n.get('title') in the element to read the value in the language pack.
The user selects the language or the remote language package can be cached, and it is used directly when the user enters the App next time. Use cache to specify, the cached data will be saved to localStorage.
The variable can also be a custom rendering function, which can customize the rendering of the content provided in the language template. The following example is to render the word detail specified in the language entry into a link:
Some multi-language front-end projects may need SEO support, so you need to specify different language versions in the URL. urlParamsType allows I18n to check the URL when instantiating, and supports multiple types:
path: root path, for example /zh/home
querystring: located in URL query string, the name is specified with urlParamsName
ccTLD: Subdomain, such as jp.website.com
gTLD: Top-level domain name, such as website.jp
const i18n = new I18n({
fallbackLanguage: 'en',
urlParamsType: 'ccTLD',
resources: {
en: {
title: 'This is I18n',
},
},
});
TIP
When urlParamsType is path, then the links in the page and the history routing switch need to be modified,
this operation can be defined globally by setting history.basePath: