Skip to content

Commit 5942bdf

Browse files
feat: Add tag_handling_version parameter to translate_text
1 parent 23f0117 commit 5942bdf

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Added `tagHandlingVersion` parameter to `translateText()` to specify which version of the tag handling algorithm to use. Options are `v1` and `v2`.
810

911
## [1.23.0] - 2025-12-03
1012
### Added

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ console.log(await deeplClient.translateText('How are you?', null, 'de', { formal
198198
time, at the cost of translation quality.
199199
- `tagHandling`: type of tags to parse before translation, options are `'html'`
200200
and `'xml'`.
201+
- `tagHandlingVersion`: specifies which version of the tag handling algorithm to use.
202+
Options are `v1` and `v2`.
201203

202204
The following options are only used if `tagHandling` is `'xml'`:
203205

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export interface DeepLClientOptions extends TranslatorOptions {}
8484
export type Formality = 'less' | 'more' | 'default' | 'prefer_less' | 'prefer_more';
8585
export type SentenceSplittingMode = 'off' | 'on' | 'nonewlines' | 'default';
8686
export type TagHandlingMode = 'html' | 'xml';
87+
export type TagHandlingVersion = 'v1' | 'v2';
8788
export type ModelType = 'quality_optimized' | 'latency_optimized' | 'prefer_quality_optimized';
8889
export type GlossaryId = string;
8990
export type StyleId = string;
@@ -163,6 +164,9 @@ export interface TranslateTextOptions extends BaseRequestOptions {
163164
/** Type of tags to parse before translation, options are 'html' and 'xml'. */
164165
tagHandling?: TagHandlingMode;
165166

167+
/** Version of tag handling to use, options are 'v1' and 'v2'. */
168+
tagHandlingVersion?: TagHandlingVersion;
169+
166170
/** Set to false to disable automatic tag detection, default is true. */
167171
outlineDetection?: boolean;
168172

src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ export function validateAndAppendTextOptions(
257257
if (options.tagHandling !== undefined) {
258258
data.append('tag_handling', options.tagHandling);
259259
}
260+
if (options.tagHandlingVersion !== undefined) {
261+
data.append('tag_handling_version', options.tagHandlingVersion);
262+
}
260263
if (options.outlineDetection !== undefined) {
261264
data.append('outline_detection', toBoolString(options.outlineDetection));
262265
}

tests/translateText.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,26 @@ describe('translate text', () => {
213213
await translator.translateText(text, null, 'de', { tagHandling: 'html' });
214214
});
215215

216+
it('should translate with tag_handling_version option', async () => {
217+
const text = '<p>Hello world</p>';
218+
const translator = makeTranslator();
219+
// Test with v1
220+
const resultV1 = await translator.translateText(text, null, 'de', {
221+
tagHandling: 'html',
222+
tagHandlingVersion: 'v1',
223+
});
224+
expect(resultV1.text).toBeTruthy();
225+
expect(resultV1.detectedSourceLang).toBe('en');
226+
227+
// Test with v2
228+
const resultV2 = await translator.translateText(text, null, 'de', {
229+
tagHandling: 'html',
230+
tagHandlingVersion: 'v2',
231+
});
232+
expect(resultV2.text).toBeTruthy();
233+
expect(resultV2.detectedSourceLang).toBe('en');
234+
});
235+
216236
it('should translate with context option', async () => {
217237
const translator = makeTranslator();
218238
const text = 'Das ist scharf!';

0 commit comments

Comments
 (0)