跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客我的书AI学习GitHub 精选镜像AI工具UI配色美学关于
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
JavaScript大前端

前端国际化实战:i18next 与 react-intl 方案

前端国际化的必要性及实现方案。针对文本硬编码的问题,演示了如何使用 i18next 和 react-intl 库进行多语言配置与切换。此外,还涵盖了日期和数字格式化的本地化处理技巧,帮助开发者构建支持全球用户的国际化应用。

宁静发布于 2026/4/6更新于 2026/10/2397 浏览

前端国际化实战

为什么需要国际化

在拓展海外市场时,如果所有文本都硬编码在代码里,应用将难以被本地用户理解。以下是硬编码的反面教材。

反面教材
// 反面教材:硬编码文本
function App() {
  return (
    <div>
      <h1>欢迎来到我的网站</h1>
      <p>这是一个示例应用</p>
      <button>点击我</button>
      <div>
        <h2>产品列表</h2>
        <ul>
          <li>产品 1</li>
          <li>产品 2</li>
          <li>产品 3</li>
        </ul>
      </div>
    </div>
  );
}
export default App;

正确姿势

1. i18next

配置

// i18n.js
import i18n  ;
 { initReactI18next }  ;

 resources = {
  : {
    : {
      : ,
      : ,
      : ,
      : ,
      : ,
      : ,
      : 
    }
  },
  : {
    : {
      : ,
      : ,
      : ,
      : ,
      : ,
      : ,
      : 
    }
  }
};

i18n
  .(initReactI18next)
  .({
    resources,
    : ,
    : ,
    : {
      : 
    }
  });

  i18n;
from
'i18next'
import
from
'react-i18next'
const
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'
use
init
lng
'zh'
fallbackLng
'en'
interpolation
escapeValue
false
export
default

使用

// App.jsx
import React from 'react';
import { useTranslation } from 'react-i18next';

function App() {
  const { t, i18n } = useTranslation();
  const changeLanguage = (lng) => {
    i18n.changeLanguage(lng);
  };

  return (
    <div>
      <div>
        <button onClick={() => changeLanguage('zh')}>中文</button>
        <button onClick={() => changeLanguage('en')}>English</button>
      </div>
      <h1>{t('welcome')}</h1>
      <p>{t('description')}</p>
      <button>{t('button')}</button>
      <div>
        <h2>{t('products')}</h2>
        <ul>
          <li>{t('product1')}</li>
          <li>{t('product2')}</li>
          <li>{t('product3')}</li>
        </ul>
      </div>
    </div>
  );
}

export default App;

入口文件

// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './i18n';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);
2. react-intl

配置与使用

// App.jsx
import React from 'react';
import { IntlProvider, FormattedMessage } from 'react-intl';

const zhMessages = {
  welcome: '欢迎来到我的网站',
  description: '这是一个示例应用',
  button: '点击我',
  products: '产品列表',
  product1: '产品 1',
  product2: '产品 2',
  product3: '产品 3'
};

const enMessages = {
  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'
};

function App() {
  const [locale, setLocale] = React.useState('zh');
  const messages = locale === 'zh' ? zhMessages : enMessages;

  return (
    <IntlProvider locale={locale} messages={messages}>
      <div>
        <div>
          <button onClick={() => setLocale('zh')}>中文</button>
          <button onClick={() => setLocale('en')}>English</button>
        </div>
        <h1><FormattedMessage id="welcome" /></h1>
        <p><FormattedMessage id="description" /></p>
        <button><FormattedMessage id="button" /></button>
        <div>
          <h2><FormattedMessage id="products" /></h2>
          <ul>
            <li><FormattedMessage id="product1" /></li>
            <li><FormattedMessage id="product2" /></li>
            <li><FormattedMessage id="product3" /></li>
          </ul>
        </div>
      </div>
    </IntlProvider>
  );
}

export default App;
3. 日期和数字格式化
// 日期和数字格式化
import React from 'react';
import { useTranslation } from 'react-i18next';

function FormattingExample() {
  const { t, i18n } = useTranslation();

  const formatDate = (date) => {
    return new Intl.DateTimeFormat(i18n.language).format(date);
  };

  const formatNumber = (number) => {
    return new Intl.NumberFormat(i18n.language).format(number);
  };

  return (
    <div>
      <p>{t('currentDate')}: {formatDate(new Date())}</p>
      <p>{t('price')}: {formatNumber(12345.67)}</p>
    </div>
  );
}

export default FormattingExample;

通过以上方案,可以实现应用的多语言支持及本地化格式化,确保在全球范围内都能被用户理解。

目录

  1. 前端国际化实战
  2. 为什么需要国际化
  3. 反面教材
  4. 正确姿势
  5. 1. i18next
  6. 2. react-intl
  7. 3. 日期和数字格式化

更多推荐文章

查看全部
  • 基于 KaiwuDB 与 CodeArts 智能体的智能家居本地化数据处理方案
  • MoltBot 对接钉钉 Stream 流式接入配置指南
  • Awesome-Chinese-LLM:主流开源中文大模型与工具整理
  • C# 扩展 Dynamics 365 Copilot:自定义插件与场景实现
  • 计算机视觉基础、模型架构与实战应用
  • MySQL 数据库备份与恢复常用命令
  • 2025年6月GESP C++七级真题及题解:选择题与判断题
  • Android 开发:基于 ListView 实现简易备忘录
  • 4G Cat.1 模组赋能 AI 教育机器人:算力与物联网融合分析
  • AI 绘画实战指南:从提示词到高质量图像生成
  • Java Stream API 实战:对象列表属性提取与去重
  • 大模型分布式训练核心原理与高效调参实战
  • OpenClaw 本地部署、隧道映射与大模型接入指南
  • OpenClaw Java:基于 Spring Boot 的 AI Agent Gateway 全栈实践
  • 多平台热点内容自动化总结工作流实战
  • RPGViewerPlus:macOS 运行 RPG Maker 游戏的原生启动器方案
  • 自然语言处理在客户服务中的实战应用
  • 直流无刷电机 FOC 控制算法详解
  • OpenSUSE 最小化安装实战指南
  • macOS 本地部署 OpenClaw 智能体框架指南

相关免费在线工具

  • Keycode 信息

    查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online

  • Escape 与 Native 编解码

    JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online

  • JavaScript / HTML 格式化

    使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online

  • JavaScript 压缩与混淆

    Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online

  • Base64 字符串编码/解码

    将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online

  • Base64 文件转换器

    将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online