前端国际化, React 项目多语言:React-i18next 本地化数据 && 请求后台数据
<p>关键词:前端,国际化,多语言,react,react-i18next,i18n, antd</p><p> </p>
<p><span style="font-size: 18px; color: rgba(255, 102, 0, 1)"><strong>GitHub Examples</strong></span></p>
<p> </p>
<p><span style="font-size: 16px"><strong>参考资料:</strong></span></p>
<ul>
<li><span style="font-size: 16px"><strong>react-i18next </strong></span></li>
<li><span style="font-size: 16px"><strong>react-i18next documentation</strong></span></li>
<li><span style="font-size: 16px"><strong>i18next documentation</strong></span></li>
<li><span style="font-size: 16px"><strong>i18next-xhr-backend</strong></span></li>
<li><span style="font-size: 16px"><strong>i18next-browser-languageDetector</strong></span></li>
<li><span style="font-size: 16px"><strong>i18next-multiload-backend-adapter</strong></span></li>
</ul>
<p> </p>
<p><span style="font-size: 16px"><strong>一、搭建 React 项目</strong></span></p>
<div class="cnblogs_code">
<pre>npx create-react-app demo</pre>
</div>
<p> </p>
<p><span style="font-size: 16px"><strong>二、安装 react-i18next</strong></span></p>
<div class="cnblogs_code">
<pre>npm install i18next react-i18next --save</pre>
</div>
<p> </p>
<p><span style="font-size: 16px"><strong>三、两种使用方式 (本地化数据、请求后台数据)</strong></span></p>
<p> </p>
<p><span style="font-size: 16px"><strong><span style="font-size: 18px">方式一</span>:本地化数据</strong></span></p>
<p><strong>Basic sample:</strong></p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import ReactDOM from </span>'react-dom'<span style="color: rgba(0, 0, 0, 1)">;
import i18n from </span>'i18next'<span style="color: rgba(0, 0, 0, 1)">;
import { useTranslation, initReactI18next } from </span>'react-i18next'<span style="color: rgba(0, 0, 0, 1)">;
i18n
.use(initReactI18next) </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> passes i18n down to react-i18next</span>
<span style="color: rgba(0, 0, 0, 1)">.init({
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> the translations</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (tip move them in a JSON file and import them)</span>
<span style="color: rgba(0, 0, 0, 1)"> resources: {
en: {
translation: {
</span>'Welcome to React': 'Welcome to React and react-i18next'<span style="color: rgba(0, 0, 0, 1)">
}
},
zh: {
translation: {
</span>'Welcome to React': '欢迎使用 React 和 react-i18next'<span style="color: rgba(0, 0, 0, 1)">
}
}
},
lng: </span>'en'<span style="color: rgba(0, 0, 0, 1)">,
fallbackLng: </span>'en'<span style="color: rgba(0, 0, 0, 1)">,
interpolation: {
escapeValue: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
}
});
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> App() {
const { t } </span>=<span style="color: rgba(0, 0, 0, 1)"> useTranslation();
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <h2>{t('Welcome to React')}</h2>;
<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> append app to dom</span>
ReactDOM.render(<App />, document.getElementById('root'));</pre>
</div>
<p> </p>
<p><strong>Basic sample with JSON:</strong></p>
<p>根据 resources 上面的注释,把 resources 部分提取到 json 文件中,然后 import 引入。</p>
<p>en.json:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
</span>"translation"<span style="color: rgba(0, 0, 0, 1)">: {
</span>"Welcome to React": "Welcome to React and react-i18next"<span style="color: rgba(0, 0, 0, 1)">
}
}</span></pre>
</div>
<p>zh.json:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
</span>"translation"<span style="color: rgba(0, 0, 0, 1)">: {
</span>"Welcome to React": "欢迎使用 React 和 react-i18next"<span style="color: rgba(0, 0, 0, 1)">
}
}</span></pre>
</div>
<p><strong>注意</strong>:translation 是 react-i18next 默认的命名空间(暂时先记着这样写,后面可以自定义命名空间)</p>
<p><strong> </strong>引入 JSON 文件:( 引入路径要根据 json 文件所在 src 目录下的相对路径 )</p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import ReactDOM from </span>'react-dom'<span style="color: rgba(0, 0, 0, 1)">;
import i18n from </span>'i18next'<span style="color: rgba(0, 0, 0, 1)">;
import en from </span>'./en.json'<span style="color: rgba(0, 0, 0, 1)">;
import zh from </span>'./zh.json'<span style="color: rgba(0, 0, 0, 1)">;
import { useTranslation, initReactI18next } from </span>'react-i18next'<span style="color: rgba(0, 0, 0, 1)">;
i18n
.use(initReactI18next) </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> passes i18n down to react-i18next</span>
<span style="color: rgba(0, 0, 0, 1)">.init({
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> the translations</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (tip move them in a JSON file and import them)</span>
<span style="color: rgba(0, 0, 0, 1)"> resources: {
en,
zh
},
lng: </span>'en'<span style="color: rgba(0, 0, 0, 1)">,
fallbackLng: </span>'en'<span style="color: rgba(0, 0, 0, 1)">,
interpolation: {
escapeValue: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
}
});
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> App() {
const { t } </span>=<span style="color: rgba(0, 0, 0, 1)"> useTranslation();
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <h2>{t('Welcome to React')}</h2>;
<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> append app to dom</span>
ReactDOM.render(<App />, document.getElementById('root'));</pre>
</div>
<p> </p>
<p>目前为止,还没有用到 <em>i18next-xhr-backend</em> , 这个插件可以使你的 json 文件放到 public 目录下使用(下例 Locales JSON)。 </p>
<p>而 <em>i18next-browser-languagedetector</em> , 可以帮助你自动识别浏览器语言</p>
<p> </p>
<p><span style="color: rgba(0, 0, 0, 1)"><strong>Basic sample with <span style="color: rgba(255, 102, 0, 1)">Locales</span> JSON: <span style="color: rgba(255, 102, 0, 1)"><strong>(推荐)</strong></span></strong></span></p>
<p>继续安装插件:</p>
<div class="cnblogs_code">
<pre>npm install i18next-xhr-backend i18next-browser-languagedetector --save</pre>
</div>
<p><span style="background-color: rgba(255, 255, 255, 1); color: rgba(0, 0, 0, 1)">在 <em><strong>public</strong></em> 目录下新建 <em><strong>locales</strong></em> 文件夹,继续新建对应语言的<span style="text-decoration: underline">文件夹</span> (例如:<em><strong>en</strong></em>,<em><strong>zh</strong></em>),然后创建 <em><strong>translation.json</strong></em>。</span></p>
<p>例:public/locales/en/translation.json:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
</span>"Welcome to React": "Welcome to React and react-i18next"<span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p>修改 i18n 初始化:</p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import ReactDOM from </span>'react-dom'<span style="color: rgba(0, 0, 0, 1)">;
import i18n from </span>'i18next'<span style="color: rgba(0, 0, 0, 1)">;
import Backend from </span>'i18next-xhr-backend'<span style="color: rgba(0, 0, 0, 1)">;
import LanguageDetector from </span>'i18next-browser-languagedetector'<span style="color: rgba(0, 0, 0, 1)">;
import { useTranslation, initReactI18next } from </span>'react-i18next'<span style="color: rgba(0, 0, 0, 1)">;
i18n
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> load translation using xhr -> see /public/locales</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> learn more: https://github.com/i18next/i18next-xhr-backend</span>
<span style="color: rgba(0, 0, 0, 1)">.use(Backend)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> detect user language</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> learn more: https://github.com/i18next/i18next-browser-languageDetector</span>
<span style="color: rgba(0, 0, 0, 1)">.use(LanguageDetector)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> pass the i18n instance to react-i18next.</span>
<span style="color: rgba(0, 0, 0, 1)">.use(initReactI18next)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> init i18next</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> for all options read: https://www.i18next.com/overview/configuration-options</span>
<span style="color: rgba(0, 0, 0, 1)">.init({
fallbackLng: </span>'en'<span style="color: rgba(0, 0, 0, 1)">,
debug: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
interpolation: {
escapeValue: </span><span style="color: rgba(0, 0, 255, 1)">false</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> not needed for react as it escapes by default</span>
<span style="color: rgba(0, 0, 0, 1)"> },
});
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> App() {
const { t } </span>=<span style="color: rgba(0, 0, 0, 1)"> useTranslation();
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <h2>{t('Welcome to React')}</h2>;
<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> append app to dom</span>
ReactDOM.render(<App />, document.getElementById('root'));</pre>
</div>
<p> </p>
<p><span style="font-size: 16px"><strong><strong><span style="font-size: 18px">方式二</span>:</strong>请求后台数据</strong></span></p>
<p> <strong>Basic sample with <span style="color: rgba(255, 102, 0, 1)">XHR</span> JSON:</strong></p>
<p> 继续安装插件:</p>
<div class="cnblogs_code">
<pre>npm install i18next-xhr-backend i18next-browser-<span style="color: rgba(0, 0, 0, 1)">languagedetector i18next</span>-multiload-backend-adapter --save</pre>
</div>
<p> </p>
<p>使用实例:</p>
<div class="cnblogs_code">
<pre>import React, { Suspense } from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import ReactDOM from </span>'react-dom'<span style="color: rgba(0, 0, 0, 1)">;
import i18n from </span>'i18next'<span style="color: rgba(0, 0, 0, 1)">;
import XHR from </span>'i18next-xhr-backend'<span style="color: rgba(0, 0, 0, 1)">;
import BackendAdapter from </span>'i18next-multiload-backend-adapter'<span style="color: rgba(0, 0, 0, 1)">;
import LanguageDetector from </span>'i18next-browser-languagedetector'<span style="color: rgba(0, 0, 0, 1)">;
import { useTranslation, initReactI18next } from </span>'react-i18next'<span style="color: rgba(0, 0, 0, 1)">;
i18n
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> learn more: https://github.com/i18next/i18next-multiload-backend-adapter</span>
<span style="color: rgba(0, 0, 0, 1)">.use(BackendAdapter)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> detect user language</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> learn more: https://github.com/i18next/i18next-browser-languageDetector</span>
<span style="color: rgba(0, 0, 0, 1)">.use(LanguageDetector)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> pass the i18n instance to react-i18next.</span>
<span style="color: rgba(0, 0, 0, 1)">.use(initReactI18next)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> init i18next</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> for all options read: https://www.i18next.com/overview/configuration-options</span>
<span style="color: rgba(0, 0, 0, 1)">.init({
lng: navigator.language,
fallbackLng: </span>'en'<span style="color: rgba(0, 0, 0, 1)">,
debug: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
preload: navigator.languages,
interpolation: {
escapeValue: </span><span style="color: rgba(0, 0, 255, 1)">false</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> not needed for react as it escapes by default</span>
<span style="color: rgba(0, 0, 0, 1)"> },
backend: {
backend: XHR,
allowMultiLoading: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> learn more: https://github.com/i18next/i18next-xhr-backend</span>
<span style="color: rgba(0, 0, 0, 1)"> backendOption: {
loadPath: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(lngs, namespaces) {
console.log(lngs, namespaces);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> 'https://my-json-server.typicode.com/TserHub/Json/qinwm?lng={{lng}}&ns={{ns}}'<span style="color: rgba(0, 0, 0, 1)">;
},
addPath:
</span>'https://my-json-server.typicode.com/TserHub/Json/qinwm?lng={{lng}}&ns={{ns}}'<span style="color: rgba(0, 0, 0, 1)">
}
}
});
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> App() {
const { t } </span>=<span style="color: rgba(0, 0, 0, 1)"> useTranslation();
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <h2>{t('Welcome to React')}</h2>;
<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> loading component for suspense fallback</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> Loader() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <div>Loading...</div>;
<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> append app to dom</span>
<span style="color: rgba(0, 0, 0, 1)">ReactDOM.render(
</span><Suspense fallback={<Loader />}>
<App />
</Suspense>,
document.getElementById('root'<span style="color: rgba(0, 0, 0, 1)">)
);</span></pre>
</div>
<p> </p>
<p>更多 i18n 的详细配置 和 使用方法,请自行去参考资料查找。</p>
<p> </p>
<div id="gtx-trans" style="position: absolute; left: 415px; top: -16px"> </div><br><br>
来源:https://www.cnblogs.com/jserhub/p/12651376.html
頁:
[1]