gptrans 2.0.2 → 2.0.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 CHANGED
@@ -15,16 +15,19 @@ Whether you're building a multilingual website, a mobile app, or a localization
15
15
  - **Caching with JSON:** Quickly retrieves cached translations to boost performance
16
16
  - **Parameter Substitution:** Dynamically replace placeholders in your translations
17
17
  - **Smart Context Handling:** Add contextual information to improve translation accuracy. Perfect for gender-aware translations, domain-specific content, or any scenario where additional context helps produce better results. The context is automatically cleared after each translation to prevent unintended effects.
18
+ - **Translation Instructions:** Pass additional instructions (e.g., "Use natural tone") to guide the AI translator's style.
18
19
 
19
20
  ## 📦 Installation
20
21
 
21
22
  ```bash
22
23
  npm install gptrans
23
24
  ```
25
+
24
26
  > **AI Skill**: You can also add GPTrans as a skill for AI agentic development:
25
27
  > ```bash
26
28
  > npx skills add https://github.com/clasen/GPTrans --skill gptrans
27
29
  > ```
30
+
28
31
  ### 🌐 Environment Setup
29
32
 
30
33
  GPTrans uses dotenv for environment configuration. Create a `.env` file in your project root and add your API keys:
@@ -76,6 +79,7 @@ When creating a new instance of GPTrans, you can customize:
76
79
  | `model` | Translation model key or array of models for fallback | `sonnet45` `gpt41` |
77
80
  | `batchThreshold` | Maximum number of characters to accumulate before triggering batch processing | `1500` |
78
81
  | `debounceTimeout` | Time in milliseconds to wait before processing translations | `500` |
82
+ | `instruction` | Additional instruction for the translator (e.g., tone, style). Does not affect the cache key | `''` |
79
83
  | `freeze` | Freeze mode to prevent translations from being queued | `false` |
80
84
 
81
85
  ### BCP 47 Language Tags
@@ -120,6 +124,31 @@ GPTrans stands out by combining advanced AI capabilities with efficient batching
120
124
 
121
125
  If you're looking to streamline your translation workflow and bring your applications to a global audience effortlessly, GPTrans is the perfect choice!
122
126
 
127
+ ## 📝 Translation Instructions
128
+
129
+ The `instruction` option lets you guide the AI translator's style, tone, or behavior without creating a separate cache entry. Unlike `context` (which produces separate translations for each unique context), `instruction` does not affect the cache key — so the same text with the same context always maps to the same cache entry regardless of the instruction used.
130
+
131
+ ### Usage
132
+
133
+ ```javascript
134
+ const gptrans = new GPTrans({
135
+ from: 'en',
136
+ target: 'es-AR',
137
+ instruction: 'Use natural and colloquial tone'
138
+ });
139
+
140
+ console.log(gptrans.t('Welcome to our platform'));
141
+ console.log(gptrans.t('Please verify your identity'));
142
+ ```
143
+
144
+ ### Instruction vs Context
145
+
146
+ | | `context` | `instruction` |
147
+ |---|---|---|
148
+ | **Purpose** | Semantic information (gender, domain) | Style guidance (tone, formality) |
149
+ | **Affects cache key** | Yes — different contexts create separate translations | No — same key regardless of instruction |
150
+ | **Example** | `'Use natural tone'` |
151
+
123
152
  ## 🔄 Preloading Translations
124
153
 
125
154
  The `preload()` method allows you to pre-translate all texts in your database. It now supports advanced options for improved translation accuracy through reference translations and alternate base languages.
package/index.js CHANGED
@@ -64,7 +64,7 @@ class GPTrans {
64
64
  return isLanguageAvailable(langCode);
65
65
  }
66
66
 
