Closed RobbeCl closed 1 year ago
__dirname
is CJS only while import.meta.url
is ESM only. So i18n would need some transpiler to support both package types... eventually there'll be major release for ESM only dropping CJS support as proposed by https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
For now you might try with explicitly setting directory
instead of falling back to defaults, ie.:
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import { I18n } from 'i18n';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const i18n = new I18n({
locales: ['en', 'de'],
directory: path.join(__dirname, 'locales')
});
now you get back full control about your locales directory setting
Thanks for the response. I'll wait for the transition to ESM
actually node (at least v18+) works as expected and resolves directory
to ./locales
relative to i18n.js
- no matter if I try with .cjs or .mjs or set "type": "module"
in package.json.
Leaving directory
to default has never been a good choice as it will always use that node_modules/i18n/locales
path which won't be part of any modern deployment strategy.
Sites/tmp/i18n-esm is π¦ v0.0.0 via ξ v18.12.1
β― tree node_modules/i18n/ -L 2
node_modules/i18n/
βββ LICENSE
βββ README.md
βββ SECURITY.md
βββ i18n.js
βββ index.js
βββ locales
βΒ Β βββ de.json
βΒ Β βββ en.json
βββ node_modules
βΒ Β βββ mustache
βββ package.json
3 directories, 8 files
So, you should always set directory
anyways
Well, the problem I get that I get errors when importing i18n because of this line: 'https://github.com/mashpie/i18n-node/blob/v0.15.0/i18n.js#L125'
do you have a reproduction repo? I just tried both https://github.com/mashpie/i18n-esm-cjs-example and don't get any errors
same issue
/** Localization */
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/** Configure localization */
i18n.configure({
locales: ['en_us', 'de_de'],
defaultLocale: 'en_us',
directory: path.join(__dirname, 'locales'),
});
app.use(i18n.init);
app.use(setLocale);
// routes
app.get('/', async function (req: Request, res: Response) {
res.json({ message: i18n.__('server-running') });
});
above code works in dev, not in prod. jsons file are empty
works in dev, not in prod. jsons file are empty
so, where do dev and prod differ?
works in dev, not in prod. jsons file are empty
so, where do dev and prod differ?
when I run dev tsc && ts-node index.ts
, It is taking the proper locale path (src/locales).
But when I run npm run build
locales folder is not copy in the dist folder hence run dist/index.js
also didn't work.
So I end up using copyfiles package to copy locales folder in dist folder while building
Well, any kind of build step will most probably run within a different env than the resulting script. Same applies to dynamic values of __dirname or import.meta. So you canβt really rely on them. Instead, you should treat the directory setting like any other config and set that explicitly.
It is not possible to use this package in an ES module because of the use of
__dirname
in the code: https://github.com/mashpie/i18n-node/blob/v0.15.0/i18n.js#L125. What it be possible to use another approach?Or export a file that can be used by a ES module project