react-i18next 使用翻译将值传递给 .json
react-i18next pass value into .json using translation
我关注了这里的文章:https://dev.to/adrai/how-to-properly-internationalize-a-react-application-using-i18next-3hdb。
现在我想知道是否有办法将参数传递到从 .json 文件中提取的字符串中。
public/locales/en/translation.json
{
"GREETING": "Hello ${name}, nice to see you."
}
src/i18n.ts
import i18n from 'i18next';
import {initReactI18next} from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-http-backend';
i18n
// i18next-http-backend
// loads translations from your server
// https://github.com/i18next/i18next-http-backend
.use(Backend)
// detect user language
// learn more: https://github.com/i18next/i18next-browser-languageDetector
.use(LanguageDetector)
// pass the i18n instance to react-i18next.
.use(initReactI18next)
// init i18next
// for all options read: https://www.i18next.com/overview/configuration-options
.init({
debug: false,
fallbackLng: 'en',
interpolation: {
escapeValue: false // not needed for react as it escapes by default
}
});
export default i18n;
src/App
import React, {useState} from 'react';
import {useTranslation} from 'react-i18next';
function App() {
const {t} = useTranslation();
const [name] = useState('John Doe');
return (
<div>
<p>{t('GREETING')}</p>
</div>
);
}
export default App;
当前: 浏览器显示“你好 ${name},很高兴见到你。”
我需要: 浏览器显示“你好 John Doe,很高兴见到你。”
您必须将变量作为第二个参数传递给“t”函数。
<p>{t('GREETING', {name: "John Doe")}</p>
默认情况下 i18next 使用不同的格式前缀 ({{
) 和后缀 (}}
)
试试这样写你的翻译:
{
"GREETING": "Hello {{name}}, nice to see you."
}
interpolation 变量如下:
<p>{t('GREETING', {name: "John Doe")}</p>
我关注了这里的文章:https://dev.to/adrai/how-to-properly-internationalize-a-react-application-using-i18next-3hdb。
现在我想知道是否有办法将参数传递到从 .json 文件中提取的字符串中。
public/locales/en/translation.json
{
"GREETING": "Hello ${name}, nice to see you."
}
src/i18n.ts
import i18n from 'i18next';
import {initReactI18next} from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-http-backend';
i18n
// i18next-http-backend
// loads translations from your server
// https://github.com/i18next/i18next-http-backend
.use(Backend)
// detect user language
// learn more: https://github.com/i18next/i18next-browser-languageDetector
.use(LanguageDetector)
// pass the i18n instance to react-i18next.
.use(initReactI18next)
// init i18next
// for all options read: https://www.i18next.com/overview/configuration-options
.init({
debug: false,
fallbackLng: 'en',
interpolation: {
escapeValue: false // not needed for react as it escapes by default
}
});
export default i18n;
src/App
import React, {useState} from 'react';
import {useTranslation} from 'react-i18next';
function App() {
const {t} = useTranslation();
const [name] = useState('John Doe');
return (
<div>
<p>{t('GREETING')}</p>
</div>
);
}
export default App;
当前: 浏览器显示“你好 ${name},很高兴见到你。”
我需要: 浏览器显示“你好 John Doe,很高兴见到你。”
您必须将变量作为第二个参数传递给“t”函数。
<p>{t('GREETING', {name: "John Doe")}</p>
默认情况下 i18next 使用不同的格式前缀 ({{
) 和后缀 (}}
)
试试这样写你的翻译:
{
"GREETING": "Hello {{name}}, nice to see you."
}
interpolation 变量如下:
<p>{t('GREETING', {name: "John Doe")}</p>