首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >I18next和Webpack

I18next和Webpack
EN

Stack Overflow用户
提问于 2021-06-21 06:45:15
回答 1查看 924关注 0票数 1

我从Webpack开始,发现了i18next钩子翻译的问题.

代码语言:javascript
复制
// webpack.config.js
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyPlugin = require("copy-webpack-plugin")

module.exports = {
    output: { 
        path: path.join(__dirname, '/dist'),
        filename: 'index.bundle.js'
    },
    devServer: {
        port: 3000,
        host: 'localhost',
        watchContentBase: true,
      
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /nodeModules/,
                use: {
                    loader: "babel-loader"
                }
            },
        ]
    },
    plugins: [
        new HtmlWebpackPlugin(
            { 
                template: './src/index.html' 
            }
        ),
        new CopyPlugin({
            patterns: [
                {
                    from: path.join(__dirname, '/public') , to: path.join(__dirname, '/dist') // copy public folder, which contains locales for i18next
                }
            ]
        })
    ],
}
代码语言:javascript
复制
//i18next.js
import i18next from 'i18next'
import { initReactI18next } from 'react-i18next'
import HttpApi from 'i18next-http-backend' 

i18next
  .use(initReactI18next)
  .use(HttpApi)
  .init(
    {
      backend: {
        loadPath: '/locales/{{lng}}/{{ns}}.json'
      },
      lng: "en_GB",
      supportedLngs: ["en_GB","cs_CZ"]
  })

export default i18next
代码语言:javascript
复制
//example.js
import React from 'react'

import { useTranslation } from 'react-i18next'

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

    return (
        <div>
            {/* {t("common:greeting")} */}
        </div>
    )
}

export default Greeting

在浏览器中没有加载,并且在控制台中打印这些错误。

代码语言:javascript
复制
[WDS] Disconnected!
Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: ...

我希望在公用文件夹中有区域设置,而不是在资产中,根据文档,最好将代码与翻译分开。我什么都试过了,但我找不到任何解决办法。有人也有类似的问题?

EN

回答 1

Stack Overflow用户

发布于 2022-05-09 21:56:33

您没有定义名称空间,所以loadPath: '/locales/{{lng}}/{{ns}}.json'不会找到{{ns}}.json,因为您说必须找到{t("common:greeting")},您正在搜索的名称空间是common .So,以修复loadPath:‘/locales/{lng}}/common.json’这个更改

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68063441

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档