Trans 组件在没有 i18nKey 属性的情况下不呈现翻译
Trans component does not render translation without i18nKey attribute
我有一个应用在我的应用中使用 Trans component to perform translations. I do not declare the i18nKey
attribute on my Trans component and just use the content inside the Trans
component to generate the key, yet even though a key exists, Trans
will not produce a translation unless i18nKey
is present on the component. The t
function from useTranslation
correctly translates its arguments, yet the Trans component will not translate to french in this example. Full project on github here 示例:
index.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import { I18nextProvider } from 'react-i18next';
import URLSearchParams from '@ungap/url-search-params';
import 'web/js/sentry';
import 'react-toastify/dist/ReactToastify.css';
import { Root } from 'web/js/page/root';
import i18n from 'common/i18n';
if (!window.URLSearchParams) {
window.URLSearchParams = URLSearchParams;
}
ReactDOM.render(
<I18nextProvider i18n={i18n}>
<Root />
</I18nextProvider>,
document.querySelector('.projectroot'),
);
topic/hangul/index.jsx
import React from 'react';
import { Trans } from 'react-i18next';
import { Utterance } from 'web/js/application-hook/utterance';
import './style.scss';
export function Hangul() {
return (
<div styleName='root'>
<section styleName='section'>
<Trans>
<Utterance text='한글'>한글</Utterance> and <Utterance text='조선글'>조선글</Utterance> are the respective names of the contemporary Korean writing system used in South Korea and North Korea.
</Trans>
<Trans>
<Utterance text='훈민정음'>훈민정음</Utterance>, the original name of the writing system, was introduced and promoted in 1446 by Sejong the Great.
</Trans>
</section>
</div>
);
}
common/i18n.js
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import fr from 'common/i18n/fr/translation.json';
import { SUPPORTED_LANGUAGE_IDS } from 'common/models';
i18n
.use(initReactI18next)
.init({
preload: SUPPORTED_LANGUAGE_IDS,
fallbackLng: 'en',
initImmediate: false,
returnEmptyString: false,
resources: {
fr: {
translation: fr
}
},
interpolation: {
escapeValue: false
},
react: {
hashTransKey: function(defaultValue) {
console.log('missing key', defaultValue);
return defaultValue;
},
}
});
export default i18n;
common/i18n/fr/translation.json
{
"The Korean Writing System": "Le système d'écriture coréen",
"<0>한글</0> and <2>조선글</2> are the respective names of the contemporary Korean writing system used in South Korea and North Korea.": "<0>한글</0> et <2>조선글</2> sont les noms respectifs du système d'écriture coréen contemporain utilisé en Corée du Sud et en Corée du Nord.",
"<0>훈민정음</0>, the original name of the writing system, was introduced and promoted in 1446 by Sejong the Great.": "<0>훈민정음</0>, le nom original du système d'écriture, a été introduit et promu en 1446 par Sejong le Grand."
}
我发现了为什么会这样,在 configuration options for i18next 中默认的 keySeparator
是 .
,所以我的所有文本都包含一个 .
字符由于 .
充当 keySeperator,因此未正确计算翻译。
我有一个应用在我的应用中使用 Trans component to perform translations. I do not declare the i18nKey
attribute on my Trans component and just use the content inside the Trans
component to generate the key, yet even though a key exists, Trans
will not produce a translation unless i18nKey
is present on the component. The t
function from useTranslation
correctly translates its arguments, yet the Trans component will not translate to french in this example. Full project on github here 示例:
index.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import { I18nextProvider } from 'react-i18next';
import URLSearchParams from '@ungap/url-search-params';
import 'web/js/sentry';
import 'react-toastify/dist/ReactToastify.css';
import { Root } from 'web/js/page/root';
import i18n from 'common/i18n';
if (!window.URLSearchParams) {
window.URLSearchParams = URLSearchParams;
}
ReactDOM.render(
<I18nextProvider i18n={i18n}>
<Root />
</I18nextProvider>,
document.querySelector('.projectroot'),
);
topic/hangul/index.jsx
import React from 'react';
import { Trans } from 'react-i18next';
import { Utterance } from 'web/js/application-hook/utterance';
import './style.scss';
export function Hangul() {
return (
<div styleName='root'>
<section styleName='section'>
<Trans>
<Utterance text='한글'>한글</Utterance> and <Utterance text='조선글'>조선글</Utterance> are the respective names of the contemporary Korean writing system used in South Korea and North Korea.
</Trans>
<Trans>
<Utterance text='훈민정음'>훈민정음</Utterance>, the original name of the writing system, was introduced and promoted in 1446 by Sejong the Great.
</Trans>
</section>
</div>
);
}
common/i18n.js
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import fr from 'common/i18n/fr/translation.json';
import { SUPPORTED_LANGUAGE_IDS } from 'common/models';
i18n
.use(initReactI18next)
.init({
preload: SUPPORTED_LANGUAGE_IDS,
fallbackLng: 'en',
initImmediate: false,
returnEmptyString: false,
resources: {
fr: {
translation: fr
}
},
interpolation: {
escapeValue: false
},
react: {
hashTransKey: function(defaultValue) {
console.log('missing key', defaultValue);
return defaultValue;
},
}
});
export default i18n;
common/i18n/fr/translation.json
{
"The Korean Writing System": "Le système d'écriture coréen",
"<0>한글</0> and <2>조선글</2> are the respective names of the contemporary Korean writing system used in South Korea and North Korea.": "<0>한글</0> et <2>조선글</2> sont les noms respectifs du système d'écriture coréen contemporain utilisé en Corée du Sud et en Corée du Nord.",
"<0>훈민정음</0>, the original name of the writing system, was introduced and promoted in 1446 by Sejong the Great.": "<0>훈민정음</0>, le nom original du système d'écriture, a été introduit et promu en 1446 par Sejong le Grand."
}
我发现了为什么会这样,在 configuration options for i18next 中默认的 keySeparator
是 .
,所以我的所有文本都包含一个 .
字符由于 .
充当 keySeperator,因此未正确计算翻译。