Skip to content

Commit fc17577

Browse files
committed
fix: use global
1 parent b97fe6c commit fc17577

File tree

8 files changed

+204
-195
lines changed

8 files changed

+204
-195
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"stylelint-prettier": "^5.0.0",
6666
"typescript": "5.8.2",
6767
"vite": "^5.2.12",
68-
"vue-tsc": "^2.1.10"
68+
"vue-tsc": "2.1.10"
6969
},
7070
"husky": {
7171
"hooks": {

packages/common-helpers/src/i18n.ts

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,80 @@ import get from "lodash-es/get";
33
import trim from "lodash-es/trim";
44
import has from "lodash-es/has";
55

6-
/**
7-
* I18n Composable
8-
*
9-
* @composable
10-
*/
11-
export default function useI18n<L extends Record<string, string | Record<string, string>>>(
12-
options: { locale?: L } = {}
13-
) {
6+
import type { iPluginOptions } from "@open-xamu-co/ui-common-types";
7+
8+
interface iUseI18n<L extends Record<string, string | Record<string, string>>> {
149
/**
1510
* Interpolates localized text
1611
*
1712
* @param key key to the text to interpolate. Ex: "hello_name" => "Hello {name}!"
1813
* @param data Optional number or variables to interpolate into text
1914
* @returns {string}
2015
*/
21-
function t<K extends string & keyof L, Ko extends L[K], KA extends string & keyof Ko>(
16+
t: <K extends string & keyof L, Ko extends L[K], KA extends string & keyof Ko>(
2217
key: Ko extends string ? K : `${K}.${KA}`,
23-
data: number | { [key: string]: unknown; count?: number } = {},
24-
fallback = `No locale for "${key}" provided`
25-
): string {
26-
// Empty string string if locale doesn't exist
27-
let locale = get(options.locale || {}, key, fallback);
28-
const interpolate = /\{(.+?)\}/g;
29-
const plurals = locale.split("|");
30-
const count = typeof data === "number" ? data : (data?.count ?? -1);
31-
32-
// Pluralization
33-
if (count > -1 && plurals.length > 1) {
34-
if (plurals.length === 2) {
35-
// product, products
36-
locale = plurals[count > 1 ? 1 : 0];
37-
} else if (plurals.length === 3) {
38-
// no products, a product, products
39-
locale = plurals[count ? (count > 1 ? 2 : 1) : 0];
40-
}
41-
}
42-
43-
const compile = template(trim(locale), { interpolate });
44-
45-
return compile(typeof data === "number" ? { count } : data);
46-
}
47-
18+
data?: number | { [key: string]: unknown; count?: number },
19+
fallback?: string
20+
) => string;
4821
/**
4922
* Checks if the interpolation key exist
5023
*
5124
* @param key interpolation key to check
5225
* @returns {boolean} true if the key exists
5326
*/
54-
function te<K extends string & keyof L, Ko extends L[K], KA extends string & keyof Ko>(
27+
te: <K extends string & keyof L, Ko extends L[K], KA extends string & keyof Ko>(
5528
key: string
56-
): key is Ko extends string ? K : `${K}.${KA}` {
57-
return has(options.locale || {}, key);
58-
}
59-
29+
) => key is Ko extends string ? K : `${K}.${KA}`;
6030
/**
6131
* Returns translation if key exist
6232
* @param key interpolation key to check
6333
* @returns {string}
6434
*/
65-
function tet(key: string): string {
66-
return (te(key) && t(key)) || key;
67-
}
35+
tet: (key: string) => string;
36+
}
6837

38+
/**
39+
* I18n Composable
40+
*
41+
* @composable
42+
*/
43+
export default function useI18n<L extends Record<string, string | Record<string, string>>>(
44+
options: iPluginOptions & { locale?: L } = {}
45+
): iUseI18n<L> {
6946
return {
70-
t,
71-
te,
72-
tet,
47+
t<K extends string & keyof L, Ko extends L[K], KA extends string & keyof Ko>(
48+
key: Ko extends string ? K : `${K}.${KA}`,
49+
data: number | { [key: string]: unknown; count?: number } = {},
50+
fallback = `No locale for "${key}" provided`
51+
): string {
52+
// Empty string string if locale doesn't exist
53+
let locale = get(options.locale || {}, key, fallback);
54+
const interpolate = /\{(.+?)\}/g;
55+
const plurals = locale.split("|");
56+
const count = typeof data === "number" ? data : (data?.count ?? -1);
57+
58+
// Pluralization
59+
if (count > -1 && plurals.length > 1) {
60+
if (plurals.length === 2) {
61+
// product, products
62+
locale = plurals[count > 1 ? 1 : 0];
63+
} else if (plurals.length === 3) {
64+
// no products, a product, products
65+
locale = plurals[count ? (count > 1 ? 2 : 1) : 0];
66+
}
67+
}
68+
69+
const compile = template(trim(locale), { interpolate });
70+
71+
return compile(typeof data === "number" ? { count } : data);
72+
},
73+
te<K extends string & keyof L, Ko extends L[K], KA extends string & keyof Ko>(
74+
key: string
75+
): key is Ko extends string ? K : `${K}.${KA}` {
76+
return has(options.locale || {}, key);
77+
},
78+
tet(key: string): string {
79+
return (this.te(key) && this.t(key)) || key;
80+
},
7381
};
7482
}

0 commit comments

Comments
 (0)