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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

rayz1065
Copy link

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.

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
1 participant