What you were expecting:
Set initial locale with value from localStorage.getItem('locale_language');
instead of 'en'
.
import polyglotI18nProvider from 'ra-i18n-polyglot'; // react-admin
const i18nProvider = polyglotI18nProvider(locale => {
if (locale === 'ru') {
return import('./i18n/ru').then(messages => messages.default);
}
return englishMessages;
}, 'en');
What happened instead:
But when I do this there is error Error: The i18nProvider returned a Promise for the messages of the default locale (ru). Please update your i18nProvider to return the messages of the default locale in a synchronous way.
Related code:
There is how I get value from localStore
export function getLocalLanguage(): string {
let defaultLocalLanguage = 'en';
const storedLanguage = localStorage.getItem('locale_language');
if (storedLanguage !== null) {
defaultLocalLanguage = storedLanguage;
}
return defaultLocalLanguage;
}
There is how I set initial local for polyglotI18nProvider
const i18nProvider = polyglotI18nProvider(locale => {
if (locale === 'ru') {
return import('./i18n/ru').then(messages => messages.default);
}
return englishMessages;
}, getLocalLanguage());
Here is said localStorage is syncronous so there should be no problem then?
resolveBrowserLocale
may be an alternative, but I need a more persistent way to store language.