@vue-sfc-i18n/composable 0.0.4 → 0.0.6
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 +6 -3
- package/dist/index.js +23 -2
- package/package.json +24 -22
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export interface I18nSchema {
|
|
2
2
|
}
|
|
3
|
-
type
|
|
3
|
+
type KeysOf<F extends string> = F extends keyof I18nSchema ? keyof I18nSchema[F] : keyof I18nSchema[keyof I18nSchema];
|
|
4
|
+
type ParamsArg<F extends keyof I18nSchema, K extends KeysOf<F>> = K extends keyof I18nSchema[F] ? I18nSchema[F][K] extends string ? [] : [params: I18nSchema[F][K]] : [params?: Record<string, string | number> | number];
|
|
4
5
|
type EntryLoader = () => Promise<Record<string, string>>;
|
|
5
|
-
export
|
|
6
|
-
|
|
6
|
+
export type PluralRule = (choice: number) => number;
|
|
7
|
+
export declare const useI18n: <F extends keyof I18nSchema = keyof I18nSchema>(_filePath?: F | undefined) => {
|
|
8
|
+
t: <K extends KeysOf<F>>(key: K, ...args: ParamsArg<F, K>) => string;
|
|
7
9
|
};
|
|
8
10
|
export declare const manager: {
|
|
9
11
|
/** @private */
|
|
@@ -15,5 +17,6 @@ export declare const manager: {
|
|
|
15
17
|
registerEntryLoader(fn: EntryLoader): void;
|
|
16
18
|
/** @private */
|
|
17
19
|
fetchEntryTranslations(): Promise<Record<string, string>>;
|
|
20
|
+
setPluralRules(rules: Record<string, PluralRule>): void;
|
|
18
21
|
};
|
|
19
22
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -2,8 +2,26 @@ import { ref } from "vue";
|
|
|
2
2
|
const translations = ref({});
|
|
3
3
|
const language = ref(null);
|
|
4
4
|
const entryLoaders = [];
|
|
5
|
-
const
|
|
6
|
-
|
|
5
|
+
const defaultPluralRule = choice => (choice === 1 ? 0 : 1);
|
|
6
|
+
const pluralRules = {};
|
|
7
|
+
const interpolate = (str, params) => str.replace(/\{\s*(\w+)\s*\}/g, (_, k) => String(params[k] ?? `{${k}}`));
|
|
8
|
+
export const useI18n = (_filePath) => {
|
|
9
|
+
const t = (key, ...args) => {
|
|
10
|
+
const raw = translations.value[key] ?? key;
|
|
11
|
+
const arg = args[0];
|
|
12
|
+
if (typeof arg === "number") {
|
|
13
|
+
const rule = pluralRules[language.value ?? ""] ?? defaultPluralRule;
|
|
14
|
+
const parts = raw.split("|").map(p => p.trim());
|
|
15
|
+
const index = rule(arg);
|
|
16
|
+
const variant = parts[index];
|
|
17
|
+
if (index < 0 || variant === undefined)
|
|
18
|
+
throw new Error(`[@vue-sfc-i18n/composable] plural rule returned index ${index} for key "${key}", but only ${parts.length} variant(s) exist`);
|
|
19
|
+
return interpolate(variant, { n: arg });
|
|
20
|
+
}
|
|
21
|
+
return arg ? interpolate(raw, arg) : raw;
|
|
22
|
+
};
|
|
23
|
+
return { t };
|
|
24
|
+
};
|
|
7
25
|
export const manager = {
|
|
8
26
|
/** @private */
|
|
9
27
|
getLanguage() {
|
|
@@ -31,4 +49,7 @@ export const manager = {
|
|
|
31
49
|
const results = await Promise.all(entryLoaders.map(fn => fn()));
|
|
32
50
|
return results.reduce((acc, r) => ({ ...acc, ...r }), {});
|
|
33
51
|
},
|
|
52
|
+
setPluralRules(rules) {
|
|
53
|
+
Object.assign(pluralRules, rules);
|
|
54
|
+
},
|
|
34
55
|
};
|
package/package.json
CHANGED
|
@@ -1,47 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vue-sfc-i18n/composable",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "Vue composable for using i18n translations from Vue SFC custom blocks",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"
|
|
7
|
-
"vue3",
|
|
6
|
+
"composable",
|
|
8
7
|
"i18n",
|
|
8
|
+
"internationalization",
|
|
9
9
|
"sfc",
|
|
10
|
-
"
|
|
11
|
-
"
|
|
10
|
+
"vue",
|
|
11
|
+
"vue3"
|
|
12
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
13
|
"homepage": "git+https://github.com/levchak0910/vue-sfc-i18n#readme",
|
|
20
14
|
"bugs": {
|
|
21
15
|
"url": "git+https://github.com/levchak0910/vue-sfc-i18n/issues"
|
|
22
16
|
},
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "Sándor Levcsák",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/levchak0910/vue-sfc-i18n.git"
|
|
27
22
|
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
28
26
|
"type": "module",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
29
28
|
"exports": {
|
|
30
29
|
".": "./dist/index.js"
|
|
31
30
|
},
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public",
|
|
33
|
+
"provenance": true,
|
|
34
|
+
"registry": "https://registry.npmjs.org/"
|
|
35
|
+
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"vue": "^3.5.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@types/node": "^24.12.2",
|
|
41
|
-
"typescript": "^5.9.3"
|
|
41
|
+
"typescript": "^5.9.3",
|
|
42
|
+
"vitest": "^4.1.11"
|
|
42
43
|
},
|
|
43
44
|
"scripts": {
|
|
44
|
-
"build": "tsc --noEmit false --declaration true --outDir dist",
|
|
45
|
-
"type-check": "tsc --noEmit"
|
|
45
|
+
"build": "tsc --noEmit false --allowImportingTsExtensions false --declaration true --outDir dist",
|
|
46
|
+
"type-check": "tsc --noEmit",
|
|
47
|
+
"test": "vitest run"
|
|
46
48
|
}
|
|
47
49
|
}
|