Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catalogsMergePath doesn't merge all the catalogs #1233

Closed
gramalinbechtel opened this issue Apr 19, 2022 · 8 comments · Fixed by #1341
Closed

catalogsMergePath doesn't merge all the catalogs #1233

gramalinbechtel opened this issue Apr 19, 2022 · 8 comments · Fixed by #1341
Assignees
Labels

Comments

@gramalinbechtel
Copy link

gramalinbechtel commented Apr 19, 2022

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.)
@Hashs7
Copy link

Hashs7 commented Apr 22, 2022

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 !

@semoal
Copy link
Contributor

semoal commented Apr 24, 2022

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

@Bertg
Copy link
Contributor

Bertg commented May 9, 2022

@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.

@stale
Copy link

stale bot commented Jul 10, 2022

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 stale bot added the wontfix label Jul 10, 2022
@Bertg
Copy link
Contributor

Bertg commented Jul 11, 2022

Not stale!

@stale stale bot removed the wontfix label Jul 11, 2022
@stale
Copy link

stale bot commented Sep 16, 2022

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 stale bot added the wontfix label Sep 16, 2022
@Bertg
Copy link
Contributor

Bertg commented Sep 16, 2022

Not stale!

@stale stale bot removed the wontfix label Sep 16, 2022
@stale
Copy link

stale bot commented Nov 19, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants