Skip to content

Support clearing or overriding locales #221

@jekor

Description

@jekor

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions