@vue-sfc-i18n/composable 0.0.5 → 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 CHANGED
@@ -1,10 +1,11 @@
1
1
  export interface I18nSchema {
2
2
  }
3
- type TranslationKey = keyof I18nSchema extends never ? string : keyof I18nSchema;
4
- type ParamsArg<K extends TranslationKey> = K extends keyof I18nSchema ? I18nSchema[K] extends string ? [] : [params: I18nSchema[K]] : [params?: Record<string, string | number>];
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];
5
5
  type EntryLoader = () => Promise<Record<string, string>>;
6
- export declare const useI18n: () => {
7
- t: <K extends TranslationKey>(key: K, ...args: ParamsArg<K>) => string;
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;
8
9
  };
9
10
  export declare const manager: {
10
11
  /** @private */
@@ -16,5 +17,6 @@ export declare const manager: {
16
17
  registerEntryLoader(fn: EntryLoader): void;
17
18
  /** @private */
18
19
  fetchEntryTranslations(): Promise<Record<string, string>>;
20
+ setPluralRules(rules: Record<string, PluralRule>): void;
19
21
  };
20
22
  export {};
package/dist/index.js CHANGED
@@ -2,13 +2,26 @@ import { ref } from "vue";
2
2
  const translations = ref({});
3
3
  const language = ref(null);
4
4
  const entryLoaders = [];
5
+ const defaultPluralRule = choice => (choice === 1 ? 0 : 1);
6
+ const pluralRules = {};
5
7
  const interpolate = (str, params) => str.replace(/\{\s*(\w+)\s*\}/g, (_, k) => String(params[k] ?? `{${k}}`));
6
- const t = (key, ...args) => {
7
- const value = translations.value[key] ?? key;
8
- const params = args[0];
9
- return params ? interpolate(value, params) : value;
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 };
10
24
  };
11
- export const useI18n = () => ({ t });
12
25
  export const manager = {
13
26
  /** @private */
14
27
  getLanguage() {
@@ -36,4 +49,7 @@ export const manager = {
36
49
  const results = await Promise.all(entryLoaders.map(fn => fn()));
37
50
  return results.reduce((acc, r) => ({ ...acc, ...r }), {});
38
51
  },
52
+ setPluralRules(rules) {
53
+ Object.assign(pluralRules, rules);
54
+ },
39
55
  };
package/package.json CHANGED
@@ -1,38 +1,38 @@
1
1
  {
2
2
  "name": "@vue-sfc-i18n/composable",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "Vue composable for using i18n translations from Vue SFC custom blocks",
5
5
  "keywords": [
6
- "vue",
7
- "vue3",
6
+ "composable",
8
7
  "i18n",
8
+ "internationalization",
9
9
  "sfc",
10
- "composable",
11
- "internationalization"
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
- "publishConfig": {
24
- "access": "public",
25
- "registry": "https://registry.npmjs.org/",
26
- "provenance": true
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
- "types": "./dist/index.d.ts",
33
- "files": [
34
- "dist"
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
  },