i18n icon indicating copy to clipboard operation
i18n copied to clipboard

Allow overriding translations

Open rayz1065 opened this issue 1 year ago • 3 comments

Closes #48

For some use-cases it would be useful to have the ability to override certain translations programmatically, for example by loading multiple translation files before loading the default translations.

This pull request changes the matchBundles function to allow matching multiple bundles for the same locale before falling back to the default bundle.

Within the bot it is then possible to use a piece of code such as:

function loadLocaleData() {
  if (process.env.LOCALE_OVERRIDES) {
    const overrides = process.env.LOCALE_OVERRIDES.split(',');
    overrides.forEach((override: string) => {
      const directory = path.join(__dirname, 'i18n', override);
      console.log('loading locale override', override);
      i18n.loadLocalesDirSync(directory);
    });
  }

  i18n.loadLocalesDirSync(path.join(__dirname, 'i18n'));
}

When paired with a properly implemented warnings handler it allows properly overriding translations.

{
  fluentOptions: {
    warningHandler: (warning) => {
      if (
        warning.type === TranslateWarnings.MISSING_MESSAGE &&
        !warning.bundle.hasMessage('-override')
      ) {
        // ignore warning on override file
        return;
      }

      defaultWarningHandler(console.log)(warning);
    },
  },
}

Note: the above example assumes a message with key '-override' exists on override files.

rayz1065 avatar Jul 10 '24 10:07 rayz1065

Do I understand this correctly that instead of having a locale and then a default local as fallback, you want to be able to specify a list of locales and then be able to pick a translation from the first locale that has a translation?

KnorpelSenf avatar Sep 11 '24 19:09 KnorpelSenf

Not exactly. I would say that the ideal implementation for this is having multiple files for the same locale. After creating the bundles the library would go in order through the bundles until it finds a translation in the current locale. If no translation is found it would issue a warning and then run through all the different bundles of the fallback locale.

The initial suggestion I've made in this issue is potentially unintuitive and limited in scope, being able to separate translations into multiple files would address this and other use-cases.

On Wed, Sep 11, 2024, 21:53 KnorpelSenf @.***> wrote:

Do I understand this correctly that instead of having a locale and then a default local as fallback, you want to be able to specify a list of locales and then be able to pick a translation from the first locale that has a translation?

— Reply to this email directly, view it on GitHub https://github.com/grammyjs/i18n/pull/49#issuecomment-2344581469, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJAHSZ5L5WE2NQYJ7JQZBNLZWCNR7AVCNFSM6AAAAABKUR3VLKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNBUGU4DCNBWHE . You are receiving this because you authored the thread.Message ID: @.***>

rayz1065 avatar Sep 11 '24 20:09 rayz1065

In other words, this is being superseded by #52?

KnorpelSenf avatar Sep 11 '24 20:09 KnorpelSenf

In other words, this is being superseded by #52?

Yes absolutely

rayz1065 avatar Sep 13 '24 16:09 rayz1065