Add local storage of user settings locale.

This commit is contained in:
Paulo Gustavo Veiga 2022-03-14 23:17:28 -03:00
parent ee3b5e6ea4
commit 75274e5cc8

View File

@ -19,13 +19,21 @@ export class Locale {
} }
export default abstract class AppI18n { export default abstract class AppI18n {
private static LOCAL_STORAGE_KEY = 'user.locale';
public static getUserLocale(): Locale { public static getUserLocale(): Locale {
// @Todo Hack: Try page must not account info. Add this to avoid 403 errors. // @Todo Hack: Try page must not account info. Add this to avoid 403 errors.
const isTryPage = window.location.href.endsWith('/try'); const isTryPage = window.location.href.endsWith('/try');
let result: Locale; let result: Locale;
if (!isTryPage) { if (!isTryPage) {
const account = fetchAccount(); const account = fetchAccount();
result = account?.locale ? account.locale : this.getBrowserLocale(); result = account?.locale ? account.locale : this.getDefaultLocale();
// If the local storage value is different, update ...
if (account?.locale && result.code !== localStorage.getItem(AppI18n.LOCAL_STORAGE_KEY)) {
localStorage.setItem(AppI18n.LOCAL_STORAGE_KEY, result.code);
}
} else { } else {
result = this.getBrowserLocale(); result = this.getBrowserLocale();
} }
@ -47,6 +55,21 @@ export default abstract class AppI18n {
return result; return result;
} }
public static getDefaultLocale(): Locale {
// Fetch local from local storage ...
let result: Locale;
const userLocaleCode: string = localStorage.getItem(AppI18n.LOCAL_STORAGE_KEY);
if (userLocaleCode) {
result = localeFromStr(userLocaleCode);
}
// Ok, use browser default ...
if (!result) {
result = this.getBrowserLocale();
}
return result;
}
} }
export type LocaleCode = 'en' | 'es' | 'fr' | 'de' | 'ru' | 'zh'; export type LocaleCode = 'en' | 'es' | 'fr' | 'de' | 'ru' | 'zh';