lingui / js-lingui

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

Lingui renders string keys #2047

Closed trevcodesjs closed 1 month ago

trevcodesjs commented 1 month ago

Describe the bug When I load Lingui without a message catalog but activate a locale, should it render the default English text or the generated key values?

In my case:

My goal is to avoid loading anything unless the user requires i18n. Is this a valid concern, or should the default behavior be different?

To Reproduce Steps to reproduce the behavior, possibly with minimal code sample, e.g:

  useEffect(() => {
    async function loadMessages() {
      const { messages } = await import(
        /* @vite-ignore */ `../../locales/${language}/messages`
      );
      i18n.activate(language);
    }

    if (loadMessageCatalog) {
      void loadMessages();
    } else {
      i18n.activate(defaultLocale);
    }
  }, [language]);

Expected behavior Expected that the default English strings will render

Additional context

{
    presets: [
      '@babel/preset-react',
      '@linaria',
      ['@babel/preset-env', { corejs: '3.35.1', useBuiltIns: 'usage' }],
      ['@babel/preset-typescript', { optimizeConstEnums: true, allowDeclareFields: true }],
    ],
    plugins: [
      '@babel/plugin-proposal-export-default-from',
      '@babel/plugin-transform-runtime',
      [
        'babel-plugin-import',
        {
          camel2DashComponentName: false,
          libraryDirectory: '',
          libraryName: '@mui/material',
        },
        'core',
      ],
      [
        'babel-plugin-import',
        {
          camel2DashComponentName: false,
          libraryName: '@mui/icons-material',
          libraryDirectory: '',
        },
        'icons',
      ],
      [require.resolve('babel-plugin-module-resolver'), resolve],
      'macros',
    ].filter(Boolean),
  };
timofei-iatsenko commented 1 month ago

You need to load catalog in any case. Even if user is on a "default" language. The Lingui is trying to make i18n footprint as small as possible. It drops default messages from the bundle in production builds. Simply image if you will not drop default messages and you switched to ES, user will have to load EN + ES messages. With current lingui strategy, it loads only messages for the one language.

Regarding differences between SWC / babel, there shouldn't be any differences, both of them dropping non-essential props during production builds. The inconsistencies rather related to the setup of this tooling and do they receive NODE_ENV properly.