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

Use lingui.js with multiple packages #1157

Closed
sitole opened this issue Oct 26, 2021 · 14 comments · Fixed by #1469
Closed

Use lingui.js with multiple packages #1157

sitole opened this issue Oct 26, 2021 · 14 comments · Fixed by #1469

Comments

@sitole
Copy link

sitole commented Oct 26, 2021

Hey,
four our frontend we are using monrepository architecture.
We have split codebase per application and library (ui, utils..).

So, now we want to implement Lingui in our UI package. So we want prepare some React components separately with and then use this components in applications. It works well expect translations. We have installed Lingui in this packages and also works well. Its no problem to compile this library. Library is written in TypeScript and React.

But, after this i want use some component in application. If i paster msgid with translation directly into application po file also everything works good and component is translated. But after this i want to run lingui extract for example because i added some another translation to app. But msgid used in external component is not found and its marked as unused and commented.

I know Lingui searching components only in defined paths, but its possible to force Lingui to extract this translations from another package/node_modules folder etc?

Out current configuration

{
  "fallbackLocales": {
    "default": "en"
  },
  "sourceLocale": "en",
  "format": "po",
  "locales": ["en", "cs", "es"],
  "catalogs": [
    {
        path: "src/locales/{locale}/messages",
        include: [
            "<rootDir>/src/**/*"
        ]
    }
  ],
}
@sitole
Copy link
Author

sitole commented Oct 27, 2021

For now, we decided to use our own path.
We have EnglishMessages.ts files with simple object {myExampleMessageId: 'Translation', ...}
and then this file is merged with standard output from Lingui.js in application:

const options = {
  en: { ...catalogEN.messages, ...EnglishMessages },
  es: { ...catalogES.messages, ...EnglishMessages },
}

i18n.loadLocaleData(lang, {});
i18n.load(options);
i18n.activate(lang);

So now we can use components from the UI package.
A negative side effect of this solution is that we are not able to extract translations directly using lingui extract from the UI package, but this issue can be solved later.

@semoal
Copy link
Contributor

semoal commented Nov 26, 2021

This is something I want to explore for a long time, how Lingui works with monorepo of UI's and how can these translations co-exist together.
Will try to get some free time and create a repo where we can play and update Lingui to work nicely with monorepos.

@renchap
Copy link
Contributor

renchap commented Dec 10, 2021

I am having a similar issue: In a monorepo, I have both a web React app, a react-native mobile app, and a shared library with some common code.

I am not sure how to properly handle this, as:

  • we have many common strings between the web & mobile apps, so it would be great to only translate them once
  • we want to have some strings in the shared library translated
  • we do not want to ship mobile-only string into our web-app, and the other way around. Strings in the shared library should be in both

Any ideas?

The best option I am thinking about is extracting strings for each project independently, then importing and merging mobile/locale.js + ``shared/locale.jsin the mobile app, andweb/locale.js` + `shared/locale.js` in the web app, but strings need to be translated in each project independently as we get 3 `.po` files.

@Hideman85
Copy link

I having the same issue and I'm interesting to know what would be your best practice on it.

I guess it is not that easy to architecture that and I'm having the exact same questioning as @renchap

  • How to split the translations where they belong and how to avoid translating multiple times the same entry?
  • If we think of merging same entry translated multiple times we could deal with conflict (different trans for saying the similar thing).
  • I'm wondering, maybe lingui could aggregate the different translations and treat each entry as an import then we can use the unused code elimination of webpack (or any bundler)

@stale
Copy link

stale bot commented Mar 12, 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 Mar 12, 2022
@stale stale bot closed this as completed Mar 19, 2022
@sitole
Copy link
Author

sitole commented Mar 21, 2022

Still actual?

@landsman
Copy link
Contributor

landsman commented Aug 9, 2022

I would love to see this in progress. Is it possible?

@semoal
Copy link
Contributor

semoal commented Aug 9, 2022

Ysss, I'll take care of this as soon as possible!

@semoal semoal reopened this Aug 9, 2022
@stale stale bot removed the wontfix label Aug 9, 2022
@timofei-iatsenko
Copy link
Collaborator

I will add a bit from my experience. The built-in Angular i18n tooling solve this problem effectively.

Instead of globbing folders, they wrote a plugin for bundler (webpack). It is webpack's responsibility to discover and parse all files used in the current application. So while webpack is bundling your actual application, plugin extract the messages from a code.

Instead of manually pointing extractor to places where msgs potentially might be such as:

monorepo/shared-ui1/src
monorepo/shared-ui2/src
monorepo/shared-ui3/src

You point extractor to your entry points:

monorepo/web-app1/src/main.ts
monorepo/mobile-app1/src/main.ts
monorepo/desktop-app1/src/main.ts

Then extractor automatically (thanks to webpack) follow all imports and discover all files needed for extraction. This effectively solved the issue even with packages which are installed from npm.

The drawbacks of this are slower extraction process and more complicated setup. The plus of it, you can use your existing webpack setup with all yours aliases/path mappings setup.

After such extracting every app in monorepo would have it's own catalog of messages. The web app would not have messages from native and vice versa. Then localization platform will do the rest for you (reuse translation for the same keys)

@stale
Copy link

stale bot commented Oct 29, 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 Oct 29, 2022
@mgrosperrin
Copy link

Up

@stale stale bot closed this as completed Nov 9, 2022
@mgrosperrin
Copy link

Why has this issue been closed? Nothing has been done yet and the latest status is that @semoal will work on it when he can.

@andrii-bodnar
Copy link
Contributor

@thekip can this issue be resolved by the new experimental extractor feature?

@timofei-iatsenko
Copy link
Collaborator

Yes. Attach it to here #1458

@andrii-bodnar andrii-bodnar linked a pull request Mar 13, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants