Browse Source

feat: create locale shared utils

Nicolas Meienberger 2 years ago
parent
commit
242d475ba6
3 changed files with 45 additions and 0 deletions
  1. 11 0
      .eslintrc.js
  2. 31 0
      src/shared/internationalization/locales.ts
  3. 3 0
      tsconfig.json

+ 11 - 0
.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 - 0
src/shared/internationalization/locales.ts

@@ -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';
+};

+ 3 - 0
tsconfig.json

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