I've spent almost whole day on trying to configure i18next translations in electron + typescript. 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".
for boilderplate i've used https://github.com/diego3g/electron-typescript-react
i18n.config file (locales are in same folder and structured: locales/lang/namespace
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:
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:
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. So there is no direct fs (filesystem) access.
Try this i18next backend plugin instead: https://github.com/reZach/i18next-electron-fs-backend