electron 如何在Quasar 2的电子主过程中使用i18n

c9x0cxw0  于 2022-12-08  发布在  Electron
关注(0)|答案(2)|浏览(269)

我如何在用quasar cli和vite创建的项目的主进程(菜单和尝试)中使用i18 n。
我目前使用的是i18 next和i18 next-fs-backend来实现菜单和托盘。它被配置为使用src/i18 n中渲染进程使用的相同的语言环境文件。这在dev模式下工作,但是prod build显示的是键,而不是翻译。
我将i18 next配置设置为loadPath: "./src/i18n/{{lng}}.json"

toe95027

toe950272#

我是i18next-electron-fs-backend的维护者。尝试以下步骤(在secure-electron-template库中可以找到此包的工作实现)。

导入后端

import i18n from "i18next";
import {
  initReactI18next
} from "react-i18next";
import backend from "i18next-electron-fs-backend";

i18n
  .use(backend)
  .use(initReactI18next)
  .init({
    backend: {
      loadPath: "./app/localization/locales/{{lng}}/{{ns}}.json",
      addPath: "./app/localization/locales/{{lng}}/{{ns}}.missing.json",
      ipcRenderer: window.api.i18nextElectronBackend // important!
    },

    // other options you might configure
    debug: true,
    saveMissing: true,
    saveMissingTo: "current",
    lng: "en"
  });

export default i18n;

更新预加载脚本

const {
    contextBridge,
    ipcRenderer
} = require("electron");
const backend = require("i18next-electron-fs-backend");

contextBridge.exposeInMainWorld(
    "api", {
        i18nextElectronBackend: backend.preloadBindings(ipcRenderer, process)
    }
);

更新主进程

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);
  }
});

我在不久前报告的an issue中发现,在生产环境中,资源没有正确加载,也许我拥有的i18n.mainconfig.js文件可以帮助您进行特定的修复。

i18n.mainconfig.js

const i18n = require("i18next");
const backend = require("i18next-fs-backend");
const whitelist = require("./whitelist");

// On Mac, the folder for resources isn't
// in the same directory as Linux/Windows;
// https://www.electron.build/configuration/contents#extrafiles
const path = require("path");
const isMac = process.platform === "darwin";
const isDev = process.env.NODE_ENV === "development";
const prependPath = isMac && !isDev ? path.join(process.resourcesPath, "..") : ".";

i18n
  .use(backend)
  .init({
    backend: {
      loadPath: prependPath + "/app/localization/locales/{{lng}}/{{ns}}.json",
      addPath: prependPath + "/app/localization/locales/{{lng}}/{{ns}}.missing.json"
    },
    debug: false,
    namespace: "translation",
    saveMissing: true,
    saveMissingTo: "current",
    lng: "en",
    fallbackLng: false, // set to false when generating translation files locally
    supportedLngs: whitelist.langs
  });

module.exports = i18n;

相关问题