node.jselectronelectron-builder

electron-builder : remove locales from production in windows platform


From this link:
https://www.electron.build/configuration/configuration

It says we have this option:

electronLanguages Array | String - The electron locales to keep. By default, all Electron locales used as-is.

But apparently electronLanguages just works for mac platform(As is it in this boilerplate)

But no matter where I put this option, this option does not work for win platform:

  "build": {
    "appId": "com.electron.electron-serial",
    "electronLanguages": ["en_US"], // I tested here
    "win": {
      "target": [
        "msi"
      ],
      "icon": "./icon/logo.png",
    "electronLanguages": ["en_US"], // I tested here
    },
    "msi": {
      "oneClick": false,
      "createDesktopShortcut": true,
      "perMachine": true,
      "electronLanguages": ["en_US"], // I tested here
    }
  },
  "electronLanguages": ["en_US"], // I tested here

Solution

  • win platform does not support electronLanguages option. I found this work around in this issue:

    When using electron-builder, you can default to the locale en-US via app.commandLine.appendSwitch('lang', 'en-US'); // before app.on('ready', ...)
    and remove the unused locales from your electron project via the "afterPack" hook in the package.json
    "build": { "afterPack": "./removeLocales.js", ... }
    which could look like

    //https://www.electron.build/configuration/configuration#afterpack
    exports.default = async function(context) {   //console.log(context)
      var fs = require('fs');     var localeDir =
    context.appOutDir+'/locales/';
    
      fs.readdir(localeDir, function(err, files){         //files is array of
    filenames (basename form)         if(!(files && files.length)) return;        for
    (var i = 0, len = files.length; i < len; i++) {           var match =
    files[i].match(/en-US\.pak/);             if(match === null){
                  fs.unlinkSync(localeDir+files[i]);          }       }   }); }