mashpie / i18n-node

Lightweight simple translation module for node.js / express.js with dynamic json storage. Uses common __('...') syntax in app and templates.
MIT License
3.08k stars 419 forks source link

When i18n is packaged into exe cannot be used, i18n directory cannot find the files under the directory path. #499

Closed Yomisana closed 2 years ago

Yomisana commented 2 years ago
Hello everyone, When I am debugging, the software can read my language files normally, but after I package it, it seems that it cannot be read because locales folder are in asar? But the error messages all point to the i18n package, so I want to ask this How to deal with it? error picture: https://dev.yomisana.xyz/20220714_code_error_screenshot :shipit: default asar file asar file(use asarmor) unpack asar(folder name: app.asar)
status :x: :x: :heavy_check_mark:

❌ can't work ✔️ can work

project tree (electron)

Mjolnir LeagueLite
├─ .gitignore
├─ index.js
├─ LICENSE
├─ package.json
├─ preload.js
└─ src
    ├─ core
    │ ├─ global.js
    │ ├─ i18n.config.js
    │ └─ i18n.js (set restore user language)
    ├─ locales
    │ ├─ en.json
    │ └─ en-TW.json
    └─ resource
       ├─ css
       ├─ html
       ├─ img
       └─ plugin

i18n.config.js

const { I18n } = require('i18n');
const path = require('path');

const i18n = new I18n({
   locales: ['en', 'zh-TW'],
   defaultLocale: 'en',
   autoReload: true,
   extension: '.json',
   directory: path.join('./src', 'locales')
});

module.exports = i18n;
mashpie commented 2 years ago

i18n-node was build for server side node.js usage. While it could work in a packaged electron app, there might be other issues as well. That been said, my guess is that the "path" to your locales will be different in the packed app than in dev mode.

To come around that, you might use https://github.com/mashpie/i18n-node#some-words-on-staticcatalog-option to statically feed i18n with locales at the build step. Unfortunately I don't have any example on how to achieve that with electron (and ist various build methods).

Instinctively I'd choose another i18n framework that is more frontend-related or officially supporting electron, like https://www.i18next.com/ (as far as I get it)

Yomisana commented 2 years ago

I just tried using the staticCatalog option as you said and it works! Oh! Thanks for letting me know this is for server side node.js .Using it currently works I like the format too. :heart:

mashpie commented 2 years ago

glad to hear 👍