Sure, here’s the translation: vite + react + typescript + i18next

huangapple go评论75阅读模式
英文:

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(&#39;test&#39;) is not translate

This is my i18n.ts file

import i18n from &quot;i18next&quot;;
import { initReactI18next } from &quot;react-i18next&quot;;
import enJSON from &#39;./locales/en.json&#39;;
import cnJSON from &#39;./locales/cn.json&#39;;
i18n.use(initReactI18next).init({
    resources: {
        en: { ...enJSON },
        cn: { ...cnJSON },
    }, 
    lng: &quot;en&quot;, 
});

This is my App.tsx file

import { useState } from &#39;react&#39;
import { useTranslation } from &#39;react-i18next&#39;;

function App() {
  const { t, i18n: {changeLanguage, language} } = useTranslation();
  const [currentLanguage, setCurrentLanguage] = useState(language)

  const handleChangeLanguage = () =&gt; {
    const newLanguage = currentLanguage === &quot;en&quot; ? &quot;cn&quot; : &quot;en&quot;;
    setCurrentLanguage(newLanguage);
    changeLanguage(newLanguage);
  }
  
  
  return (
    &lt;div&gt;
        &lt;h1&gt;
        {t(&#39;test&#39;)}
        &lt;/h1&gt;
        &lt;h3&gt;
            Current Language: {currentLanguage}
        &lt;/h3&gt;
        &lt;button 
            type=&quot;button&quot; 
            onClick={()=&gt; handleChangeLanguage()}
        &gt;
            Change Language
        &lt;/button&gt;
    &lt;/div&gt;
  )
}

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 &quot;react&quot;;
import { createRoot } from &#39;react-dom/client&#39;;
import App from &#39;./App&#39;;

// import i18n (needs to be bundled ;)) 
import &#39;./i18n&#39;;

const root = createRoot(document.getElementById(&#39;root&#39;));
root.render(
  &lt;App /&gt;
);

<!-- 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;

    &lt;I18nextProvider i18n={i18n}&gt;
        &lt;App /&gt;
    &lt;I18nextProvider /&gt;

答案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

{
  &quot;translations&quot;: {
    &quot;test&quot;: &quot;test&quot;
  }
}

use

t(&#39;translations:test&#39;)

答案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

  &quot;test&quot;: &quot;This is a test&quot;

cn.json

  &quot;test&quot;: &quot;这是一个测试&quot;

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 &#39;react&#39;
import { useTranslation } from &#39;react-i18next&#39;;

function App() {
  const { t, i18n: {changeLanguage, language} } = useTranslation();
  const [currentLanguage, setCurrentLanguage] = useState(language)

  useEffect(() =&gt; {
    setCurrentLanguage(language);
  }, [language]);

  const handleChangeLanguage = () =&gt; {
    const newLanguage = currentLanguage === &quot;en&quot; ? &quot;cn&quot; : &quot;en&quot;;
    setCurrentLanguage(newLanguage);
    changeLanguage(newLanguage);
  }
  
  return (
    &lt;div&gt;
        &lt;h1&gt;
        {t(&#39;test&#39;)}
        &lt;/h1&gt;
        &lt;h3&gt;
            Current Language: {currentLanguage}
        &lt;/h3&gt;
        &lt;button 
            type=&quot;button&quot; 
            onClick={()=&gt; handleChangeLanguage()}
        &gt;
            Change Language
        &lt;/button&gt;
    &lt;/div&gt;
  )
}

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

  &quot;test&quot;: &quot;This is a test&quot;

cn.json

  &quot;test&quot;: &quot;这是一个测试&quot;

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 &#39;react&#39;
import { useTranslation } from &#39;react-i18next&#39;;

function App() {
  const { t, i18n: {changeLanguage, language} } = useTranslation();
  const [currentLanguage, setCurrentLanguage] = useState(language)

  useEffect(() =&gt; {
    setCurrentLanguage(language);
  }, [language]);

  const handleChangeLanguage = () =&gt; {
    const newLanguage = currentLanguage === &quot;en&quot; ? &quot;cn&quot; : &quot;en&quot;;
    setCurrentLanguage(newLanguage);
    changeLanguage(newLanguage);
  }
  
  return (
    &lt;div&gt;
        &lt;h1&gt;
        {t(&#39;test&#39;)}
        &lt;/h1&gt;
        &lt;h3&gt;
            Current Language: {currentLanguage}
        &lt;/h3&gt;
        &lt;button 
            type=&quot;button&quot; 
            onClick={()=&gt; handleChangeLanguage()}
        &gt;
            Change Language
        &lt;/button&gt;
    &lt;/div&gt;
  )
}

export default App

This code will re-render the component whenever the language changes, which should force the translations to update.

huangapple
  • 本文由 发表于 2023年5月17日 13:15:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定