Basic Concepts
Before integrating internationalization capabilities into your project, you need to understand internationalization-related concepts. Understanding core concepts can help you quickly establish a stable translation system and better solve various problems during use.
Core Concepts
i18n
i18n is the abbreviation for Internationalization, which refers to making applications run well in different languages, regions, and cultures. It requires considering factors such as multilingual resources, numbers/dates/currencies, and cultural differences at the design stage.
i18next
i18next is a general-purpose internationalization framework that provides capabilities such as language detection, resource management, interpolation, and pluralization. @modern-js/plugin-i18n is based on i18next by default. Please refer to its official documentation for complete configuration instructions.
react-i18next
react-i18next is a React binding library for i18next, providing Hooks/components such as useTranslation and Trans to achieve good integration with React lifecycle:
i18n Instance
i18next exports a default instance by default, and also supports generating multiple instances through createInstance:
The instance is responsible for translation resources, current language, language switching, and other functions. You can also pass a custom instance in Modern.js's runtime.
Initialization (init)
i18next completes initialization through init. Common core options:
lng: Initial languagens/defaultNS: Namespace list and default namespacesupportedLngs: Allowed language setfallbackLng: Fallback language when resources are missing (can be an array or mapping)interpolation: Interpolation settings, usually configured withescapeValue: falsein React environments
t Function
t is the core API for obtaining translations. It can be used directly from the instance or obtained through react-i18next Hook:
t supports advanced features such as interpolation, pluralization, and context, which will be explained in detail later.
Language Code
Language codes are used to identify the current interface language, following the ISO 639-1 standard (en, zh, etc.), and can also carry region information (en-US, zh-CN).
- Supported Language List: Declared through plugin configuration, so you can know what products need to be generated at compile time.
- Default Language: Used when user language cannot be detected or resources are missing.
- Fallback Language Chain: Chains like
en-US → en → zhdetermine the search order when translations are missing.
💡 It is recommended to maintain supportedLanguages and fallbackLanguage in sync to avoid situations where users switch to unconfigured languages.
Namespace
Namespaces are used to split translation files by business modules, facilitating code splitting and on-demand loading. The default namespace translation is used when not specified.
Using different namespaces in components:
Namespaces can also be combined with dynamic loading to request large amounts of text on demand.
Resource File Structure
Recommended resource file directory:
- File Naming:
locales/<language>/<namespace>.json - Format: Standard JSON, key-value pairs or nested objects
- Organization: Nested objects are used to represent UI hierarchy, such as buttons, dialogs, etc.
You can also directly inject resources through the resources option during initialization, or call addResourceBundle at runtime:
Translation Key
Translation keys are paths to access translations, usually using dots to represent hierarchy: common.button.submit.
Naming convention recommendations:
- Use semantic words, avoid abbreviations
- Divide prefixes by module (
dashboard.table.*) - Can use
:to specify namespace (common:button.submit) - Avoid using complete Chinese text directly as keys
Interpolation and Variables
Interpolation allows dynamic injection of variables into translation text.
Resource File:
Usage:
Nested Interpolation
You can directly pass objects or multi-level variables:
Formatted Interpolation
Format numbers, dates, etc. through the interpolation.format function:
Escaping Interpolation
react-i18next escapes interpolation values by default to prevent XSS. If you need to render safe HTML, you need to explicitly enable interpolation.escapeValue = false and ensure the data is trustworthy.
Pluralization
Pluralization automatically selects the appropriate word form based on the language, depending on the count parameter.
Different languages have different pluralization rules, for example:
- English: Singular, plural
- Russian: Multiple forms such as one, few, many
- Chinese: Usually only a single form, can use
_0key to override special text
💡 If you need to customize pluralization rules, you can extend through i18next.services.pluralResolver. See advanced usage for details.
Nested Translation Structure
Nested structures can intuitively reflect UI hierarchy.
Use dots to access in code:
Advantages of nested structures:
- Avoid lengthy key names
- Easy to view module text as a whole in JSON
- Can be combined with
keyPrefixto simplify calls:useTranslation('common', { keyPrefix: 'button' })
Fallback Language
When the current language is missing a key, it will continue searching according to the fallback language chain.
You can fallback from regional languages (such as zh-CN) to general languages (zh), and finally to the default language (en), ensuring that all keys have available text.
Language Detection
i18next automatically identifies user language through language detection plugins. Modern.js plugin has built-in browser and server support.
In Modern.js, you can directly enable built-in detection in the plugin configuration:
After enabling detection, there is no need to explicitly set lng in init. If you manually call changeLanguage() without passing a language, it will also automatically infer based on the detection configuration.