i18n本地化在Node.js构建版本中不起作用。

huangapple go评论51阅读模式
英文:

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文件夹才能使其正常工作。
在构建时,您可以添加此过程。

  1. 添加复制文件的包
npm install --save-dev copyfiles
  1. 在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.

  1. Add package to copy files
npm install --save-dev copyfiles
  1. 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.

huangapple
  • 本文由 发表于 2023年4月13日 19:54:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定