英文:
i18n localisation not working in nodejs build version
问题
I have a node js project and added the i18n package for localization, and it worked great in dev,
But when I run the same project from the dist folder, all JSON files are empty.
I have a locales
folder in the src folder which contains en_us.json
and de_de.json
.
When I run it after npm build, I see the locales folder with both JSON files, but both are empty.
I think this is an issue related to TypeScript or a directory problem. Please, can anyone check?
Here is the code.
import i18n from 'i18n';
import path from 'path';
import { setLocale } from './config/locale/locale.js';
/** Localization */
const __dirname = path.dirname(new URL(import.meta.url).pathname);
/** Configure localization */
i18n.configure({
locales: ['en_us', 'de_de'],
defaultLocale: 'en_us',
directory: __dirname + '/locales',
});
app.use(i18n.init);
app.use(setLocale);
// routes
app.get('/', async function (req: Request, res: Response) {
res.json({ message: i18n.__('server-running') });
});
app.use(routes);
app.use('*', notFoundHandler);
app.use(errorHandler);
I tried with a different path, and it didn't work.
I am expecting the same JSON files with content to be in the dist folder as well.
英文:
I have a node js project and added the i18n package from for localization and it worked great in dev,
But when I run the same project from dist folder, all JSON files are empty.
I have a locales
folder in src folder which contain `en_us.json` and `dede.json`.
when I run after npm build, I see the locales folder with both JSON file but both are empty.
I think this is an issue related to typescript or directory issue, please can anyone check.
Here is the code.
import i18n from 'i18n';
import path from 'path';
import { setLocale } from './config/locale/locale.js';
/** Localization */
const __dirname = path.dirname(new URL(import.meta.url).pathname);
/** Configure localization */
i18n.configure({
locales: ['en_us', 'de_de'],
defaultLocale: 'en_us',
directory: __dirname + '/locales',
});
app.use(i18n.init);
app.use(setLocale);
// routes
app.get('/', async function (req: Request, res: Response) {
res.json({ message: i18n.__('server-running') });
});
app.use(routes);
app.use('*', notFoundHandler);
app.use(errorHandler);
I tried with a different path and did't work.
I am expecting same JSON file with content should be there in dist folder as well
答案1
得分: 1
你需要将locales的JSON文件复制到dist文件夹才能使其正常工作。
在构建时,您可以添加此过程。
- 添加复制文件的包
npm install --save-dev copyfiles
- 在package.json脚本中添加
"build": "tsc && copyfiles -u 1 -e \"**/*.ts\" \"./src/locales/**/*.json\" \"./dist\" --flatten",
这将把src/locales文件夹中的locales文件复制到dist文件夹中。
英文:
You will have to copy the locales json file in dist folder to make it work.
You could add this process while running the build.
- Add package to copy files
npm install --save-dev copyfiles
- in package.json script add
"build": "tsc && copyfiles -u 1 -e \"**/*.ts\" \"./src/locales/**/*.json\" \"./dist\" --flatten",
this will copy locales folder in src/locales to your dist folder.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论