feat: create locale shared utils
This commit is contained in:
parent
0a4d7b38ff
commit
7d4e583d79
3 changed files with 45 additions and 0 deletions
11
.eslintrc.js
11
.eslintrc.js
|
@ -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: [
|
||||
{
|
||||
|
|
31
src/shared/internationalization/locales.ts
Normal file
31
src/shared/internationalization/locales.ts
Normal 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';
|
||||
};
|
|
@ -15,6 +15,9 @@
|
|||
"@/server/*": [
|
||||
"./src/server/*"
|
||||
],
|
||||
"@/shared/*": [
|
||||
"./src/shared/*"
|
||||
],
|
||||
},
|
||||
"lib": [
|
||||
"dom",
|
||||
|
|
Loading…
Reference in a new issue