lingui / js-lingui

🌍 📖 A readable, automated, and optimized (3 kb) internationalization for JavaScript
https://lingui.dev
MIT License
4.6k stars 382 forks source link

Allow fallback to a messages defined in the code without compilation #2043

Open timofei-iatsenko opened 1 month ago

timofei-iatsenko commented 1 month ago

ToDo: motivation, related issues

Tasks:

  1. Make configurable strip_non_essential_fields in the SWC plugin from outside. Now it's hardcoded to the ENV variable

    • or introduce new option supress_stripping_message, which will allow stripping context and comment but left message.
  2. Make the same options for Babel macro plugin

  3. Make possible to register MessageCompiler in production, PR: https://github.com/lingui/js-lingui/pull/2035

  4. Write the docs describing all configuration to achieve that.

ilyausorov commented 1 month ago

Would really appreciate supress_stripping_message because right now I'm having to patch this line in the @lingui/macro package const stripNonEssentialProps = false; // process.env.NODE_ENV == "production" && !opts.extract; to keep the message values available so I can do something like this

const SOME_TEXT = msg`Some text`;

const object = {
       originalValue: SOME_TEXT.message,
       translatedValue: _(SOME_TEXT)
}
timofei-iatsenko commented 1 month ago

You don't need to patch, this is a configurable parameter. You can either use pre-release v5 version where macro is extracted to separate babel plugin and parameters could be passed directly to it, or use babel-plugin-macros configuration as specified there: https://github.com/kentcdodds/babel-plugin-macros/blob/main/other/docs/user.md#config use lingui for macro name

// babel-plugin-macros.config.js
module.exports = {
  // ...
  // Other macros config
  lingui: {
    extract: true,
  },
}
ilyausorov commented 1 month ago

hey @timofei-iatsenko, thanks for this info. Just curious, is using the babel-plugin-macros configuration for lingui and specifically that the extract property is an available property, documented anywhere in the documentation? I feel like I thoroughly reviewed it, and I didn't see this before.

timofei-iatsenko commented 1 month ago

It's not documented, it is used internally for extractor. Later I will create a documented parameter intended for public use as described here in the first message. But you can safely use this "extract" param, because it's not going to be changed any time soon.

ilyausorov commented 1 month ago

Hmm I couldn't get it to work but maybe thats because I'm also in a monorepo and it's react native. I'll leave my patch in for now until you get the v5 fully released and tested 🙏