Reactjs - 加载翻译文件时出错 [英] Reactjs - error loading translation files

查看:30
本文介绍了Reactjs - 加载翻译文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试开始一个新的 React 项目来练习一些技能.

最近我尝试通过添加 react-i18nexti18next 来添加国际化和本地化.

只需遵循

解决方案

所以我认为 o 发现了一个解决方案

我所做的就是添加 copy-webpack-plugin 并将以下代码放入我的 webpack 文件中

const common = {...插件: [...新的复制插件([{ from: "./src/locales", to: "locales";},]),]}

I've been trying to start a new react project to practice some skills.

And recently I tried to add internationalization and localization, by adding react-i18next and i18next.

Simply followed the step by step guide available on https://react.i18next.com and added my translation files.

But when I run the project I get the console error GET https://localhost:8080/locales/en/generic.json 404 (Not Found)

At first I thought that the project was missing a json-loader so I added to the webpack.config.js, but the result is the same. I check the paths and all seems right.

Thanks in advance

i18n file

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

import Backend from 'i18next-http-backend';
import LanguageDetector from 'i18next-browser-languagedetector';

const fallbackLng = ["pt"];
const availableLanguages = ["pt", "en"];

const backendOptions = {
  loadPath: "/locales/{{lng}}/{{ns}}.json",
  crossDomain: true
};


i18n
  // load translation using http
  .use(Backend)
  // detect user language
  .use(LanguageDetector)
  // pass the i18n instance to react-i18next.
  .use(initReactI18next)
  .init({
    backend: backendOptions,
    fallbackLng,
    detection: {
      checkWhitelist: true,
    },
    whitelist: availableLanguages,
    interpolation: {
      escapeValue: false // react already safes from xss
    },
    ns: [
      "generic"
    ],
    defaultNS: "generic",
  });

export default i18n;

root file

import './i18n';
import { I18nextProvider } from 'react-i18next';
import i18n from './i18n';


const App: React.FC = React.memo(() => {
  return (
    <AppContainer>
      <I18nextProvider i18n={i18n}>
        <Router>
          <React.Suspense fallback={<div>Loading...</div>}>
            <MainContainer />
          </React.Suspense>
        </Router>
      </I18nextProvider>
    </AppContainer>
  )
});

ReactDOM.render(<App />, document.getElementById('root'))

Extras

tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist",
    "module": "esnext",
    // Target latest version of ECMAScript.
    "target": "esnext",
    // Search under node_modules for non-relative imports.
    "moduleResolution": "node",
    // Process & infer types from .js files.
    "allowJs": true,
    // Don't emit; allow Babel to transform files.
    "noEmit": true,
    // Enable strictest settings like strictNullChecks & noImplicitAny.
    "strict": true,
    // Disallow features that require cross-file information for emit.
    "isolatedModules": false,
    // Import non-ES modules as default imports.
    "esModuleInterop": true,
    "checkJs": false,
    "skipLibCheck": false,
    "noImplicitAny": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "jsx": "react",
    "lib": [
      "ES6",
      "dom",
      "dom.iterable",
    ],
  },
  "baseUrl": "./",
  "paths": {
    "@assets/*": [
      "src/assets/*"
    ]
  },
  "include": [
    "src",
    "./externals.d.ts",
  ],
  "exclude": [
    "node_modules",
    "webpack.*.js"
  ]
}

解决方案

So I think that o discovered a solution

All I did was add the copy-webpack-plugin and put the following code to my webpack file

const common = {
  ...
  plugins: [
    ...
    new CopyPlugin([
      { from: "./src/locales", to: "locales" },
    ]),
  ]
}

这篇关于Reactjs - 加载翻译文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