67
- constructor({ from = 'en-US', target = 'es', model = 'sonnet45', batchThreshold = 1500, debounceTimeout = 500, promptFile = null, name = '', context = '', freeze = false, debug = false } = {}) {
67
+ constructor({ from = 'en-US', target = 'es', model = 'sonnet45', batchThreshold = 1500, debounceTimeout = 500, promptFile = null, name = '', context = '', instruction = '', freeze = false, debug = false } = {}) {
68
68
 
69
69
  target = this.normalizeBCP47(target);
70
70
  from = this.normalizeBCP47(from);
@@ -99,6 +99,7 @@ class GPTrans {
99
99
  this.modelKey = model;
100
100
  this.promptFile = promptFile ?? new URL('./prompt/translate.md', import.meta.url).pathname;
101
101
  this.context = context;
102
+ this.instruction = instruction;
102
103
  this.freeze = freeze;
103
104
  this.modelConfig = {
104
105
  options: {
@@ -322,6 +323,7 @@ class GPTrans {
322
323
  model.replace({
323
324
  '{INPUT}': text,
324
325
  '{CONTEXT}': this.context,
326
+ '{INSTRUCTION}': this.instruction,
325
327
  '{REFERENCES}': referencesText,
326
328
  '{TARGET_ISO}': this.replaceTarget.TARGET_ISO,
327
329
  '{TARGET_LANG}': this.replaceTarget.TARGET_LANG,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gptrans",
3
3
  "type": "module",
4
- "version": "2.0.2",
4
+ "version": "2.0.6",
5
5
  "description": "🚆 GPTrans - The smarter AI-powered way to translate.",
6
6
  "keywords": [
7
7
  "translate",
@@ -1,5 +1,6 @@
1
1
  # Goal
2
2
  Translation from {FROM_ISO} to {TARGET_ISO} ({TARGET_DENONYM} {TARGET_LANG}) with cultural adaptations.
3
+ {INSTRUCTION}
3
4
 
4
5
  ## Text to translate
5
6
  ```
@@ -22,4 +23,4 @@ Translation from {FROM_ISO} to {TARGET_ISO} ({TARGET_DENONYM} {TARGET_LANG}) wit
22
23
 
23
24
 
24
25
  # Context
25
- {CONTEXT}
26
+ {CONTEXT}
@@ -110,6 +110,7 @@ console.log(gptrans.setContext().t('Welcome back'));
110
110
  | `freeze` | `boolean` | `false` | Prevent new translations from being queued |
111
111
  | `name` | `string` | `''` | Instance name (isolates cache files) |
112
112
  | `context` | `string` | `''` | Default context for translations |
113
+ | `instruction` | `string` | `''` | Additional instruction for the translator (tone, style). Does not affect cache key |
113
114
  | `promptFile` | `string` | `null` | Custom prompt file path (overrides built-in) |
114
115
  | `debug` | `boolean` | `false` | Enable debug output |
115
116
 
@@ -186,6 +187,17 @@ console.log(gptrans.setContext('The user is female').t('You are welcome'));
186
187
  console.log(gptrans.setContext().t('Thank you'));
187
188
  ```
188
189
 
190
+ ### Set instruction for translation style
191
+
192
+ ```javascript
193
+ const gptrans = new GPTrans({
194
+ from: 'en', target: 'es-AR',
195
+ instruction: 'Use a formal and professional tone'
196
+ });
197
+
198
+ console.log(gptrans.t('Welcome to our platform'));
199
+ ```
200
+
189
201
  ### Refine existing translations
190
202
 
191
203
  Improve cached translations with specific instructions:
@@ -276,6 +288,7 @@ if (GPTrans.isLanguageAvailable('pt-BR')) {
276
288
  - The `t()` method is synchronous and non-blocking. It returns the original text on first call and the cached translation on subsequent calls. Do NOT `await` it.
277
289
  - To ensure translations are complete before using them, call `await gptrans.preload()` after registering texts with `t()`.
278
290
  - Use `setContext()` for gender-aware or domain-specific translations. Context is captured per-batch and auto-resets when changed.
291
+ - Use the `instruction` constructor option for style/tone guidance (e.g., "Use a more natural tone"). Unlike `context`, `instruction` does NOT affect the cache key — different instructions for the same text overwrite the same translation entry.
279
292
  - Prefer passing an array of instructions to `refine()` over multiple calls — it processes everything in a single API pass.
280
293
  - Use model arrays (`model: ['sonnet45', 'gpt41']`) for production resilience with automatic fallback.
281
294
  - Translation caches live in `db/gptrans_<locale>.json`. These files can be manually edited to override specific translations.