Quick Start
This guide will help you quickly integrate internationalization functionality into your Modern.js project.
Install Plugin
First, install the necessary dependencies:
请确保 @modern-js/plugin-i18n 的版本与项目中 @modern-js/app-tools 的版本一致。所有 Modern.js 官方包都使用统一的版本号发布,版本不匹配可能会导致兼容性问题。
请先检查 @modern-js/app-tools 的版本,然后安装相同版本的 @modern-js/plugin-i18n:
i18next 和 react-i18next 是 peer dependencies,需要手动安装。
Basic Configuration
Configure Plugin in modern.config.ts
server.publicDir is a required configuration used to specify the actual location of resource files. Even when using the default loadPath, this configuration is still required.
Configure Runtime Options in src/modern.runtime.ts
创建 src/modern.runtime.ts 文件并配置 i18n 运行时选项:
modern.runtime.ts 是运行时配置文件,用于配置 i18next 的初始化选项。即使是最基础的配置,也建议创建此文件以确保插件正常工作。
建议使用 i18next.createInstance() 创建一个新的实例,而不是直接使用默认导出的 i18next,这样可以避免全局状态污染,并确保每个应用都有独立的 i18n 实例。
Create Language Resource Files
Create a locales folder in the project root and organize resource files by language:
locales/en/translation.json:
locales/zh/translation.json:
Use in Components
Next Steps
- Learn detailed Configuration instructions
- Learn about multiple Locale Detection methods
- Check Resource Loading configuration options