浏览代码

refac: Move store to i18n index

Ased Mammad 1 年之前
父节点
当前提交
41378748b8
共有 2 个文件被更改,包括 37 次插入38 次删除
  1. 37 4
      src/lib/i18n/index.ts
  2. 0 34
      src/lib/i18n/store.ts

+ 37 - 4
src/lib/i18n/index.ts

@@ -1,7 +1,41 @@
 import i18next from 'i18next';
 import resourcesToBackend from 'i18next-resources-to-backend';
 import LanguageDetector from 'i18next-browser-languagedetector';
-import { createI18nStore, isLoading as isLoadingStore } from './store';
+import type { i18n as i18nType } from 'i18next';
+import { writable } from 'svelte/store';
+
+const createI18nStore = (i18n: i18n) => {
+	const i18nWritable = writable(i18n);
+
+	i18n.on('initialized', () => {
+		i18nWritable.set(i18n);
+	});
+	i18n.on('loaded', () => {
+		i18nWritable.set(i18n);
+	});
+	i18n.on('added', () => i18nWritable.set(i18n));
+	i18n.on('languageChanged', () => {
+		i18nWritable.set(i18n);
+	});
+	return i18nWritable;
+};
+
+const createIsLoadingStore = (i18n: i18n) => {
+	const isLoading = writable(false);
+
+	// if loaded resources are empty || {}, set loading to true
+	i18n.on('loaded', (resources) => {
+		// console.log('loaded:', resources);
+		Object.keys(resources).length !== 0 && isLoading.set(false);
+	});
+
+	// if resources failed loading, set loading to true
+	i18n.on('failedLoading', () => {
+		isLoading.set(true);
+	});
+
+	return isLoading;
+};
 
 i18next
 	.use(
@@ -18,13 +52,12 @@ i18next
 		},
 		fallbackLng: 'en',
 		ns: 'common',
-		// backend: {
-		// 	loadPath: '/locales/{{lng}}/{{ns}}.json'
-		// }
 		interpolation: {
 			escapeValue: false // not needed for svelte as it escapes by default
 		}
 	});
+
 const i18n = createI18nStore(i18next);
+const isLoadingStore = createIsLoadingStore(i18next);
 export default i18n;
 export const isLoading = isLoadingStore;

+ 0 - 34
src/lib/i18n/store.ts

@@ -1,34 +0,0 @@
-import type { i18n } from 'i18next';
-import { writable } from 'svelte/store';
-
-export const createI18nStore = (i18n: i18n) => {
-	const i18nWritable = writable(i18n);
-
-	i18n.on('initialized', () => {
-		i18nWritable.set(i18n);
-	});
-	i18n.on('loaded', () => {
-		i18nWritable.set(i18n);
-	});
-	i18n.on('added', () => i18nWritable.set(i18n));
-	i18n.on('languageChanged', () => {
-		i18nWritable.set(i18n);
-	});
-	return i18nWritable;
-};
-
-export const isLoading = (i18n: i18n) => {
-	const isLoading = writable(false);
-
-	// if loaded resources are empty || {}, set loading to true
-	i18n.on('loaded', (resources) => {
-		Object.keys(resources).length !== 0 && isLoading.set(false);
-	});
-
-	// if resources failed loading, set loading to true
-	i18n.on('failedLoading', () => {
-		isLoading.set(true);
-	});
-
-	return isLoading;
-};