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

Support clearing or overriding locales #221

Open
jekor opened this issue May 1, 2021 · 0 comments
Open

Support clearing or overriding locales #221

jekor opened this issue May 1, 2021 · 0 comments

Comments

@jekor
Copy link

jekor commented May 1, 2021

Once useLocales(...some locales...) is called, calling useLocales([]) retains the existing locales (though I would expect it to override them and force no translation). I haven't tested what happens when a different set of locales is used in the second call.

The use case for this is responding to dynamic changes to the UI language (such as the languagechange event). For example, given a project where untranslated strings are in English:

addLocale('zh-CN', require('./zh-CN.po.json'));
function changeLocale () {
  // Remove any locales after any locales after any English locale to prevent them from overriding the source English text.
  const preferredOverEnglish = ...
  // on the first call:  preferredOverEnglish = ['zh-CN']
  // on the second call: preferredOverEnglish = []
  useLocales(preferredOverEnglish);
}
// To begin, navigator.languages is ['zh-CN', 'en-US']
changeLocale();
window.addEventListener('languagechange', changeLocale);
// Now the user moves en-US to the top of their preferences, navigator.languages is ['en-US', 'zh-CN'], the languagechange event fires, and changeLocale() gets called.
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

No branches or pull requests

1 participant