react-i18next使用翻译将.json传递给.json

发布于 2025-01-27 02:49:05 字数 1796 浏览 5 评论 0原文

我在此处遵循文章: https:> https:> https: //dev.to/adrai/how-to-properly-internations-a-react-application-iusing-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;

当前:浏览器显示“ hello $ {name},很高兴见到您。 '

我需要的东西:浏览器显示“ 你好约翰·杜,很高兴见到你。

I followed the article here: https://dev.to/adrai/how-to-properly-internationalize-a-react-application-using-i18next-3hdb.

Now I want to know if there is a way to pass an argument into the string pulled from the .json file.

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;

currently: the browser is showing "Hello ${name}, nice to see you."

What I need: the browser to show "Hello John Doe, nice to see you."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望笑 2025-02-03 02:49:05

默认情况下,i18next使用不同的格式前缀({{)和后缀(}}}

尝试像这样编写翻译:

{
  "GREETING": "Hello {{name}}, nice to see you."
}

<p>{t('GREETING', {name: "John Doe")}</p>

By default i18next uses different format prefixes ({{) and suffixes (}})

Try writing your translations like this:

{
  "GREETING": "Hello {{name}}, nice to see you."
}

And the interpolation variable like this:

<p>{t('GREETING', {name: "John Doe")}</p>
不打扰别人 2025-02-03 02:49:05

您必须将变量作为第二个参数传递给“ t”函数。

<p>{t('GREETING', {name: "John Doe")}</p>

You would have to pass the variable as the second argument to the "t" function.

<p>{t('GREETING', {name: "John Doe")}</p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文