鸿海 發表於 2022-4-28 09:53:00

React 中使用 react-i18next 国际化

<p>本文使用 <code>React-i18next</code> 库结合 React, 介绍如何在 React 中配置使用国际化。<br>
<span style="color: rgba(220, 20, 60, 1)"><strong>官方地址:</strong></span>i18next | react-i18next</p>
<h2 id="简介">简介</h2>
<p>react-i18next 是基于 i18next 的一款强大的国际化框架,可以用于 react 和 react-native 应用;<br>
react-i18next 特点:</p>
<ol>
<li>提供多种组件可以在hoc, hook 和 class 的情况下进行国际化操作;</li>
<li>基于 i18next 不仅限于react,学一次就可以用在其它地方;</li>
<li>适合服务器的渲染;</li>
<li>有许多插件的支持,比如可以用插件检测当前系统的语言环境,从服务器或者文件系统加载翻译资源;</li>
</ol>
<h2 id="安装与使用">安装与使用</h2>
<h3 id="安装">安装</h3>
<pre><code class="language-shell"># npm
npm install react-i18next i18next --save
# 如果需要检测当前浏览器的语言或者从服务器获取配置资源可以安装下面依赖
npm install i18next-http-backend i18next-browser-languagedetector --save
</code></pre>
<h3 id="准备">准备</h3>
<ol>
<li>新建一个 React 项目,安装依赖包;<pre><code class="language-shell">npm install react-i18next i18next --save
</code></pre>
</li>
<li>新建文件 <code>en.json</code> 和 <code>zh.json</code>;
<ul>
<li>src\react-i18next\locales\en.json<pre><code class="language-json">{
        title: "Hello Word"
}
</code></pre>
</li>
<li>src\react-i18next\locales\zh.json<pre><code class="language-json">{
        title: "你好 世界"
}
</code></pre>
</li>
</ul>
</li>
<li>新建 <code>resources.js</code> 和 <code>i18n.js</code>;
<ul>
<li>src\react-i18next\locales\resources.js<pre><code class="language-js">import ja from "./ja.json";
import en from "./en.json";
import zh from "./zh.json";

export const resources = {
        "ja": {
                translation: ja
        },
        "en": {
                translation: en
        },
        "zh": {
                translation: zh
        }
}
</code></pre>
</li>
<li>src\react-i18next\i18n.js<pre><code class="language-js">import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import { resources } from './locales/resources';

i18n
        // 将 i18n 实例传递给 react-i18next
        .use(initReactI18next)
        // 初始化 i18next
        // 所有配置选项: https://www.i18next.com/overview/configuration-options
        .init({
                resources,
                fallbackLng: "zh",
                lng: "zh",
                debug: true,
                interpolation: {
                        escapeValue: false, // not needed for react as it escapes by default
                }
        });

export default i18n;
</code></pre>
</li>
</ul>
</li>
</ol>
<h3 id="使用">使用</h3>
<ol>
<li>在程序入口引入 i118n;<pre><code class="language-jsx">import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import "./react-i18next/i18n";

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
&lt;React.StrictMode&gt;
        &lt;App /&gt;
&lt;/React.StrictMode&gt;
);
</code></pre>
</li>
<li>在 Hooks 中使用国际化;<pre><code class="language-jsx">import { useTranslation } from "react-i18next";

function App() {
const { t } = useTranslation();
return (
        &lt;div className="App"&gt;
          {t("title")}
        &lt;/div&gt;
);
}

export default App;
</code></pre>
</li>
<li>在 class 组件中使用国际化;<pre><code class="language-jsx">import React from "react";
import { withTranslation } from "react-i18next";

class App extends React.Component {

render() {
        const { t } = this.props;
        return (&lt;div className="App"&gt;
          {t("title")}
        &lt;/div&gt;);
}
}

export default withTranslation()(App);
</code></pre>
</li>
</ol>
<h3 id="检测当前浏览器语言国际化组件">检测当前浏览器语言国际化组件</h3>
<ol>
<li>安装依赖<pre><code class="language-shell">npm install i18next-browser-languagedetector --save
</code></pre>
</li>
<li>配置使用插件<pre><code class="language-js">// src\react-i18next\i18n.js
import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import { initReactI18next } from 'react-i18next';
import { resources } from './locales/resources';

i18n
        // 检测用户语言
        // 操作文档: https://github.com/i18next/i18next-browser-languageDetector
        .use(LanguageDetector)
        // 将 i18n 实例传递给 react-i18next
        .use(initReactI18next)
        // 初始化 i18next
        // 所有配置选项: https://www.i18next.com/overview/configuration-options
        .init({
                resources,
                fallbackLng: "en",
                lng: navigator.language,
                debug: true,
                interpolation: {
                        escapeValue: false, // not needed for react as it escapes by default
                }
        });

export default i18n;
</code></pre>
上面代码,首先导入 LanguageDetector,其次 <code>use(LanguageDetector)</code>, 使用插件,最终在 init 配置项里配置 <code>lng: navigator.language</code>, 至此切换浏览器语言国际化组件完成;</li>
</ol>
<h3 id="手动切换国际化语言">手动切换国际化语言</h3>
<pre><code class="language-jsx">// class 组件
const { t, i18n } = this.props;
i18n.changeLanguage("en");                 // 手动切换到英文

// Hooks 组件
const { t, i18n } = useTranslation();
i18n.changeLanguage("zh");                 // 手动切换到中文
</code></pre>
<h2 id="总结">总结</h2>
<p>i18next 是一款强大的国际化框架,react-i18next 是基于 i18next 适用于 React 的框架,另外 i18next 还和很多的前端框架可以结合,所以只需要学习一次,学习成本低;<br>
本文介绍了 react-i18next 的基本用法,如果更特殊的需求,文章开头的官方地址可以找到答案;</p><br><br>
来源:https://www.cnblogs.com/operate/p/16199940.html
頁: [1]
查看完整版本: React 中使用 react-i18next 国际化