js-lingui: @lingui/babel-preset-react is probably missing in babel config
Describe the bug
<Trans>elements don’t show in dev- Console prints:
@lingui/babel-preset-react is probably missing in babel config, but you are using <Trans> component in a way which requires it. Either don't use children in <Trans> component or configure babel to load @lingui/babel-preset-react preset. See tutorial for more info: https://l.lingui.io/tutorial-i18n-react - The link in the console message doesn’t point to a tutorial
- Production crashes
To Reproduce
- Fork https://github.com/facebook/create-react-app
- Make app
- Follow https://lingui.js.org/tutorials/react.html#installing-linguijs
- Install cli locally so managing its version across developers is much easier
- find that
babel-plugin-macrosis already enabled in the config: https://github.com/WoWAnalyzer/create-react-app/blob/wowanalyzer-2.0/packages/babel-preset-react-app/create.js#L109
Expected behavior
- I’d expect it to work without
@lingui/babel-preset-reactsince it’s not mentioned in the docs - The link to point to a tutorial
Additional context Add any other context about the problem here.
- jsLingui version
lingui --version2.7.0 - Babel version
npm list babel-corebabel-core@7.0.0-bridge.0 - Your Babel config (e.g.
.babelrc) or framework you use (Create React App, Meteor, etc.) https://github.com/WoWAnalyzer/create-react-app/blob/wowanalyzer-2.0/packages/babel-preset-react-app/create.js (CRA 2.0 with minor changes)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 18 (13 by maintainers)
It used to be like that in early version of LinguiJS but I was afraid that having a global/default i18n instance would lead to errors.
Still, no way to get rid of two imports (runtime vs macro), because macros must be imported from specific module name. At least we could get
i18n.tsyntax back:this would be transformed to:
This is trivial example to keep things simple. We could rename runtime/macro exports to make it clear what is what…
If you want to use a custom instance of
i18n, then you need to create your own./i18n.macro.jsfile as described above.Oh my god, it’s so simple! 😊 Thanks, I’ll try it tonight but it makes total sense.
💡I agree that exporting
Transcomponent from both packages might be confusing. This is something what could be changed in v3.However I don’t mind extra import, because it clearly separates runtime code (
@lingui/react) from build time macros (@lingui/macro). Anything imported from@lingui/reactstays in the runtime code, but macros are processed at build time and replaced with runtimeTranscomponent.It is possible to use
i18nmanager directly, but the problem is that when you change the language, the text won’t be updated.with18nHOC andI18ncomponent readi18nmanager from context and they both trigger re-render when language changes.💡On the other hand, if you don’t mind to do a full page refresh on language change, then you can use
i18nmanager directly. We should definitely document both usages and their differences.I actually think the opposite in this case.
i18n.t'Hello ${name}'was transformed in plugin toi18n._('Hello {name}', { name }). Although it looks simpler, you weren’t aware of such transformation and you got confusing errors in production if you forgot to use Lingui plugins.On the other hand, writing
i18n._(t'Hello ${name}')is transformed intoi18n._({ id: 'Hello {name}', values: { name }})and you can clearly distinguish runtime vs build time code. Especially because you have to importtmacro explicitly to make it work. You see a macro and you know instantly there’s something going on at build time.I’m trying to further simplify it to
i18n(t'Hello')but I think the issue isn’t withi18n._method, but rather the whole syntax right? I’m curious what we could do here to make it easier to understand.Although I planned to support both macros and plugins, I don’t think if I’ll be able to do that given my limited resources. Ideally I would like to have one way how to do it. Let’s keep it open. Macro should be the recommended way, but standalone plugin might be the advanced usage.
Conclusion: Thank you for your feedback, it’s really valuable. Based on feedback from other people, there’s a lot of confusion around macros and I think it may have two reasons: