You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-desktop/ts/util/i18n/shared.ts

92 lines
2.8 KiB
TypeScript

import { Locale } from 'date-fns';
import { CrowdinLocale } from '../../localization/constants';
import { timeLocaleMap } from './timeLocaleMap';
let mappedBrowserLocaleDisplayed = false;
let crowdinLocale: CrowdinLocale | undefined;
/**
* Logs an i18n message to the console.
* @param message - The message to log.
* @deprecated we want to use a normal logger instead of this function, eventually.
*/
export function i18nLog(message: string) {
if (typeof window !== 'undefined') {
// eslint-disable-next-line no-console
(window?.log?.error ?? console.log)(`i18n: ${message}`);
} else {
// eslint-disable-next-line no-console
console.log(`i18n: ${message}`);
}
}
export function getTimeLocaleDictionary() {
return (timeLocaleMap as Record<string, Locale>)[getBrowserLocale()] || timeLocaleMap.en;
}
/**
* Returns the current locale as supported by Session (i.e. one generated by crowdin)
*/
export function getCrowdinLocale(): CrowdinLocale {
if (!crowdinLocale) {
i18nLog(`getCrowdinLocale: ${crowdinLocale}`);
throw new Error('crowdinLocale is unset');
}
return crowdinLocale;
}
/**
* Returns the closest supported locale by the browser.
*/
export function getBrowserLocale() {
const browserLocale = process.env.LANGUAGE || getCrowdinLocale() || 'en';
// supportedLocalesOf will throw if the locales has a '_' instead of a '-' in it.
const userLocaleDashed = browserLocale.replaceAll('_', '-');
try {
let matchingLocales: Array<string> = [];
try {
matchingLocales = Intl.DateTimeFormat.supportedLocalesOf(userLocaleDashed);
} catch (innerError) {
// some users have a locale setup with a ':' in it.
// see https://github.com/oxen-io/session-desktop/issues/3221
const semiColonIndex = userLocaleDashed.indexOf(':');
if (semiColonIndex > -1) {
matchingLocales = Intl.DateTimeFormat.supportedLocalesOf(
userLocaleDashed.substring(0, semiColonIndex)
);
}
}
const mappingTo = matchingLocales?.[0] || 'en';
if (!mappedBrowserLocaleDisplayed) {
mappedBrowserLocaleDisplayed = true;
i18nLog(`userLocaleDashed: '${userLocaleDashed}', mapping to browser locale: ${mappingTo}`);
}
return mappingTo;
} catch (e) {
if (!mappedBrowserLocaleDisplayed) {
mappedBrowserLocaleDisplayed = true;
i18nLog(
`userLocaleDashed: '${userLocaleDashed}' was an invalid locale for supportedLocalesOf(). Falling back to 'en'. Error:${e.message} `
);
}
return 'en';
}
}
export function setInitialLocale(crowdinLocaleArg: CrowdinLocale) {
if (crowdinLocale) {
i18nLog('setInitialLocale: crowdinLocale is already init');
}
crowdinLocale = crowdinLocaleArg;
}
export function isSessionLocaleSet() {
return !!crowdinLocale;
}