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

Fallback locale in case formatMessage failure #58

Open
sakarisson opened this issue Sep 22, 2020 · 3 comments
Open

Fallback locale in case formatMessage failure #58

sakarisson opened this issue Sep 22, 2020 · 3 comments

Comments

@sakarisson
Copy link

Hey, thanks for a great library!

Currently I'm in the process of converting our ancient, manual i18n logic to something more streamlined, and I think this library seems like a great fit!

I was wondering though, if it would be possible to set a default locale that would be fallen back to in case formatMessage fails when a local key is missing. In our project, we sometimes make releases before all our translators have translated each new key. In this case we want to fall back to English.

For example, let's say we add a new message, and neglect to translate it to German. If we call formatMessage for that key in DE locale, we don't have a German translation. In this case, I'd like to fall back to English. Only if English key wouldn't exist either, should we fall back to rendering the key itself. Is this possible? I didn't find support for this behavior in your documentation.

@joshswan
Copy link
Owner

There isn't currently a locale-based fallback mechanism for formatMessage. That's something that I could potentially explore adding or accept a PR for, though the mechanism for deciding the fallback locale would need to figured out (not everyone will want English).

That said, there are a couple of workarounds that may or may not help:

  • Use the defaultMessage option/prop and set it to the English translation. This doesn't allow formatting/replacements currently (it's just treated as a string), but it would definitely hide the message key.
  • Add the English translations to your German translations object so that all keys are present in the German translations, even if some are in English. You could do this manually or potentially just write a quick function that copies any missing keys from the English object to the German one and do this before even passing the messages to GlobalizeProvider.

@sakarisson
Copy link
Author

sakarisson commented Sep 23, 2020

Thanks for your response

(not everyone will want English).

Definitely. I used English as an example, but in practice the fallback locale would probably be decided from a prop.

Add the English translations to your German translations object so that all keys are present in the German translations, even if some are in English. You could do this manually or potentially just write a quick function that copies any missing keys from the English object to the German one and do this before even passing the messages to GlobalizeProvider.

Yeah, this is basically the workaround that I opted for. It has a bit more steps than my example, but essentially:

  loadMessages({
    de: {
      ...englishMessages,
      ...germanMessages,
    },
  });

For now I'm happy with this solution, as it seems to cover my needs entirely 👍

@joshswan
Copy link
Owner

Sounds good, that does seem like a decent workaround for now.

Per another issue that came up earlier today, a defaultLocale option/prop is now available. Currently it's used when the specified locale isn't available/loaded, but it could also be used in this situation. The problem is that it'd basically require initializing two instances of all the formatters (one with the specified locale/messages and one with the fallback locale/messages). Not sure if that's the best idea so I'll try to give this issue some thought.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants