前端国际化最佳实践与实现方案
为什么需要国际化
近期项目拓展海外市场时,若所有文本硬编码在代码里,将导致维护困难。国际产品需支持多语言,而非仅限本地应用。
推荐方案
1. i18next
配置示例:
// i18n.js
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
const resources = {
zh: {
translation: {
welcome: '欢迎来到我的网站',
description: '这是一个示例应用',
button: '点击我',
products: '产品列表',
product1: '产品 1',
product2: '产品 2',
product3: '产品 3'
}
},
en: {
translation: {
welcome: 'Welcome to my website',
description: 'This is a sample application',
button: 'Click me',
products: 'Product List',
product1: 'Product 1',
product2: 'Product 2',
product3: 'Product 3'
}
}
};
i18n
.use(initReactI18next)
.init({
resources,
lng: ,
: ,
: { : }
});
i18n;

