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

Allow overriding translations #49

Closed
wants to merge 2 commits into from
Closed

Conversation

rayz1065
Copy link
Member

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.

@KnorpelSenf
Copy link
Member

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?

@rayz1065
Copy link
Member Author

rayz1065 commented Sep 11, 2024 via email

@KnorpelSenf
Copy link
Member

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

@rayz1065
Copy link
Member Author

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

Yes absolutely

@rayz1065 rayz1065 closed this Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow overriding translations
2 participants