标签: react-i18next

使用 useTranslation() 中的 t() 时,反应 i18next“hooks”错误

t()我正在使用 React-i18next 库,我需要在函数内部获取翻译函数。

export function translateCell(cell) {
  const { t } = useTranslation();
  return (t(cell));
}
Run Code Online (Sandbox Code Playgroud)

使用时useTranslation()失败并出现错误

钩子只能在函数组件体内调用。

我尝试在初始化中使用I18nextProviderand .use(initReactI18next),但是两种设置都会产生相同的错误。

我是否以错误的方式使用它,或者我误解了什么?

编辑: 我发现,只有将呈现为组件的函数才能使用 useTranslation。喜欢

function export MyComponent (cell) {
  const { t } = useTranslation();
  return (<div>{t(cell)}</div>);
}
...
render(){
<MyComponent/>
Run Code Online (Sandbox Code Playgroud)

internationalization i18next reactjs react-i18next

6
推荐指数
1
解决办法
1万
查看次数

使用 react-i18next useTranslationHooks 时出现“i18next backendConnector:加载命名空间失败”

我正在尝试使用 useTranslation() 挂钩在我的 create-react-app 应用程序上实现 react-i18next。

但是,在控制台中,调试模式显示

i18next::backendConnector: 为语言加载命名空间 billingAddress 失败,将 /locales/en/billingAddress.json 解析为 json

i18next::translator: missingKey en billingAddress Form.email 客户电子邮件(默认)

i18next.ts

import {initReactI18next} from "react-i18next";
import i18next from "i18next";
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-xhr-backend';
import XHR from "i18next-xhr-backend";

i18next
// .use(Backend)
    .use(LanguageDetector)
    .use(XHR)
    .use(initReactI18next) // passes i18n down to react-i18next
    .init({
        lng: "en",
        fallbackLng: {
            'en-US':['en'],
            'zh-CN':['cn'],
            default:['en']
        },
        debug: true,
        ns: ['billingAddress'],
        defaultNS: 'billingAddress',
        // load: "currentOnly",
        interpolation: {
            escapeValue: false // react already safes …
Run Code Online (Sandbox Code Playgroud)

typescript i18next reactjs react-i18next react-hooks

6
推荐指数
1
解决办法
5832
查看次数

React-i18n Trans 组件包含 HTML 标签的翻译不起作用

我正在尝试使用react-i18next包含一些 HTML 标签的翻译 json,如下所示:

// en.json
{ "welcome": "Welcome to our site, <strong>{{name}}</strong>" }
Run Code Online (Sandbox Code Playgroud)

在我的 React 组件中,我希望字符串像这样呈现。

欢迎来到我们的网站,约翰

使用该useTranslation函数通常按字面意思打印字符串,而不将其解释为 HTML,例如Welcome to our site, <strong>John</strong>.

import React from 'react'
import { useTranslation } from 'react-i18next'

const App = () => {
  const { t } = useTranslation()

  return(
    <div>{t('welcome', { name: 'John' })}</div>
  )
}
Run Code Online (Sandbox Code Playgroud)

我将其替换为dangerouslySetInnerHTML并且它已正确呈现。

<div dangerouslySetInnerHTML={{ __html: t('welcome', { name: 'John' }) }}></div>
Run Code Online (Sandbox Code Playgroud)

但是,如果可能的话,我想避免使用dangerouslySetInnerHTML。我在文档中读到,您可以使用称为 Trans 组件的东西来进行 HTML 标签的翻译。 …

i18next reactjs react-i18next

6
推荐指数
2
解决办法
2万
查看次数

如何使用 react-i18next 正确等待翻译

我正在使用 react-18next 在整个 React 应用程序中加载翻译。我在让我的应用程序等待翻译时遇到问题。这打破了我们在 Jenkins 中的测试,因为他们在许多情况下都在搜索已翻译的密钥。

i18n.tsx:

i18n
.use(initReactI18next)
.init({
  resources, // set ressources to be used
  lng: "en", // set default language
  keySeparator: false, 
  interpolation: {
    escapeValue: false
  },
  react: {
    useSuspense: false,
  }
});
Run Code Online (Sandbox Code Playgroud)

注意:我尝试使用 Suspense 和不使用 Suspense,useSuspense 标志与尝试匹配(真/默认为 Suspense)。

尝试准备:

const SiteLabel: React.FunctionComponent<ISiteLabelProps> = (props) => {
  const { t, ready } = useTranslation(undefined, {
    useSuspense: false
  });

  const getTo = (): string => {
    return "/" + props.module.toLowerCase();
  }

  const getLabel = (): string => …
Run Code Online (Sandbox Code Playgroud)

internationalization reactjs react-i18next react-suspense

6
推荐指数
1
解决办法
714
查看次数

尽管浏览器有另一种语言,但 Server Next js 自动重定向到英语

服务器 Next.js 自动重定向到英语,尽管浏览器有另一种语言 http://localhost:3000/en 而不是 http://localhost:3000。

我的next-i18next.config

module.exports = {
    i18n: {
        locales: ['ua', 'en', 'ru', 'ar'],
        defaultLocale: 'ua',
    }
}
Run Code Online (Sandbox Code Playgroud)

浏览器中安装了乌克兰语和英语。一开始是乌克兰语。

Accept-Language在请求标头中:uk,en;q=0.9

如何让它不重定向到英文?我究竟做错了什么?我的package.json

{
    "name": "connect-prerelease",
    "version": "0.1.0",
    "private": true,
    "scripts": {
        "dev": "next",
        "build": "next build",
        "start": "next start -p $PORT"
    },
    "dependencies": {
        "@parse/react-ssr": "0.0.1-alpha.14",
        "@types/parse": "^2.18.6",
        "bootstrap": "^4.6.0",
        "next": "10.2.3",
        "next-i18next": "^8.5.0",
        "next-images": "^1.8.1",
        "parse": "^3.2.0",
        "react": "17.0.2",
        "react-bootstrap": "^1.6.1",
        "react-dom": "17.0.2"
    },
    "devDependencies": {
        "@types/react": "17.0.11",
        "next-compose": "0.0.2",
        "typescript": "4.3.2"
    }
}
Run Code Online (Sandbox Code Playgroud)

internationalization i18next next.js react-i18next

6
推荐指数
1
解决办法
4841
查看次数

有没有办法在没有 Next.js 国际化路由的情况下使用 next-i18next ?

next-i18next已经在几个项目中使用了 package,而且效果很好。

在我当前的项目中,我需要集成 i18n,但这次只有 cookie 支持,没有国际化路由,我想知道我是否可以用这个包实现这一点,或者我应该返回react-i18nexti18next

样本:

// No cookie or EN cookie
https://app.example.com shows EN version

// Cookie `next-i18n-lang` set to specific language (IT, DE, etc)
https://app.example.com shows translated version
Run Code Online (Sandbox Code Playgroud)

我认为我们不能next/router locale开箱即用,但这作为集成的缺点是可以接受的。

internationalization next.js react-i18next next-i18next

6
推荐指数
0
解决办法
1210
查看次数

next-i18next 翻译在故事书中不起作用,控制台日志缺少Key

我正在编写故事书,并使用next-i18next. 这就是我的设置方式:

// .storybook/i18n.js
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

i18n.use(initReactI18next).init({
  fallbackLng: 'de',
  debug: true,
});

export default i18n;
Run Code Online (Sandbox Code Playgroud)
// .storybook/preview.js
import { StoreMall } from '../components/layouts/StoreMall';
import { I18nextProvider } from 'react-i18next';
import i18n from './i18n';
import { ThemeProvider } from '@material-ui/core/styles';
import { jamesTheme } from '../components/colors';
import { themes } from '@storybook/theming';
import CssBaseline from '@material-ui/core/CssBaseline';

export const parameters = {
  // ...
};

export const decorators = [
  (Story) => …
Run Code Online (Sandbox Code Playgroud)

i18next react-i18next storybook next-i18next

6
推荐指数
1
解决办法
2971
查看次数

是否可以对 redux Actions 使用 i18n 本地化?

我试图为 Redux Action 函数实现 i18n 本地化,但我不断收到不同的钩子错误。当我以这种方式实现 i18n 时,我收到错误。

第 428:17 行:在函数“sendDataRequest”中调用 React Hook“useTranslation”,该函数既不是 React 函数组件,也不是自定义 React Hook 函数。

import { useTranslation } from "react-i18next";

export const sendDataRequest = (requestId) => {
  const { t } = useTranslation();
  return async (dispatch) => {
    try {
      dispatch(sendDataRequest());
      await dataAPI.sendDataRequest({ requestId });
      notification.success({
        message: t('infoPage.requestSentSuccessfully'),      
     });
      dispatch(sendDataSuccess());
    } catch (error) {
      dispatch(sendDataFailure());
      console.error(error);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我移动了const { t } = useTranslation(); return 语句内部。但后来我收到另一个错误在此输入图像描述 看来我在这里显然用错了。但我找不到任何有关在 Actions 中使用 i18n 的示例。有谁知道是否可以在 Actions 中使用 i18b?

internationalization i18next reactjs redux react-i18next

6
推荐指数
1
解决办法
3423
查看次数

i18next React - 更改语言仅在浏览器刷新后有效

在我的 React 页面上,更改语言仅在浏览器刷新后才有效。已经尝试了以前帖子中所有可能的建议,但一无所获。它仅对 App.js 组件上的字符串按预期工作。任何帮助表示赞赏。

这是我的 i18n.js 文件

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';

const resources = {
    'en': {translation: require('./translations/en.json')},
    'th': {translation: require('./translations/th.json')},
    'pt': {translation: require('./translations/pt.json')},
}

i18n
  // 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: true,
    fallbackLng: 'en',
    // lng: 'th',
    interpolation: {
      escapeValue: false, // not needed for react …
Run Code Online (Sandbox Code Playgroud)

i18next reactjs react-i18next

6
推荐指数
1
解决办法
1万
查看次数

获取 i18next 未定义

我正在初始化i18next但出现错误:

Uncaught TypeError: Cannot read property 'use' of undefined
Run Code Online (Sandbox Code Playgroud)

以下是我的代码:

import i18n from 'i18next';// Getting eslint error Module imports itself.
import { initReactI18next } from 'react-i18next';
import Backend from 'i18next-xhr-backend';
import LanguageDetector from 'i18next-browser-languagedetector';

const fallbackLng = ['en'];
const availableLanguages = ['en', 'ar', 'fr'];

i18n
  .use(Backend)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    fallbackLng,

    detection: {
      checkWhitelist: true,
    },

    debug: false,

    whitelist: availableLanguages,

    interpolation: {
      escapeValue: false,
    },
  });

export default i18n;
Run Code Online (Sandbox Code Playgroud)

反应:16.13.1

i18下一个:19.4.5

javascript i18next reactjs react-i18next

5
推荐指数
1
解决办法
9170
查看次数