简体   繁体   English

Electron + typescript + i18next 问题 - i18next::backendConnector [...] fs.readFile 不是 function

[英]Electron + typescript + i18next problem - i18next::backendConnector [...] fs.readFile is not a function

I've spent almost whole day on trying to configure i18next translations in electron + typescript.我几乎一整天都在尝试在 electron + typescript 中配置 i18next 翻译。 I actually have no idea what can i do with it to make it work.我实际上不知道我能用它做什么来让它工作。 I always get the following error: "i18next::backendConnector: loading namespace translation for language en failed TypeError: fs.readFile is not a function".我总是收到以下错误:“i18next::backendConnector: loading namespace translation for language en failed TypeError: fs.readFile is not a function”。

for boilderplate i've used https://github.com/diego3g/electron-typescript-react对于样板,我使用了 https://github.com/diego3g/electron-typescript-react

i18n.config file (locales are in same folder and structured: locales/ lang / namespace i18n.config 文件(语言环境在同一个文件夹中并且结构为:语言环境/语言/命名空间

import i18next, { InitOptions } from 'i18next'
import backend from 'i18next-fs-backend'
import { initReactI18next } from 'react-i18next'

export const DEFAULT_LANGUAGE = 'en'
export const DEFAULT_NAMESPACE = 'common'

const i18nOptions: InitOptions = {
  lng: DEFAULT_LANGUAGE,
  fallbackLng: DEFAULT_LANGUAGE,
  defaultNS: DEFAULT_NAMESPACE,
  fallbackNS: DEFAULT_NAMESPACE,
  interpolation: {
    escapeValue: false,
  },
  debug: true,
  backend: {
    loadPath: './locales/{{lng}}/{{ns}}.json',
    addPath: './locales/{{lng}}/{{ns}}.missing.json',
  },
}

i18next.use(backend).use(initReactI18next).init(i18nOptions)

export default i18next

webpack config: webpack 配置:

const path = require('path')
var i18nextPlugin = require('ya-i18next-webpack-plugin').default

module.exports = {
  resolve: {
    extensions: ['.ts', '.tsx', '.js'],
    modules: [path.resolve(__dirname, '../src'), 'node_modules'],
    fallback: {
      fs: false,
    },
  },
  module: {
    rules: require('./rules.webpack'),
  },
  plugins: [
    new i18nextPlugin({
      defaultLanguage: 'en',
      defaultNamespace: 'common',
      languages: ['en', 'pl'],
      functionName: '_t',
      resourcePath: '..public/locales/{{lng}}/{{ns}}.json',
    }),
  ],
}

i18n initialization: i18n 初始化:

import ReactDOM from 'react-dom'
import App from './app'
import { i18n } from './config'

i18n.init()

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

In electron you need to use IPC (inter-process-communication) to request a file be read or written from the electron's main process.在 electron 中,您需要使用 IPC(进程间通信)来请求从电子的主进程读取或写入文件。 So there is no direct fs (filesystem) access.所以没有直接的 fs(文件系统)访问。

Try this i18next backend plugin instead: https://github.com/reZach/i18next-electron-fs-backend试试这个 i18next 后端插件: https://github.com/reZach/i18next-electron-fs-backend

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM