feat: create locale shared utils

This commit is contained in:
Nicolas Meienberger 2023-05-06 13:20:44 +02:00 committed by Nicolas Meienberger
parent 0a4d7b38ff
commit 7d4e583d79
3 changed files with 45 additions and 0 deletions

View file

@ -36,6 +36,17 @@ module.exports = {
'arrow-body-style': 0,
'class-methods-use-this': 0,
'jsdoc/require-returns': 0,
'import/extensions': [
'error',
'ignorePackages',
{
'': 'never',
js: 'never',
jsx: 'never',
ts: 'never',
tsx: 'never',
},
],
},
overrides: [
{

View file

@ -0,0 +1,31 @@
export const APP_LOCALES = {
en: 'English',
'fr-FR': 'Français',
} as const;
const FALLBACK_LOCALES = [
{ from: 'fr', to: 'fr-FR' },
{ from: 'en', to: 'en' },
];
export type Locale = keyof typeof APP_LOCALES;
export const Locales = Object.keys(APP_LOCALES) as Locale[];
export const LOCALE_OPTIONS = Object.entries(APP_LOCALES).map(([value, label]) => ({ value, label }));
export const getLocaleFromString = (locale?: string): Locale => {
if (!locale) {
return 'en';
}
if (Locales.includes(locale)) {
return locale as Locale;
}
const fallback = FALLBACK_LOCALES.find(({ from }) => locale.startsWith(from));
if (fallback) {
return fallback.to as Locale;
}
return 'en';
};

View file

@ -15,6 +15,9 @@
"@/server/*": [
"./src/server/*"
],
"@/shared/*": [
"./src/shared/*"
],
},
"lib": [
"dom",