gptrans 1.1.4 → 1.1.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/README.md +1 -1
- package/db/gptrans_ar.json +9 -0
- package/demo/case_2.js +19 -0
- package/index.js +15 -11
- package/isoAssoc.js +17 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -67,7 +67,7 @@ When creating a new instance of GPTrans, you can customize:
|
|
|
67
67
|
|--------|-------------|---------|
|
|
68
68
|
| `from` | Source language locale | `es-AR` |
|
|
69
69
|
| `target` | Target language locale | `en-US` |
|
|
70
|
-
| `model` | Translation model key | `
|
|
70
|
+
| `model` | Translation model key | `claude-3-7-sonnet` |
|
|
71
71
|
| `batchThreshold` | Maximum number of characters to accumulate before triggering batch processing | `1000` |
|
|
72
72
|
| `debounceTimeout` | Time in milliseconds to wait before processing translations | `500` |
|
|
73
73
|
|
package/demo/case_2.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import GPTrans from '../index.js';
|
|
2
|
+
|
|
3
|
+
try {
|
|
4
|
+
const gptrans = new GPTrans({
|
|
5
|
+
target: 'ar',
|
|
6
|
+
model: 'claude-3-7-sonnet-20250219',
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
console.log(gptrans.t('Top-up'));
|
|
10
|
+
console.log(gptrans.t('Transfer'));
|
|
11
|
+
console.log(gptrans.t('Deposit'));
|
|
12
|
+
console.log(gptrans.t('Balance'));
|
|
13
|
+
console.log(gptrans.t('Transaction'));
|
|
14
|
+
console.log(gptrans.t('Account'));
|
|
15
|
+
console.log(gptrans.t('Card'));
|
|
16
|
+
} catch (e) {
|
|
17
|
+
console.error(e);
|
|
18
|
+
}
|
|
19
|
+
|
package/index.js
CHANGED
|
@@ -2,8 +2,7 @@ import DeepBase from 'deepbase';
|
|
|
2
2
|
import stringHash from 'string-hash';
|
|
3
3
|
import { ModelMix, MixOpenAI, MixAnthropic } from 'modelmix';
|
|
4
4
|
import dotenv from 'dotenv';
|
|
5
|
-
import
|
|
6
|
-
import { dirname, join } from 'path';
|
|
5
|
+
import path from 'path';
|
|
7
6
|
|
|
8
7
|
import { isoAssoc } from './isoAssoc.js';
|
|
9
8
|
dotenv.config();
|
|
@@ -23,19 +22,24 @@ class Gptrans {
|
|
|
23
22
|
return this.#mmixInstance;
|
|
24
23
|
}
|
|
25
24
|
|
|
26
|
-
constructor({ from = 'en-US', target = 'es-AR', model = '
|
|
27
|
-
|
|
28
|
-
this.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
constructor({ from = 'en-US', target = 'es-AR', model = 'claude-3-7-sonnet-20250219', batchThreshold = 1000, debounceTimeout = 500, promptFile = null, context = '' }) {
|
|
26
|
+
this.dbTarget = new DeepBase({ name: 'gptrans_' + target });
|
|
27
|
+
this.dbFrom = new DeepBase({ name: 'gptrans_from_' + from });
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
this.replace_target = isoAssoc(target, 'TARGET_');
|
|
31
|
+
this.replace_from = isoAssoc(from, 'FROM_');
|
|
32
|
+
} catch (e) {
|
|
33
|
+
throw new Error(`Invalid target: ${target}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
32
36
|
this.batchThreshold = batchThreshold; // Now represents character count threshold
|
|
33
37
|
this.debounceTimeout = debounceTimeout;
|
|
34
38
|
this.pendingTranslations = new Map(); // [key, text]
|
|
35
39
|
this.pendingCharCount = 0; // Add character count tracker
|
|
36
40
|
this.debounceTimer = null;
|
|
37
41
|
this.modelKey = model;
|
|
38
|
-
this.promptFile ??=
|
|
42
|
+
this.promptFile ??= path.resolve('./prompt/translate.md'); // Convert to absolute path
|
|
39
43
|
this.context = context;
|
|
40
44
|
this.modelConfig = {
|
|
41
45
|
config: {
|
|
@@ -125,8 +129,8 @@ class Gptrans {
|
|
|
125
129
|
model.addTextFromFile(this.promptFile);
|
|
126
130
|
|
|
127
131
|
model.replace({ INPUT: text, CONTEXT: this.context });
|
|
128
|
-
model.replace(
|
|
129
|
-
model.replace(
|
|
132
|
+
model.replace(this.replace_target);
|
|
133
|
+
model.replace(this.replace_from);
|
|
130
134
|
|
|
131
135
|
const response = await model.message();
|
|
132
136
|
|
package/isoAssoc.js
CHANGED
|
@@ -169,10 +169,26 @@ export function isoAssoc(iso, prefix = '') {
|
|
|
169
169
|
throw new Error(`Invalid country code: ${country}`);
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
+
// Special handling for languages with variants
|
|
173
|
+
let denonym = country ? countryDenonym[country] : 'Neutral';
|
|
174
|
+
|
|
175
|
+
// Handle Chinese without specified dialect (use "Simplified" instead of "Neutral")
|
|
176
|
+
if (lang === 'zh' && !country) {
|
|
177
|
+
denonym = 'Simplified';
|
|
178
|
+
}
|
|
179
|
+
// Handle Arabic without specified dialect (use "Standard" instead of "Neutral")
|
|
180
|
+
else if (lang === 'ar' && !country) {
|
|
181
|
+
denonym = 'Standard';
|
|
182
|
+
}
|
|
183
|
+
// Handle Portuguese without specified dialect (use "European" instead of "Neutral")
|
|
184
|
+
else if (lang === 'pt' && !country) {
|
|
185
|
+
denonym = 'European';
|
|
186
|
+
}
|
|
187
|
+
|
|
172
188
|
return {
|
|
173
189
|
[prefix + 'ISO']: iso,
|
|
174
190
|
[prefix + 'LANG']: langName[lang],
|
|
175
191
|
[prefix + 'COUNTRY']: country ? countryName[country] : langName[lang],
|
|
176
|
-
[prefix + 'DENONYM']:
|
|
192
|
+
[prefix + 'DENONYM']: denonym,
|
|
177
193
|
};
|
|
178
194
|
}
|