@itrocks/translate 0.0.11 → 0.2.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/cjs/translate.js +3 -1
- package/package.json +7 -14
- package/esm/translate.d.ts +0 -11
- package/esm/translate.js +0 -83
package/cjs/translate.js
CHANGED
|
@@ -32,7 +32,9 @@ function tr(text, parts, options) {
|
|
|
32
32
|
?? exports.translations.get(text.toLocaleLowerCase())
|
|
33
33
|
?? trMatch(text, parts);
|
|
34
34
|
if (!translated) {
|
|
35
|
-
const separator = (text.length > 1)
|
|
35
|
+
const separator = (text.length > 1)
|
|
36
|
+
? ['.', '?', '!', ';', ':', ',', '(', ')'].find(c => text.includes(c))
|
|
37
|
+
: undefined;
|
|
36
38
|
if (separator) {
|
|
37
39
|
translated = text.split(separator).map(text => tr(text, parts)).join(tr(separator).replace(/ /g, '\u00A0'));
|
|
38
40
|
return firstSpaces + translated + lastSpaces;
|
package/package.json
CHANGED
|
@@ -10,21 +10,17 @@
|
|
|
10
10
|
"devDependencies": {
|
|
11
11
|
"@types/node": "^24.10",
|
|
12
12
|
"@types/papaparse": "^5.5",
|
|
13
|
-
"typescript": "
|
|
13
|
+
"typescript": "^6.0"
|
|
14
14
|
},
|
|
15
15
|
"engines": {
|
|
16
|
-
"node": ">=
|
|
17
|
-
},
|
|
18
|
-
"exports": {
|
|
19
|
-
"import": "./esm/translate.js",
|
|
20
|
-
"require": "./cjs/translate.js"
|
|
16
|
+
"node": ">=24"
|
|
21
17
|
},
|
|
18
|
+
"exports": "./cjs/translate.js",
|
|
22
19
|
"files": [
|
|
23
20
|
"LICENSE",
|
|
24
21
|
"README.md",
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"!*/esm.*"
|
|
22
|
+
"cjs",
|
|
23
|
+
"!*.map"
|
|
28
24
|
],
|
|
29
25
|
"homepage": "https://it.rocks",
|
|
30
26
|
"keywords": [
|
|
@@ -46,10 +42,7 @@
|
|
|
46
42
|
"url": "git+https://github.com/itrocks-ts/translate.git"
|
|
47
43
|
},
|
|
48
44
|
"scripts": {
|
|
49
|
-
"build": "
|
|
50
|
-
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
51
|
-
"build:esm": "tsc -p tsconfig.esm.json && node esm/esm"
|
|
45
|
+
"build": "tsc"
|
|
52
46
|
},
|
|
53
|
-
"
|
|
54
|
-
"version": "0.0.11"
|
|
47
|
+
"version": "0.2.1"
|
|
55
48
|
}
|
package/esm/translate.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export declare const DefaultOptions: Options;
|
|
2
|
-
export declare const expressions: Set<RegExp>;
|
|
3
|
-
export declare const translations: Map<string, string>;
|
|
4
|
-
export declare function lang(): string;
|
|
5
|
-
export type Options = {
|
|
6
|
-
ucFirst?: boolean;
|
|
7
|
-
};
|
|
8
|
-
export declare function tr(text: string, options: Options): string;
|
|
9
|
-
export declare function tr(text: string, parts?: string[], options?: Options): string;
|
|
10
|
-
export declare function trInit(lang: string): void;
|
|
11
|
-
export declare function trLoad(file: string): Promise<void>;
|
package/esm/translate.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { createRequire } from 'module'
|
|
2
|
-
const require = createRequire(import.meta.url)
|
|
3
|
-
import { access } from 'node:fs/promises';
|
|
4
|
-
import { readFile } from 'node:fs/promises';
|
|
5
|
-
const parseCsv = require('papaparse').parse;
|
|
6
|
-
export const DefaultOptions = {
|
|
7
|
-
ucFirst: true
|
|
8
|
-
};
|
|
9
|
-
export const expressions = new Set;
|
|
10
|
-
export const translations = new Map;
|
|
11
|
-
let language = 'en-US';
|
|
12
|
-
export function lang() {
|
|
13
|
-
return language;
|
|
14
|
-
}
|
|
15
|
-
export function tr(text, parts, options) {
|
|
16
|
-
if (!Array.isArray(parts)) {
|
|
17
|
-
options = parts;
|
|
18
|
-
parts = [];
|
|
19
|
-
}
|
|
20
|
-
const [firstSpaces, lastSpaces] = (text.match(/^(\s*).*(\s*)$/) ?? ['', '', '']).slice(1);
|
|
21
|
-
text = text.trim();
|
|
22
|
-
let partsCount = parts.length;
|
|
23
|
-
const firstChar = text[0];
|
|
24
|
-
const ucFirst = (options?.ucFirst ?? DefaultOptions.ucFirst) && (firstChar >= 'A') && (firstChar <= 'Z');
|
|
25
|
-
let translated = translations.get(text)
|
|
26
|
-
?? (ucFirst ? translations.get(firstChar.toLocaleLowerCase() + text.slice(1)) : undefined)
|
|
27
|
-
?? translations.get(text.toLocaleLowerCase())
|
|
28
|
-
?? trMatch(text, parts);
|
|
29
|
-
if (!translated) {
|
|
30
|
-
const separator = (text.length > 1) ? text.match(/[.?!;:,()]/)?.[0] : undefined;
|
|
31
|
-
if (separator) {
|
|
32
|
-
translated = text.split(separator).map(text => tr(text, parts)).join(tr(separator).replace(/ /g, '\u00A0'));
|
|
33
|
-
return firstSpaces + translated + lastSpaces;
|
|
34
|
-
}
|
|
35
|
-
translated = text;
|
|
36
|
-
}
|
|
37
|
-
while (partsCount) {
|
|
38
|
-
translated = translated.replaceAll('$' + partsCount, parts[--partsCount]);
|
|
39
|
-
}
|
|
40
|
-
if (ucFirst) {
|
|
41
|
-
translated = translated[0].toLocaleUpperCase() + translated.slice(1);
|
|
42
|
-
}
|
|
43
|
-
return firstSpaces + translated + lastSpaces;
|
|
44
|
-
}
|
|
45
|
-
export function trInit(lang) {
|
|
46
|
-
language = lang;
|
|
47
|
-
expressions.clear();
|
|
48
|
-
translations.clear();
|
|
49
|
-
}
|
|
50
|
-
export async function trLoad(file) {
|
|
51
|
-
try {
|
|
52
|
-
await access(file);
|
|
53
|
-
}
|
|
54
|
-
catch {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
return readFile(file, 'utf-8')
|
|
58
|
-
.then((data) => parseCsv(data, { delimiter: ';' }).data)
|
|
59
|
-
.then(data => data.forEach(row => {
|
|
60
|
-
translations.set(row[0], row[1]);
|
|
61
|
-
if (row[0].includes('$')) {
|
|
62
|
-
expressions.add(RegExp('^' + row[0].replace(/(\$[0-9]+)/, '(.*)') + '$'));
|
|
63
|
-
}
|
|
64
|
-
}));
|
|
65
|
-
}
|
|
66
|
-
function trMatch(text, parts) {
|
|
67
|
-
for (const expression of expressions) {
|
|
68
|
-
const match = text.match(expression);
|
|
69
|
-
if (!match)
|
|
70
|
-
continue;
|
|
71
|
-
let counter = parts.length;
|
|
72
|
-
const replacements = [];
|
|
73
|
-
const trParts = [...parts];
|
|
74
|
-
for (const part of match.slice(1)) {
|
|
75
|
-
const translatedPart = tr(part);
|
|
76
|
-
trParts.push(translatedPart[0].toLocaleLowerCase() + translatedPart.slice(1));
|
|
77
|
-
replacements.push('$' + trParts.length);
|
|
78
|
-
}
|
|
79
|
-
const trText = expression.source.slice(1, -1).replaceAll('(.*)', () => '$' + ++counter);
|
|
80
|
-
return tr(trText, trParts);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
//# sourceMappingURL=translate.js.map
|