Replies: 1 comment
-
I forgot that I can use Found this package which picks the most suitable import preferredLocale from 'preferred-locale';
const messages = {
ru: {
Hello: 'Привет',
ru: 'Русский',
en: 'English',
},
en: {
Hello: 'Hello',
ru: 'Русский',
en: 'English',
},
};
const locale = preferredLocale(Object.keys(messages), 'en');
export default createI18n({
fallbackLocale: 'en',
locale, // always 'en' or 'ru'
messages,
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using
navigator.language
to retrieve user locale. Some browsers returns a language code with territory, e.g.ru-RU
. Thanks to the implicit fallback, I can only define messages for two-character locales (ru
).But
useI18n().locale
still returns full locale code (ru-RU
), so I can't use it, for example, to show user their current localeIn this simple case I can just trim locale to two characters (
.slice(0, 2)
). But in more complex cases I will have to re-implement entire fallback decision logic, or define translation for every possible locale.Is there a way to somehow reuse fallback decision logic?
Beta Was this translation helpful? Give feedback.
All reactions