cc-translate 1.3.0 → 1.3.3
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 +128 -10
- package/index.d.ts +5 -4
- package/index.js +417 -119
- package/lib/cache/index.d.ts +9 -3
- package/lib/cache/index.d.ts.map +1 -1
- package/lib/cache/index.js +87 -4
- package/lib/log/index.d.ts +1 -1
- package/lib/log/index.d.ts.map +1 -1
- package/lib/support.d.ts +1 -1
- package/lib/support.d.ts.map +1 -1
- package/lib/translation-format.d.ts +5 -0
- package/lib/translation-format.d.ts.map +1 -0
- package/lib/translation-format.js +31 -0
- package/lib/translation-metadata.d.ts +12 -0
- package/lib/translation-metadata.d.ts.map +1 -0
- package/lib/translation-metadata.js +94 -0
- package/lib/translation-reference.d.ts +4 -2
- package/lib/translation-reference.d.ts.map +1 -1
- package/lib/translation-reference.js +52 -35
- package/lib/translation-response.d.ts +9 -0
- package/lib/translation-response.d.ts.map +1 -0
- package/lib/translation-response.js +169 -0
- package/lib/utils.d.ts +1 -1
- package/lib/utils.d.ts.map +1 -1
- package/package.json +3 -2
- package/types/index.d.ts +46 -10
- package/types/index.d.ts.map +1 -1
- package/CHANGELOG.md +0 -41
package/README.md
CHANGED
|
@@ -45,6 +45,7 @@ const agent = new I18nTranslateAgent({
|
|
|
45
45
|
"This is a crypto wallet product",
|
|
46
46
|
"Keep blockchain terms consistent",
|
|
47
47
|
],
|
|
48
|
+
noTranslate: ["USDC", "Cozy Card"],
|
|
48
49
|
chatCompletionCreateParams: {
|
|
49
50
|
model: "gpt-4o",
|
|
50
51
|
temperature: 0.2,
|
|
@@ -72,26 +73,142 @@ const reference = new TranslationReferencePlugin({
|
|
|
72
73
|
await agent.translate(reference);
|
|
73
74
|
```
|
|
74
75
|
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
Pass one reference plugin directly. To translate without a reference, call
|
|
77
|
+
`await agent.translate()`. That mode uses the source and ordinary source
|
|
78
|
+
cache and leaves existing `.references` files untouched.
|
|
79
|
+
|
|
80
|
+
When a reference matches, it determines the meaning to translate. File-level
|
|
81
|
+
`_meta.context` provides secondary guidance only where it agrees with the
|
|
82
|
+
reference.
|
|
83
|
+
|
|
84
|
+
The cache compares source text and tracked reference text for each target
|
|
85
|
+
language. Changing a reference value requeues the corresponding key even if
|
|
86
|
+
the source text is unchanged. A newly filled key also requeues once the
|
|
87
|
+
reference file has tracked values for that target. Clearing or deleting a
|
|
88
|
+
reference leaves existing translations intact.
|
|
89
|
+
|
|
90
|
+
On first use, the current matches become the baseline for entries with a
|
|
91
|
+
non-blank target translation and a source cache hit. Missing source cache
|
|
92
|
+
files or entries still trigger translation, even if the target file already
|
|
93
|
+
contains a translation. The source and reference caches for those entries are
|
|
94
|
+
written after successful translation.
|
|
95
|
+
When a changed reference requeues a key, the model sees the new reference and
|
|
96
|
+
the required source placeholders. This request omits the original wording,
|
|
97
|
+
file context, and global `fineTune` so previous terminology cannot steer the
|
|
98
|
+
translation.
|
|
99
|
+
|
|
100
|
+
For every locale, local checks reject unsupported writing systems, verbatim
|
|
101
|
+
echoes of a different reference language, and confidently identified text in
|
|
102
|
+
another language even when it uses the same writing system. A rejected
|
|
103
|
+
reference-based response falls back to the source in the same run. This uses
|
|
104
|
+
offline, sentence-level language detection and makes no model verification
|
|
105
|
+
calls. Short labels with uncertain language remain eligible. The check
|
|
106
|
+
allows plausible translations when the detector confuses Malay with
|
|
107
|
+
Indonesian or Nepali with Hindi.
|
|
108
|
+
|
|
109
|
+
When English is the source, identifiable foreign text within an English
|
|
110
|
+
entry is preserved, along with common notation such as `Δ` and `µm`.
|
|
111
|
+
Only the English portion is translated. Completely non-English entries are
|
|
112
|
+
skipped and logged with the source filename and key. File metadata such as
|
|
113
|
+
`_meta.context` can be written in Chinese or another language.
|
|
114
|
+
|
|
115
|
+
The script preserves common React and React Native i18n syntax, including
|
|
116
|
+
i18next `{{name}}` and `{{- name}}`, JSX-style tags such as `<0>…</0>`, ICU
|
|
117
|
+
arguments and their plural or select blocks, and placeholders such as
|
|
118
|
+
`%{name}` and `%1$s`. Text between rich-text tags can still be translated.
|
|
119
|
+
Placeholder changes cause the response to be retried. For a custom literal
|
|
120
|
+
format, add the exact text to `noTranslate`.
|
|
121
|
+
|
|
122
|
+
Only matched, non-blank references are saved under
|
|
123
|
+
`<cache>/<target-language>/.references/`. After initialization, the snapshot
|
|
124
|
+
records the reference actually sent with a successful translation. Entries
|
|
125
|
+
translated from the source alone update the ordinary source cache and remove
|
|
126
|
+
the corresponding key from `.references`. After a source fallback, the
|
|
127
|
+
ordinary cache stores a digest in `_meta.rejectedReferences` so an unchanged
|
|
128
|
+
reference is skipped on the next run. Editing the reference requeues the key.
|
|
129
|
+
The digest stores no reference text. The source and reference files are
|
|
130
|
+
read-only during translation.
|
|
131
|
+
Missing or blank reference values are omitted. Legacy `null` entries are
|
|
132
|
+
migrated with their reference identities, so filling one later still
|
|
133
|
+
triggers translation even if another reference plugin ran first.
|
|
134
|
+
|
|
77
135
|
Before any model calls, the plugin reports the reference language, directory,
|
|
78
136
|
file matches, and coverage of pending keys.
|
|
79
137
|
|
|
80
138
|
- When every pending entry has a non-empty reference, translation starts automatically.
|
|
81
|
-
- Missing files or entries
|
|
82
|
-
|
|
83
|
-
terminal ends the run before
|
|
139
|
+
- Missing files or entries in the reference directory trigger a terminal
|
|
140
|
+
prompt. If you confirm, unmatched entries are translated from the source.
|
|
141
|
+
Declining or running without an interactive terminal ends the run before
|
|
142
|
+
translation.
|
|
84
143
|
- A missing or unreadable reference directory, or an invalid matching JSON
|
|
85
144
|
file, stops the run with an error that identifies the path.
|
|
86
145
|
|
|
87
146
|
The reference locale is read-only and its language is excluded from the output
|
|
88
147
|
targets. By default, the translation request includes both the source text and
|
|
89
148
|
its matched reference. Set `checkConflicts: true` to run a separate check before
|
|
90
|
-
translation.
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
149
|
+
translation. The check defaults to off. When enabled, each matched pending
|
|
150
|
+
key and each new reference baseline is checked once before any baseline is
|
|
151
|
+
saved. Confirmed conflicts and checks that still fail after retries receive
|
|
152
|
+
distinct entries in `error.log`, once per filename and key. A confirmed
|
|
153
|
+
conflict makes all targets use the source for that key, including targets
|
|
154
|
+
whose ordinary cache already matched. The usual `_meta.context`, `fineTune`,
|
|
155
|
+
and term protection apply. A check that fails after retries leaves that key
|
|
156
|
+
pending for the next run.
|
|
157
|
+
|
|
158
|
+
## File Context and Protected Terms
|
|
159
|
+
|
|
160
|
+
Add an optional `_meta` object at the top level of a **source-language** JSON
|
|
161
|
+
file, such as `locales/en/bots.json`.
|
|
162
|
+
|
|
163
|
+
```json
|
|
164
|
+
{
|
|
165
|
+
"_meta": {
|
|
166
|
+
"context": "Trading bot settings for opening positions and configuring margin.",
|
|
167
|
+
"noTranslate": ["MAX", "Cozy Card"]
|
|
168
|
+
},
|
|
169
|
+
"keyword.long": "Long",
|
|
170
|
+
"trade.hint": "Tap MAX to use your full balance.",
|
|
171
|
+
"trade.max": "MAX"
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
| Field | Accepted value | Behavior |
|
|
176
|
+
| --- | --- | --- |
|
|
177
|
+
| `context` | Non-blank string | Adds business context to every translation request for this file. When a reference is available, use this context only where it agrees with the reference. |
|
|
178
|
+
| `noTranslate` | Non-empty array of non-blank strings | Preserves the listed words or phrases in this file. Entries are terms such as `MAX` or `Cozy Card`, with case-sensitive, whole-term matching. |
|
|
179
|
+
|
|
180
|
+
Both fields are optional and validated independently. For example, a valid
|
|
181
|
+
`context` still applies if `noTranslate` is accidentally written as a string.
|
|
182
|
+
Missing or invalid metadata is ignored. The script reads metadata from the
|
|
183
|
+
source file and excludes `_meta` from generated translations and caches.
|
|
184
|
+
|
|
185
|
+
In the example, `trade.hint` can become `点击 MAX 使用全部余额。` in Chinese.
|
|
186
|
+
The surrounding sentence is translated while `MAX` is restored verbatim.
|
|
187
|
+
The word `maximum` can still be translated. A value that consists entirely of
|
|
188
|
+
a protected term, such as `trade.max`, is copied directly to the target locale.
|
|
189
|
+
If a model response drops or duplicates a protected placeholder, the script
|
|
190
|
+
uses the existing retry policy and leaves the entry pending if retries are
|
|
191
|
+
exhausted.
|
|
192
|
+
|
|
193
|
+
### Global protected terms
|
|
194
|
+
|
|
195
|
+
Set `noTranslate: ["USDC", "Cozy Card"]` on `I18nTranslateAgent` to protect
|
|
196
|
+
these terms in every source JSON file. Keep application-wide names and symbols
|
|
197
|
+
in this configuration, and put file-specific terms in `_meta.noTranslate`.
|
|
198
|
+
Both lists use the same validation and matching rules. For a long list, keep it
|
|
199
|
+
in a separate project configuration file and import it into the entry script.
|
|
200
|
+
|
|
201
|
+
The script matches file-level terms first, then global terms, using the
|
|
202
|
+
original source text for both. A full phrase such as `Tip Box` stays intact
|
|
203
|
+
when a shorter term such as `Tip` also matches it. Distinct phrases such as
|
|
204
|
+
`Two Tip` and `Tip Box` are each preserved. If they share the same occurrence
|
|
205
|
+
of `Tip` in `Two Tip Box`, the script protects the combined span once.
|
|
206
|
+
|
|
207
|
+
Editing either list or the context alone does not retranslate existing
|
|
208
|
+
sentences. If a value is entirely a protected term, the script repairs a
|
|
209
|
+
different target value by copying the source directly, even on a cache hit.
|
|
210
|
+
To apply a rule change to an existing sentence, clear its key from the
|
|
211
|
+
relevant target-language caches.
|
|
95
212
|
|
|
96
213
|
## Directory Layout
|
|
97
214
|
|
|
@@ -121,6 +238,7 @@ your-project/
|
|
|
121
238
|
| `outputRootPath` | `string` | No | Output directory, defaults to `fileRootPath` |
|
|
122
239
|
| `concurrency` | `number` | No | Queue length, default `8` |
|
|
123
240
|
| `fineTune` | `string[]` | No | Extra translation context lines |
|
|
241
|
+
| `noTranslate` | `readonly string[]` | No | Terms preserved across source files. Full protected phrases include their shorter matches. |
|
|
124
242
|
| `chatCompletionCreateParams` | `Partial<ChatCompletionCreateParams>` | No | Extra chat completion params |
|
|
125
243
|
|
|
126
244
|
### Utility Exports
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ClientOptions, OpenAI } from "openai";
|
|
2
|
-
import { II18nTranslateAgentParams, IJson, IOutputLanguageFile, ISingleTranslate, ITranslateChat, ITranslateChatResponse, SupportLanguageType } from "./types";
|
|
2
|
+
import { II18nTranslateAgentParams, IJson, IOutputLanguageFile, ISingleTranslate, ITranslateChat, ITranslateChatResponse, SourceCache, SupportLanguageType } from "./types/index.js";
|
|
3
3
|
import { TranslationReferencePlugin } from "./lib/translation-reference.js";
|
|
4
4
|
import { ChatCompletionCreateParams } from "openai/resources";
|
|
5
5
|
export { generateCache, deleteBatchCache } from "./lib/cache/index.js";
|
|
@@ -21,6 +21,7 @@ export declare class I18nTranslateAgent {
|
|
|
21
21
|
client: OpenAI | null;
|
|
22
22
|
/** 微调模型 */
|
|
23
23
|
fineTune: string[];
|
|
24
|
+
readonly noTranslate?: readonly string[];
|
|
24
25
|
/** 翻译任务并发数(队列长度),完成一个即取下一个 */
|
|
25
26
|
concurrency: number;
|
|
26
27
|
/** openai 客户端配置 */
|
|
@@ -32,11 +33,11 @@ export declare class I18nTranslateAgent {
|
|
|
32
33
|
taskRetryBaseDelayMs: number;
|
|
33
34
|
constructor(params: II18nTranslateAgentParams);
|
|
34
35
|
/** 支持翻译的语言列表 */
|
|
35
|
-
get supportLanguages(): import("./types").ILanguage[];
|
|
36
|
+
get supportLanguages(): import("./types/index.js").ILanguage[];
|
|
36
37
|
/** 输出文件根路径 */
|
|
37
38
|
get outputPath(): string;
|
|
38
39
|
/** 搜索语言 */
|
|
39
|
-
searchLanguage(code: SupportLanguageType): import("./types").ILanguage | undefined;
|
|
40
|
+
searchLanguage(code: SupportLanguageType): import("./types/index.js").ILanguage | undefined;
|
|
40
41
|
/** 创建openai客户端 */
|
|
41
42
|
createOpenAIClient: () => void;
|
|
42
43
|
/**
|
|
@@ -65,7 +66,7 @@ export declare class I18nTranslateAgent {
|
|
|
65
66
|
* @param fileName
|
|
66
67
|
* @returns
|
|
67
68
|
*/
|
|
68
|
-
getTranslateContent: (language: SupportLanguageType, fileName: string, deferredEmptyFiles?: IOutputLanguageFile[]) => Promise<IJson | undefined>;
|
|
69
|
+
getTranslateContent: (language: SupportLanguageType, fileName: string, deferredEmptyFiles?: IOutputLanguageFile[], sourceEntries?: IJson, sourceCache?: SourceCache) => Promise<IJson | undefined>;
|
|
69
70
|
/**
|
|
70
71
|
* 输出语言文件
|
|
71
72
|
* @param {Object} jsonMap
|