@vixen-tech/lynguist 0.0.1
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 +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +36 -0
- package/dist/types.d.ts +10 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +4 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +127 -0
- package/package.json +43 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { LynguistOptions, LynguistTerm, ReplacePlaceholders } from './types';
|
|
2
|
+
export type { LynguistTranslations, LynguistTerm } from './types';
|
|
3
|
+
export declare function Lynguist(options: LynguistOptions): void;
|
|
4
|
+
export declare function __(key: LynguistTerm, replace?: ReplacePlaceholders): string;
|
|
5
|
+
export declare function __(key: LynguistTerm, count: number, replace?: ReplacePlaceholders): string;
|
|
6
|
+
export declare function trans(key: LynguistTerm, replace?: ReplacePlaceholders): string;
|
|
7
|
+
export declare function transChoice(key: LynguistTerm, count: number, replace?: ReplacePlaceholders): string;
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAA;AAG5E,YAAY,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAOjE,wBAAgB,QAAQ,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI,CAGvD;AAED,wBAAgB,EAAE,CAAC,GAAG,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,MAAM,CAAA;AAC5E,wBAAgB,EAAE,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,MAAM,CAAA;AAS3F,wBAAgB,KAAK,CAAC,GAAG,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,MAAM,CAU9E;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,MAAM,CAcnG"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { pluralIndex, replacePlaceholders } from './utils';
|
|
2
|
+
let lynguist = {
|
|
3
|
+
locale: 'en',
|
|
4
|
+
translations: {},
|
|
5
|
+
};
|
|
6
|
+
export function Lynguist(options) {
|
|
7
|
+
lynguist.locale = options.locale;
|
|
8
|
+
lynguist.translations = options.translations;
|
|
9
|
+
}
|
|
10
|
+
export function __(key, countOrReplace, replace) {
|
|
11
|
+
if (typeof countOrReplace === 'number') {
|
|
12
|
+
return transChoice(key, countOrReplace, replace);
|
|
13
|
+
}
|
|
14
|
+
return trans(key, countOrReplace);
|
|
15
|
+
}
|
|
16
|
+
export function trans(key, replace) {
|
|
17
|
+
let translation = lynguist.translations[key];
|
|
18
|
+
if (!(key in lynguist.translations) || !translation)
|
|
19
|
+
return key;
|
|
20
|
+
if (replace) {
|
|
21
|
+
translation = replacePlaceholders(translation, replace);
|
|
22
|
+
}
|
|
23
|
+
return translation;
|
|
24
|
+
}
|
|
25
|
+
export function transChoice(key, count, replace) {
|
|
26
|
+
if (!(key in lynguist.translations) || !lynguist.translations[key])
|
|
27
|
+
return key;
|
|
28
|
+
const parts = lynguist.translations[key].split('|');
|
|
29
|
+
let index = pluralIndex(count, lynguist.locale);
|
|
30
|
+
let translation = parts[index];
|
|
31
|
+
translation = translation.replaceAll(/:count/g, count.toString());
|
|
32
|
+
if (replace) {
|
|
33
|
+
translation = replacePlaceholders(translation, replace);
|
|
34
|
+
}
|
|
35
|
+
return translation;
|
|
36
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface LynguistTranslations extends Record<string, string | null> {
|
|
2
|
+
}
|
|
3
|
+
export interface LynguistOptions {
|
|
4
|
+
locale: LynguistLocale;
|
|
5
|
+
translations: LynguistTranslations;
|
|
6
|
+
}
|
|
7
|
+
export type LynguistTerm = keyof LynguistTranslations;
|
|
8
|
+
export type ReplacePlaceholders = Record<string, number | string | null | undefined>;
|
|
9
|
+
export type LynguistLocale = 'af' | 'am' | 'ar' | 'be' | 'bh' | 'bn' | 'bs' | 'bg' | 'ca' | 'cs' | 'cy' | 'da' | 'de' | 'el' | 'en' | 'eo' | 'es' | 'et' | 'eu' | 'fa' | 'fi' | 'fil' | 'fo' | 'fr' | 'fur' | 'fy' | 'ga' | 'gl' | 'gu' | 'gun' | 'ha' | 'he' | 'hi' | 'hr' | 'hu' | 'hy' | 'is' | 'it' | 'ku' | 'lb' | 'ln' | 'lt' | 'lv' | 'mg' | 'mk' | 'ml' | 'mn' | 'mr' | 'mt' | 'nah' | 'nb' | 'ne' | 'nl' | 'nn' | 'no' | 'nso' | 'om' | 'or' | 'pa' | 'pap' | 'pl' | 'ps' | 'pt' | 'ro' | 'ru' | 'sk' | 'sl' | 'so' | 'sq' | 'sr' | 'sv' | 'sw' | 'ta' | 'te' | 'ti' | 'tk' | 'uk' | 'ur' | 'wa' | 'xbr' | 'zu';
|
|
10
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,oBAAqB,SAAQ,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;CAAG;AAE9E,MAAM,WAAW,eAAe;IAC5B,MAAM,EAAE,cAAc,CAAA;IACtB,YAAY,EAAE,oBAAoB,CAAA;CACrC;AAED,MAAM,MAAM,YAAY,GAAG,MAAM,oBAAoB,CAAA;AA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istLocale, ReplacePlaceholders } from './types';
|
|
2
|
+
export declare function replacePlaceholders(text: string, replace: ReplacePlaceholders): string;
|
|
3
|
+
export declare function pluralIndex(count: number, locale: LynguistLocale): number;
|
|
4
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAA;AAE7D,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,mBAAmB,GAAG,MAAM,CAkBtF;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,GAAG,MAAM,CA4HzE"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
export function replacePlaceholders(text, replace) {
|
|
2
|
+
let translation = text;
|
|
3
|
+
for (const [placeholder, value] of Object.entries(replace)) {
|
|
4
|
+
if (value !== null && value !== undefined) {
|
|
5
|
+
let parameterValue = String(value);
|
|
6
|
+
if (placeholder === placeholder.toUpperCase()) {
|
|
7
|
+
parameterValue = parameterValue.toUpperCase();
|
|
8
|
+
}
|
|
9
|
+
else if (placeholder[0] === placeholder[0].toUpperCase()) {
|
|
10
|
+
parameterValue = parameterValue.charAt(0).toUpperCase() + parameterValue.slice(1);
|
|
11
|
+
}
|
|
12
|
+
translation = translation.replace(`:${placeholder}`, parameterValue);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return translation;
|
|
16
|
+
}
|
|
17
|
+
export function pluralIndex(count, locale) {
|
|
18
|
+
switch (locale) {
|
|
19
|
+
case 'af':
|
|
20
|
+
case 'bn':
|
|
21
|
+
case 'bg':
|
|
22
|
+
case 'ca':
|
|
23
|
+
case 'da':
|
|
24
|
+
case 'de':
|
|
25
|
+
case 'el':
|
|
26
|
+
case 'en':
|
|
27
|
+
case 'eo':
|
|
28
|
+
case 'es':
|
|
29
|
+
case 'et':
|
|
30
|
+
case 'eu':
|
|
31
|
+
case 'fa':
|
|
32
|
+
case 'fi':
|
|
33
|
+
case 'fo':
|
|
34
|
+
case 'fur':
|
|
35
|
+
case 'fy':
|
|
36
|
+
case 'gl':
|
|
37
|
+
case 'gu':
|
|
38
|
+
case 'ha':
|
|
39
|
+
case 'he':
|
|
40
|
+
case 'hu':
|
|
41
|
+
case 'is':
|
|
42
|
+
case 'it':
|
|
43
|
+
case 'ku':
|
|
44
|
+
case 'lb':
|
|
45
|
+
case 'ml':
|
|
46
|
+
case 'mn':
|
|
47
|
+
case 'mr':
|
|
48
|
+
case 'nah':
|
|
49
|
+
case 'nb':
|
|
50
|
+
case 'ne':
|
|
51
|
+
case 'nl':
|
|
52
|
+
case 'nn':
|
|
53
|
+
case 'no':
|
|
54
|
+
case 'om':
|
|
55
|
+
case 'or':
|
|
56
|
+
case 'pa':
|
|
57
|
+
case 'pap':
|
|
58
|
+
case 'ps':
|
|
59
|
+
case 'pt':
|
|
60
|
+
case 'so':
|
|
61
|
+
case 'sq':
|
|
62
|
+
case 'sv':
|
|
63
|
+
case 'sw':
|
|
64
|
+
case 'ta':
|
|
65
|
+
case 'te':
|
|
66
|
+
case 'tk':
|
|
67
|
+
case 'ur':
|
|
68
|
+
case 'zu':
|
|
69
|
+
return count === 1 ? 0 : 1;
|
|
70
|
+
case 'am':
|
|
71
|
+
case 'bh':
|
|
72
|
+
case 'fil':
|
|
73
|
+
case 'fr':
|
|
74
|
+
case 'gun':
|
|
75
|
+
case 'hi':
|
|
76
|
+
case 'hy':
|
|
77
|
+
case 'ln':
|
|
78
|
+
case 'mg':
|
|
79
|
+
case 'nso':
|
|
80
|
+
case 'ti':
|
|
81
|
+
case 'wa':
|
|
82
|
+
case 'xbr':
|
|
83
|
+
return count === 0 || count === 1 ? 0 : 1;
|
|
84
|
+
case 'be':
|
|
85
|
+
case 'bs':
|
|
86
|
+
case 'hr':
|
|
87
|
+
case 'ru':
|
|
88
|
+
case 'sr':
|
|
89
|
+
case 'uk':
|
|
90
|
+
return count % 10 === 1 && count % 100 !== 11 ? 0 : count % 10 >= 2 && count % 10 <= 4 && (count % 100 < 10 || count % 100 >= 20) ? 1 : 2;
|
|
91
|
+
case 'cs':
|
|
92
|
+
case 'sk':
|
|
93
|
+
return count === 1 ? 0 : count >= 2 && count <= 4 ? 1 : 2;
|
|
94
|
+
case 'ga':
|
|
95
|
+
return count === 1 ? 0 : count === 2 ? 1 : 2;
|
|
96
|
+
case 'lt':
|
|
97
|
+
return count % 10 === 1 && count % 100 != 11 ? 0 : count % 10 >= 2 && (count % 100 < 10 || count % 100 >= 20) ? 1 : 2;
|
|
98
|
+
case 'sl':
|
|
99
|
+
return count % 100 === 1 ? 0 : count % 100 === 2 ? 1 : count % 100 === 3 || count % 100 === 4 ? 2 : 3;
|
|
100
|
+
case 'mk':
|
|
101
|
+
return count % 10 === 1 ? 0 : 1;
|
|
102
|
+
case 'mt':
|
|
103
|
+
return count === 1 ? 0 : count === 0 || (count % 100 > 1 && count % 100 < 11) ? 1 : count % 100 > 10 && count % 100 < 20 ? 2 : 3;
|
|
104
|
+
case 'lv':
|
|
105
|
+
return count === 0 ? 0 : count % 10 === 1 && count % 100 != 11 ? 1 : 2;
|
|
106
|
+
case 'pl':
|
|
107
|
+
return count === 1 ? 0 : count % 10 >= 2 && count % 10 <= 4 && (count % 100 < 12 || count % 100 > 14) ? 1 : 2;
|
|
108
|
+
case 'cy':
|
|
109
|
+
return count === 1 ? 0 : count === 2 ? 1 : count === 8 || count === 11 ? 2 : 3;
|
|
110
|
+
case 'ro':
|
|
111
|
+
return count === 1 ? 0 : count === 0 || (count % 100 > 0 && count % 100 < 20) ? 1 : 2;
|
|
112
|
+
case 'ar':
|
|
113
|
+
return count === 0
|
|
114
|
+
? 0
|
|
115
|
+
: count === 1
|
|
116
|
+
? 1
|
|
117
|
+
: count === 2
|
|
118
|
+
? 2
|
|
119
|
+
: count % 100 >= 3 && count % 100 <= 10
|
|
120
|
+
? 3
|
|
121
|
+
: count % 100 >= 11 && count % 100 <= 99
|
|
122
|
+
? 4
|
|
123
|
+
: 5;
|
|
124
|
+
default:
|
|
125
|
+
return 0;
|
|
126
|
+
}
|
|
127
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vixen-tech/lynguist",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc -p tsconfig.json && tsc-alias",
|
|
13
|
+
"clean": "rm -rf dist",
|
|
14
|
+
"prepublishOnly": "pnpm clean && pnpm build",
|
|
15
|
+
"test": "vitest run"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"translations",
|
|
19
|
+
"localization",
|
|
20
|
+
"i18n"
|
|
21
|
+
],
|
|
22
|
+
"author": "Alex Torscho <contact@alextorscho.com> (https://alextorscho.com)",
|
|
23
|
+
"license": "ISC",
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^25.0.3",
|
|
26
|
+
"prettier": "^3.7.4",
|
|
27
|
+
"tsc-alias": "^1.8.16",
|
|
28
|
+
"typescript": "^5.9.3",
|
|
29
|
+
"vite": "^7.3.0",
|
|
30
|
+
"vitest": "^4.0.16"
|
|
31
|
+
},
|
|
32
|
+
"directories": {
|
|
33
|
+
"test": "tests"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/vixen-tech/lynguist-js.git"
|
|
38
|
+
},
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/vixen-tech/lynguist-js/issues"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/vixen-tech/lynguist-js#readme"
|
|
43
|
+
}
|