"...mmclassification-speed-benchmark/vscode:/vscode.git/clone" did not exist on "85529f353461ad7aa89e4d6a8cbb1a4f282e43ef"
Commit 9c2313a1 authored by bannert's avatar bannert
Browse files

feat(i18n): improve init function with cleaner code and nullish coalescing operator

   - Use object destructuring for clearer variable assignment.
   - Utilize the nullish coalescing operator to simplify ternary expressions.
   - Refactored the import statement to remove duplication.
parent 6ffa1898
import i18next from 'i18next'; import i18next from 'i18next';
import resourcesToBackend from 'i18next-resources-to-backend';
import LanguageDetector from 'i18next-browser-languagedetector'; import LanguageDetector from 'i18next-browser-languagedetector';
import resourcesToBackend from 'i18next-resources-to-backend';
import type { i18n as i18nType } from 'i18next'; import type { i18n as i18nType } from 'i18next';
import { writable } from 'svelte/store'; import { writable } from 'svelte/store';
...@@ -37,14 +37,14 @@ const createIsLoadingStore = (i18n: i18nType) => { ...@@ -37,14 +37,14 @@ const createIsLoadingStore = (i18n: i18nType) => {
return isLoading; return isLoading;
}; };
export const initI18n = (defaultLocale: string | undefined) => { export const initI18n = (defaultLocale?: string) => {
let detectionOrder = defaultLocale // Use object destructuring for cleaner code
? ['querystring', 'localStorage'] const [defaultDetection, fallbackDetection] = defaultLocale ? ['querystring', 'localStorage'] : ['querystring', 'localStorage', 'navigator'];
: ['querystring', 'localStorage', 'navigator'];
let fallbackDefaultLocale = defaultLocale ? [defaultLocale] : ['en-US']; // Use nullish coalescing operator to simplify the ternary expression
const fallbackDefaultLocale = defaultLocale ?? 'en-US';
const loadResource = (language: string, namespace: string) => const loadResource = (language: string, namespace: string) => import(`./locales/${language}/${namespace}.json`);
import(`./locales/${language}/${namespace}.json`);
i18next i18next
.use(resourcesToBackend(loadResource)) .use(resourcesToBackend(loadResource))
...@@ -52,21 +52,17 @@ export const initI18n = (defaultLocale: string | undefined) => { ...@@ -52,21 +52,17 @@ export const initI18n = (defaultLocale: string | undefined) => {
.init({ .init({
debug: false, debug: false,
detection: { detection: {
order: detectionOrder, order: [defaultDetection, fallbackDetection],
caches: ['localStorage'], caches: ['localStorage'],
lookupQuerystring: 'lang', lookupQuerystring: 'lang',
lookupLocalStorage: 'locale' lookupLocalStorage: 'locale'
}, },
fallbackLng: { fallbackLng: fallbackDefaultLocale,
default: fallbackDefaultLocale
},
ns: 'translation', ns: 'translation',
returnEmptyString: false, returnEmptyString: false,
interpolation: { interpolation: { escapeValue: false }
escapeValue: false // not needed for svelte as it escapes by default
}
}); });
}; };
const i18n = createI18nStore(i18next); const i18n = createI18nStore(i18next);
const isLoadingStore = createIsLoadingStore(i18next); const isLoadingStore = createIsLoadingStore(i18next);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment