相关文章推荐
完美的消炎药  ·  c# - HRESULT: ...·  2 年前    · 
淡定的跑步鞋  ·  right click vs ...·  2 年前    · 
开朗的海豚  ·  android - How to know ...·  2 年前    · 

npm i i18next-electron-fs-backend

Add into your i18next config

Based on documentation for a i18next config , import the backend.

import i18n from "i18next";
import {
  initReactI18next
} from "react-i18next";
import backend from "i18next-electron-fs-backend";
  .use(backend)
  .use(initReactI18next)
  .init({
    backend: {
      loadPath: "./app/localization/locales/{{lng}}/{{ns}}.json",
      addPath: "./app/localization/locales/{{lng}}/{{ns}}.missing.json",
      contextBridgeApiKey: "api" // needs to match first parameter of contextBridge.exposeInMainWorld in preload file; defaults to "api"
    // other options you might configure
    debug: true,
    saveMissing: true,
    saveMissingTo: "current",
    lng: "en"
  });
export default i18n;

Update your preload.js script

const {
    contextBridge,
    ipcRenderer
} = require("electron");
const backend = require("i18next-electron-fs-backend");
contextBridge.exposeInMainWorld(
    "api", {
        i18nextElectronBackend: backend.preloadBindings(ipcRenderer, process)

Update your main.js script

const {
  app,
  BrowserWindow,
  session,
  ipcMain
} = require("electron");
const backend = require("i18next-electron-fs-backend");
const fs = require("fs");
let win;
async function createWindow() {  
  win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      contextIsolation: true,
      preload: path.join(__dirname, "preload.js")
  });
  backend.mainBindings(ipcMain, win, fs); // <- configures the backend
  // ...
app.on("ready", createWindow);
app.on("window-all-closed", () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== "darwin") {
    app.quit();
  } else {
    i18nextBackend.clearMainBindings(ipcMain);
});

Options

These are options that are configurable, all values below are defaults.

debug: false, // If you'd like to show diagnostic messages loadPath: "/locales/{{lng}}/{{ns}}.json", // Where the translation files get loaded from addPath: "/locales/{{lng}}/{{ns}}.missing.json" // Where the missing translation files get generated