gptrans 1.1.4 → 1.1.8

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 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 | `gpt-4o-mini` |
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
 
@@ -0,0 +1,9 @@
1
+ {
2
+ "topup_uzdh5y": "شحن",
3
+ "transf_176pc1a": "تحويل",
4
+ "deposi_wg2ec5": "إيداع",
5
+ "balanc_1rv8if7": "رصيد",
6
+ "transa_1wtqm5d": "معاملة",
7
+ "accoun_x1y0v8": "حساب",
8
+ "card_yis1ox": "بطاقة"
9
+ }
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 { fileURLToPath } from 'url';
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 = 'gpt-4o-mini', batchThreshold = 1000, debounceTimeout = 500, promptFile = null, context = '' }) {
27
- const __dirname = dirname(fileURLToPath(import.meta.url));
28
- this.target = target;
29
- this.from = from;
30
- this.dbTarget = new DeepBase({ name: 'gptrans_' + this.target });
31
- this.dbFrom = new DeepBase({ name: 'gptrans_from_' + this.from });
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 ??= join(__dirname, './prompt/translate.md'); // Convert to absolute path
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(isoAssoc(this.target, 'TARGET_'));
129
- model.replace(isoAssoc(this.from, 'FROM_'));
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']: country ? countryDenonym[country] : 'Neutral',
192
+ [prefix + 'DENONYM']: denonym,
177
193
  };
178
194
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gptrans",
3
3
  "type": "module",
4
- "version": "1.1.4",
4
+ "version": "1.1.8",
5
5
  "description": "🚆 GPTrans - The smarter AI-powered way to translate.",
6
6
  "keywords": [
7
7
  "translate",
@@ -33,7 +33,7 @@
33
33
  "dependencies": {
34
34
  "deepbase": "^1.3.2",
35
35
  "dotenv": "^16.4.7",
36
- "modelmix": "^2.8.6",
36
+ "modelmix": "^2.9.0",
37
37
  "string-hash": "^1.1.3"
38
38
  }
39
39
  }