gptrans 1.1.2 → 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 +17 -17
- 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
|
@@ -20,6 +20,15 @@ Whether you're building a multilingual website, a mobile app, or a localization
|
|
|
20
20
|
npm install gptrans
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
+
### 🌐 Environment Setup
|
|
24
|
+
|
|
25
|
+
GPTrans uses dotenv for environment configuration. Create a `.env` file in your project root and add your API keys:
|
|
26
|
+
|
|
27
|
+
```env
|
|
28
|
+
OPENAI_API_KEY=your_openai_api_key
|
|
29
|
+
ANTHROPIC_API_KEY=your_anthropic_api_key
|
|
30
|
+
```
|
|
31
|
+
|
|
23
32
|
## 🚀 Quick Start
|
|
24
33
|
|
|
25
34
|
Here's a simple example to get you started:
|
|
@@ -28,8 +37,8 @@ Here's a simple example to get you started:
|
|
|
28
37
|
import GPTrans from 'gptrans';
|
|
29
38
|
|
|
30
39
|
const gptrans = new GPTrans({
|
|
31
|
-
target: 'es-AR',
|
|
32
40
|
from: 'en-US',
|
|
41
|
+
target: 'es-AR',
|
|
33
42
|
model: 'claude-3-5-sonnet-20241022'
|
|
34
43
|
});
|
|
35
44
|
|
|
@@ -52,13 +61,13 @@ console.log(gptrans.t('Card'));
|
|
|
52
61
|
|
|
53
62
|
## ⚙️ Configuration Options
|
|
54
63
|
|
|
55
|
-
When creating a new instance of
|
|
64
|
+
When creating a new instance of GPTrans, you can customize:
|
|
56
65
|
|
|
57
66
|
| Option | Description | Default |
|
|
58
67
|
|--------|-------------|---------|
|
|
59
|
-
| `
|
|
60
|
-
| `
|
|
61
|
-
| `model` | Translation model key | - |
|
|
68
|
+
| `from` | Source language locale | `es-AR` |
|
|
69
|
+
| `target` | Target language locale | `en-US` |
|
|
70
|
+
| `model` | Translation model key | `claude-3-7-sonnet` |
|
|
62
71
|
| `batchThreshold` | Maximum number of characters to accumulate before triggering batch processing | `1000` |
|
|
63
72
|
| `debounceTimeout` | Time in milliseconds to wait before processing translations | `500` |
|
|
64
73
|
|
|
@@ -82,25 +91,16 @@ When creating a new instance of Gptrans, you can customize:
|
|
|
82
91
|
}
|
|
83
92
|
```
|
|
84
93
|
|
|
85
|
-
##
|
|
86
|
-
|
|
87
|
-
Gptrans uses dotenv for environment configuration. Create a `.env` file in your project root and add your API keys:
|
|
88
|
-
|
|
89
|
-
```env
|
|
90
|
-
OPENAI_API_KEY=your_openai_api_key
|
|
91
|
-
ANTHROPIC_API_KEY=your_anthropic_api_key
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
## 🎉 Why Choose Gptrans?
|
|
94
|
+
## 🎉 Why Choose GPTrans?
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
GPTrans stands out by combining advanced AI capabilities with efficient batching and caching. This means:
|
|
97
97
|
|
|
98
98
|
- **Speed:** Reduced API calls and instant retrieval of cached translations
|
|
99
99
|
- **Quality:** Leverage state-of-the-art models for precise and context-aware translations
|
|
100
100
|
- **Flexibility:** Tailor the tool to your specific localization needs with minimal effort
|
|
101
101
|
- **Zero Maintenance:** Set it up once and forget about it - automatic updates and self-healing capabilities keep everything running smoothly
|
|
102
102
|
|
|
103
|
-
If you're looking to streamline your translation workflow and bring your applications to a global audience effortlessly,
|
|
103
|
+
If you're looking to streamline your translation workflow and bring your applications to a global audience effortlessly, GPTrans is the perfect choice!
|
|
104
104
|
|
|
105
105
|
## Contributing
|
|
106
106
|
|
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
|
}
|