@vue-sfc-i18n/composable 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +19 -0
- package/dist/index.js +34 -0
- package/package.json +23 -2
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface I18nSchema {
|
|
2
|
+
}
|
|
3
|
+
type TranslationKey = keyof I18nSchema extends never ? string : keyof I18nSchema;
|
|
4
|
+
type EntryLoader = () => Promise<Record<string, string>>;
|
|
5
|
+
export declare const useI18n: () => {
|
|
6
|
+
t: (key: TranslationKey) => string;
|
|
7
|
+
};
|
|
8
|
+
export declare const manager: {
|
|
9
|
+
/** @private */
|
|
10
|
+
getLanguage(): string;
|
|
11
|
+
loadTranslations(lang: string): Promise<void>;
|
|
12
|
+
/** @private */
|
|
13
|
+
mergeTranslations(data: Record<string, string>): void;
|
|
14
|
+
/** @private */
|
|
15
|
+
registerEntryLoader(fn: EntryLoader): void;
|
|
16
|
+
/** @private */
|
|
17
|
+
fetchEntryTranslations(): Promise<Record<string, string>>;
|
|
18
|
+
};
|
|
19
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ref } from "vue";
|
|
2
|
+
const translations = ref({});
|
|
3
|
+
const language = ref(null);
|
|
4
|
+
const entryLoaders = [];
|
|
5
|
+
const t = (key) => translations.value[key] ?? key;
|
|
6
|
+
export const useI18n = () => ({ t });
|
|
7
|
+
export const manager = {
|
|
8
|
+
/** @private */
|
|
9
|
+
getLanguage() {
|
|
10
|
+
if (language.value === null)
|
|
11
|
+
throw new Error("[@vue-sfc-i18n/composable] language is not set");
|
|
12
|
+
return language.value;
|
|
13
|
+
},
|
|
14
|
+
async loadTranslations(lang) {
|
|
15
|
+
language.value = lang;
|
|
16
|
+
const data = await manager.fetchEntryTranslations();
|
|
17
|
+
if (entryLoaders.length > 0 && Object.keys(data).length === 0)
|
|
18
|
+
throw new Error(`[@vue-sfc-i18n/composable] no translations found for language "${lang}"`);
|
|
19
|
+
translations.value = data;
|
|
20
|
+
},
|
|
21
|
+
/** @private */
|
|
22
|
+
mergeTranslations(data) {
|
|
23
|
+
translations.value = { ...translations.value, ...data };
|
|
24
|
+
},
|
|
25
|
+
/** @private */
|
|
26
|
+
registerEntryLoader(fn) {
|
|
27
|
+
entryLoaders.push(fn);
|
|
28
|
+
},
|
|
29
|
+
/** @private */
|
|
30
|
+
async fetchEntryTranslations() {
|
|
31
|
+
const results = await Promise.all(entryLoaders.map(fn => fn()));
|
|
32
|
+
return results.reduce((acc, r) => ({ ...acc, ...r }), {});
|
|
33
|
+
},
|
|
34
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vue-sfc-i18n/composable",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "Vue composable for using i18n translations from Vue SFC custom blocks",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"vue",
|
|
7
|
+
"vue3",
|
|
8
|
+
"i18n",
|
|
9
|
+
"sfc",
|
|
10
|
+
"composable",
|
|
11
|
+
"internationalization"
|
|
12
|
+
],
|
|
13
|
+
"author": "Sándor Levcsák",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/levchak0910/vue-sfc-i18n.git"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "git+https://github.com/levchak0910/vue-sfc-i18n#readme",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "git+https://github.com/levchak0910/vue-sfc-i18n/issues"
|
|
22
|
+
},
|
|
4
23
|
"publishConfig": {
|
|
5
|
-
"access": "public"
|
|
24
|
+
"access": "public",
|
|
25
|
+
"registry": "https://registry.npmjs.org/",
|
|
26
|
+
"provenance": true
|
|
6
27
|
},
|
|
7
28
|
"type": "module",
|
|
8
29
|
"exports": {
|