You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Windows平台通过electron-builder移除生产环境多余Locale?

在Windows平台通过electron-builder移除多余Electron Locale的解决方案

electron-builder官方文档中的electronLanguages配置项目前仅对Mac平台生效,Windows平台无法通过该配置直接过滤Locale。要在Windows下移除生产环境中的多余Locale,可通过打包钩子+手动文件清理的方式实现,具体步骤如下:

步骤1:配置afterPack钩子

在项目的package.json的build配置中,添加afterPack钩子,指定执行清理逻辑的脚本文件:

"build": {
  "appId": "com.electron.electron-serial",
  "win": {
    "target": ["msi"],
    "icon": "./icon/logo.png"
  },
  "msi": {
    "oneClick": false,
    "createDesktopShortcut": true,
    "perMachine": true
  },
  "hooks": {
    "afterPack": "./scripts/clean-locales.js"
  }
}

步骤2:编写Locale清理脚本

在项目根目录创建scripts/clean-locales.js文件,编写以下清理逻辑,仅保留指定的Locale文件:

const fs = require('fs');
const path = require('path');

module.exports = async (context) => {
  // 定位Windows打包后Electron的Locale目录
  const localesDir = path.join(context.appOutDir, 'resources', 'electron', 'locales');
  // 定义需要保留的Locale文件(根据需求调整)
  const keepLocales = ['en-US.pak', 'en-US.lproj'];

  if (!fs.existsSync(localesDir)) return;

  const allFiles = fs.readdirSync(localesDir);
  allFiles.forEach(file => {
    if (!keepLocales.includes(file)) {
      const filePath = path.join(localesDir, file);
      const stats = fs.statSync(filePath);
      // 删除文件或文件夹
      if (stats.isDirectory()) {
        fs.rmSync(filePath, { recursive: true, force: true });
      } else {
        fs.unlinkSync(filePath);
      }
    }
  });
  console.log(`已完成Locale清理,仅保留:${keepLocales.join(', ')}`);
};

注意事项

  1. 路径验证:不同Electron版本的Locale目录路径可能略有差异,打包后可先查看appOutDir对应的输出目录,确认Locale文件的实际位置后调整脚本中的路径。
  2. 必要文件保留:除了.pak格式的Locale文件,部分场景下需要保留对应的.lproj文件夹,避免Electron运行时出现语言加载异常。
  3. 钩子执行时机:afterPack钩子在打包完成、生成最终安装包前执行,此时修改打包后的文件会被包含到最终的安装包中。

内容的提问来源于stack exchange,提问作者Ali Shefaee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 04:45:19