|
|
|
@ -1,10 +1,11 @@
|
|
|
|
|
import { Locale } from 'date-fns';
|
|
|
|
|
import { CrowdinLocale } from '../../localization/constants';
|
|
|
|
|
import { en } from '../../localization/locales';
|
|
|
|
|
import type { LocalizerDictionary } from '../../types/localizer';
|
|
|
|
|
import { timeLocaleMap } from './timeLocaleMap';
|
|
|
|
|
|
|
|
|
|
let mappedBrowserLocaleDisplayed = false;
|
|
|
|
|
let initialLocale: Locale | undefined;
|
|
|
|
|
|
|
|
|
|
let crowdinLocale: CrowdinLocale | undefined;
|
|
|
|
|
let translationDictionary: LocalizerDictionary | undefined;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -12,7 +13,7 @@ let translationDictionary: LocalizerDictionary | undefined;
|
|
|
|
|
*/
|
|
|
|
|
export function resetLocaleAndTranslationDict() {
|
|
|
|
|
translationDictionary = undefined;
|
|
|
|
|
initialLocale = undefined;
|
|
|
|
|
crowdinLocale = undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -46,32 +47,34 @@ export function i18nLog(message: string) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type Locale = keyof typeof timeLocaleMap;
|
|
|
|
|
|
|
|
|
|
export function getTimeLocaleDictionary() {
|
|
|
|
|
return timeLocaleMap[getLocale()];
|
|
|
|
|
return (timeLocaleMap as Record<string, Locale>)[getBrowserLocale()] || timeLocaleMap.en;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the current locale.
|
|
|
|
|
* Returns the current locale as supported by Session (i.e. one generated by crowdin)
|
|
|
|
|
*/
|
|
|
|
|
export function getLocale(): Locale {
|
|
|
|
|
if (!initialLocale) {
|
|
|
|
|
i18nLog(`getLocale: using initialLocale: ${initialLocale}`);
|
|
|
|
|
export function getCrowdinLocale(): CrowdinLocale {
|
|
|
|
|
if (!crowdinLocale) {
|
|
|
|
|
i18nLog(`getCrowdinLocale: ${crowdinLocale}`);
|
|
|
|
|
|
|
|
|
|
throw new Error('initialLocale is unset');
|
|
|
|
|
throw new Error('crowdinLocale is unset');
|
|
|
|
|
}
|
|
|
|
|
return initialLocale;
|
|
|
|
|
return crowdinLocale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the closest supported locale by the browser.
|
|
|
|
|
*/
|
|
|
|
|
export function getBrowserLocale() {
|
|
|
|
|
const userLocaleDashed = getLocale();
|
|
|
|
|
const browserLocale = process.env.LANGUAGE || getCrowdinLocale() || 'en';
|
|
|
|
|
|
|
|
|
|
// supportedLocalesOf will throw if the locales has a '_' instead of a '-' in it.
|
|
|
|
|
const userLocaleDashed = browserLocale.replaceAll('_', '-');
|
|
|
|
|
|
|
|
|
|
const matchinglocales = Intl.DateTimeFormat.supportedLocalesOf(userLocaleDashed);
|
|
|
|
|
const mappingTo = matchinglocales?.[0] || 'en';
|
|
|
|
|
const matchingLocales = Intl.DateTimeFormat.supportedLocalesOf(userLocaleDashed);
|
|
|
|
|
|
|
|
|
|
const mappingTo = matchingLocales?.[0] || 'en';
|
|
|
|
|
|
|
|
|
|
if (!mappedBrowserLocaleDisplayed) {
|
|
|
|
|
mappedBrowserLocaleDisplayed = true;
|
|
|
|
@ -81,16 +84,16 @@ export function getBrowserLocale() {
|
|
|
|
|
return mappingTo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function setInitialLocale(locale: Locale, dictionary: LocalizerDictionary) {
|
|
|
|
|
if (translationDictionary) {
|
|
|
|
|
throw new Error('setInitialLocale: translationDictionary or initialLocale is already init');
|
|
|
|
|
export function setInitialLocale(crowdinLocaleArg: CrowdinLocale, dictionary: LocalizerDictionary) {
|
|
|
|
|
if (translationDictionary || crowdinLocale) {
|
|
|
|
|
throw new Error('setInitialLocale: translationDictionary or crowdinLocale is already init');
|
|
|
|
|
}
|
|
|
|
|
translationDictionary = dictionary;
|
|
|
|
|
initialLocale = locale;
|
|
|
|
|
crowdinLocale = crowdinLocaleArg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isLocaleSet() {
|
|
|
|
|
return initialLocale !== undefined;
|
|
|
|
|
export function isSessionLocaleSet() {
|
|
|
|
|
return !!crowdinLocale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getStringForCardinalRule(
|
|
|
|
|