英文:
vite + react + typescript + i18next
问题
我在将i18next添加到我的应用程序时遇到问题,t('test')
没有被翻译。
这是我的 i18n.ts
文件
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import enJSON from './locales/en.json';
import cnJSON from './locales/cn.json';
i18n.use(initReactI18next).init({
resources: {
en: { ...enJSON },
cn: { ...cnJSON },
},
lng: "en",
});
这是我的 App.tsx
文件
import { useState } from 'react'
import { useTranslation } from 'react-i18next';
function App() {
const { t, i18n: {changeLanguage, language} } = useTranslation();
const [currentLanguage, setCurrentLanguage] = useState(language)
const handleChangeLanguage = () => {
const newLanguage = currentLanguage === "en" ? "cn" : "en";
setCurrentLanguage(newLanguage);
changeLanguage(newLanguage);
}
return (
<div>
<h1>
{t('test')}
</h1>
<h3>
Current Language: {currentLanguage}
</h3>
<button
type="button"
onClick={() => handleChangeLanguage()}
>
Change Language
</button>
</div>
)
}
export default App
英文:
I have trouble while adding i18next to my application, t('test')
is not translate
This is my i18n.ts
file
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import enJSON from './locales/en.json';
import cnJSON from './locales/cn.json';
i18n.use(initReactI18next).init({
resources: {
en: { ...enJSON },
cn: { ...cnJSON },
},
lng: "en",
});
This is my App.tsx
file
import { useState } from 'react'
import { useTranslation } from 'react-i18next';
function App() {
const { t, i18n: {changeLanguage, language} } = useTranslation();
const [currentLanguage, setCurrentLanguage] = useState(language)
const handleChangeLanguage = () => {
const newLanguage = currentLanguage === "en" ? "cn" : "en";
setCurrentLanguage(newLanguage);
changeLanguage(newLanguage);
}
return (
<div>
<h1>
{t('test')}
</h1>
<h3>
Current Language: {currentLanguage}
</h3>
<button
type="button"
onClick={()=> handleChangeLanguage()}
>
Change Language
</button>
</div>
)
}
export default App
答案1
得分: 1
在你的 main.ts 或 index.ts 文件中,
import React, { Component } from "react";
import { createRoot } from 'react-dom/client';
import App from './App';
// 导入 i18n(需要捆绑)
import './i18n';
const root = createRoot(document.getElementById('root'));
root.render(
<App />
);
或者你可以尝试简单地将你的 App 组件包装在 I18nextProvider 中(从 'react-i18next' 导入),并传递你创建的 i18n 实例,像这样:
<I18nextProvider i18n={i18n}>
<App />
</I18nextProvider>
英文:
In your main.ts or index.ts,
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import React, { Component } from "react";
import { createRoot } from 'react-dom/client';
import App from './App';
// import i18n (needs to be bundled ;))
import './i18n';
const root = createRoot(document.getElementById('root'));
root.render(
<App />
);
<!-- end snippet -->
or you can simply try wrapping your App component I18nextProvider (imported from 'react-i18next') and passing it the i18n instance you created, like this;
<I18nextProvider i18n={i18n}>
<App />
<I18nextProvider />
答案2
得分: 0
抱歉我的英文,你尝试过更改路由的路径吗?http://localhost:300/en/home 或 http://localhost:300/cn/home
英文:
excuse my english, did you try to change the path of the route? http://localhost:300/en/home or http://localhost:300/cn/home
答案3
得分: 0
{
"translations": {
"test": "测试"
}
}
使用 t('translations:test')
英文:
for
{
"translations": {
"test": "test"
}
}
use
t('translations:test')
答案4
得分: -1
以下是您要翻译的内容:
"It seems that you have initialized i18next correctly, but you need to ensure that your translation keys are correctly defined in your JSON files.
For example, if you have a translation key called "test" in your JSON files, it should look something like this:
en.json
"test": "This is a test"
cn.json
"test": "这是一个测试"
If your translation keys are defined correctly in your JSON files, then you can try the following steps to troubleshoot your issue:
Ensure that your JSON files are being loaded correctly by checking the network tab in your browser's developer tools. Make sure that the JSON files are being loaded and that their contents are correct.
Check your console for any errors related to i18next or react-i18next. If there are any errors, try to resolve them first.
Ensure that your translation key is being passed correctly to the t function. You can try logging the value of t('test') to see if it matches the expected translation.
If none of the above solutions work, you can try using the useEffect hook to force a re-render of your component when the language is changed. Here's an example:
import { useState, useEffect } from 'react'
import { useTranslation } from 'react-i18next';
function App() {
const { t, i18n: {changeLanguage, language} } = useTranslation();
const [currentLanguage, setCurrentLanguage] = useState(language)
useEffect(() => {
setCurrentLanguage(language);
}, [language]);
const handleChangeLanguage = () => {
const newLanguage = currentLanguage === "en" ? "cn" : "en";
setCurrentLanguage(newLanguage);
changeLanguage(newLanguage);
}
return (
<div>
<h1>
{t('test')}
</h1>
<h3>
Current Language: {currentLanguage}
</h3>
<button
type="button"
onClick={()=> handleChangeLanguage()}
>
Change Language
</button>
</div>
)
}
export default App
This code will re-render the component whenever the language changes, which should force the translations to update."
英文:
It seems that you have initialized i18next correctly, but you need to ensure that your translation keys are correctly defined in your JSON files.
For example, if you have a translation key called "test" in your JSON files, it should look something like this:
en.json
"test": "This is a test"
cn.json
"test": "这是一个测试"
If your translation keys are defined correctly in your JSON files, then you can try the following steps to troubleshoot your issue:
Ensure that your JSON files are being loaded correctly by checking the network tab in your browser's developer tools. Make sure that the JSON files are being loaded and that their contents are correct.
Check your console for any errors related to i18next or react-i18next. If there are any errors, try to resolve them first.
Ensure that your translation key is being passed correctly to the t function. You can try logging the value of t('test') to see if it matches the expected translation.
If none of the above solutions work, you can try using the useEffect hook to force a re-render of your component when the language is changed. Here's an example:
import { useState, useEffect } from 'react'
import { useTranslation } from 'react-i18next';
function App() {
const { t, i18n: {changeLanguage, language} } = useTranslation();
const [currentLanguage, setCurrentLanguage] = useState(language)
useEffect(() => {
setCurrentLanguage(language);
}, [language]);
const handleChangeLanguage = () => {
const newLanguage = currentLanguage === "en" ? "cn" : "en";
setCurrentLanguage(newLanguage);
changeLanguage(newLanguage);
}
return (
<div>
<h1>
{t('test')}
</h1>
<h3>
Current Language: {currentLanguage}
</h3>
<button
type="button"
onClick={()=> handleChangeLanguage()}
>
Change Language
</button>
</div>
)
}
export default App
This code will re-render the component whenever the language changes, which should force the translations to update.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论