js-lingui icon indicating copy to clipboard operation
js-lingui copied to clipboard

catalogsMergePath doesn't merge all the catalogs

Open gramalinbechtel opened this issue 2 years ago • 7 comments

Describe the bug catalogsMergePath doesn't merge all the catalogs

To Reproduce Define multiple catalogs in linguiRc and specify catalogsMergePath

Expected behavior The catalogs must be merge to a single .po file

Additional context Add any other context about the problem here.

https://github.com/lingui/js-lingui/blob/main/packages/cli/src/lingui-compile.ts#L83 image

  • jsLingui version 3.13.2
  • Babel version 7.17.5
  • Your Babel config (e.g. .babelrc) or framework you use (Create React App, Meteor, etc.)

gramalinbechtel avatar Apr 19 '22 18:04 gramalinbechtel

I had the same problem here. 🤷‍♂️

I've tried to solve this in my NextJS project like this : I created nodeJS file to compile each sub-folders in my locales files.

# lingui-compiler.js
const glob = require('glob');
const { promises: { readdir } } = require('fs');
const fs = require('fs');

const isProduction = process.env.NODE_ENV === 'production';

/**
 * Get all files from locale folder
 * Merge all file in one object
 * And write {locale}.js file
 * @param locale
 * @returns {Promise<void>}
 */
const mergeLocalesTranslations = async (locale) => {
  await glob(`**/translations/${ locale }/**/*.js`, {
    ignore: [ `**/translations/${ locale }/${ locale }.js` ]
  }, async (er, files) => {
    const data = await Promise.all(files.map(async (file) => (require(`../${ file }`)).messages));
    const mergedTranslations = data.reduce((acc, cur) => ({
      ...acc,
      ...cur
    }), {});
    const fileContent = `/*eslint-disable*/module.exports=${ JSON.stringify(mergedTranslations, null, 0) };`;
    fs.writeFileSync(`./src/translations/${ locale }/${ locale }.js`, fileContent, 'utf-8');
  });
};


/**
 * Get all translations directories
 * and compile for each locale
 * source translations in one file
 * @param source
 * @returns {Promise<Promise<{}>[]>}
 */
const compile = async (source) => (await readdir(source, { withFileTypes: true }))
  .filter((dirent) => (dirent.isDirectory() && isProduction ? dirent.name !== 'pseudo' : true))
  .map(async (dirent) => mergeLocalesTranslations(dirent.name));

// where your lingui extract your locales
compile('./src/translations');

Add glob to dev dependencies

yarn add -D glob

Update lingui compile script in package

# package.json
"scripts": {
    "lang:extract": "NODE_ENV=development lingui extract --clean",
    "lang:compile": "lingui compile && node lingui-compile.js"
  },

And this is what my lingui config file looks like

# lingui.config.js
module.exports = {
  locales: [ 'fr', 'nl', 'pseudo' ],
  pseudoLocale: 'pseudo',
  sourceLocale: 'fr',
  fallbackLocales: {
    default: 'fr'
  },
  catalogs: [
    {
      path: 'src/translations/{locale}/pages/{name}',
      include: [ 'src/pages/{name}' ],
      exclude: [ '**/_app.js' ]
    },
    {
      path: 'src/translations/{locale}/components/{name}',
      include: [ 'src/components/{name}' ]
    }
  ],
  format: 'minimal'
};

Then after running lang:compile command, you can import only one file in /translations/fr/fr.js for example

Not ideal but I hope it's help !

Hashs7 avatar Apr 22 '22 15:04 Hashs7

Yep, you're right is something I want to find time to work with, for improving a lot the monorepo experience. Will accept contributions on this one, if someone is interested

semoal avatar Apr 24 '22 15:04 semoal

@semoal How would you expect the merge functionality to work, when multiple files would be defining the exact same msgid? By defining separate catalogues this situation can happen easily, and my be desired (different translations for the same key in different components).

Just writing the merge code, is one thing, but defining this behaviour should be done first.

Bertg avatar May 09 '22 11:05 Bertg

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 10 '22 01:07 stale[bot]

Not stale!

Bertg avatar Jul 11 '22 17:07 Bertg

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 16 '22 00:09 stale[bot]

Not stale!

Bertg avatar Sep 16 '22 08:09 Bertg

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 19 '22 18:11 stale[bot]