Skip to content

Commit e11234d

Browse files
feat: Add tag_handling_version parameter to translate_text
1 parent b6fd569 commit e11234d

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88
### Added
9+
- Added `tag_handling_version` parameter to `translate_text()` to specify which version
10+
of the tag handling algorithm to use. Options are `v1` and `v2`.
911
- Added an example CLI script for realtime audio translation using DeepL's Voice API.
1012

1113
## [1.26.0] - 2025-12-03

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ arguments are:
173173
model that minimizes response time, at the cost of translation quality.
174174
- `tag_handling`: type of tags to parse before translation, options are `'html'`
175175
and `'xml'`.
176+
- `tag_handling_version`: specifies which version of the tag handling algorithm to
177+
use, options are `'v1'` and `'v2'`.
176178
- `style_rule`: specifies a style rule to use with translation, either as a string
177179
containing the ID of the style rule, or a `StyleRuleInfo` object.
178180
- `custom_instructions`: an array of instructions to customize the text

deepl/__main__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,13 @@ def add_common_arguments(subparser: argparse.ArgumentParser):
422422
default=None,
423423
help="activate processing of formatting tags, for example 'xml'",
424424
)
425+
tag_handling_group.add_argument(
426+
"--tag-handling-version",
427+
type=str,
428+
choices=["v1", "v2"],
429+
default=None,
430+
help="specify which version of the tag handling algorithm to use",
431+
)
425432
tag_handling_group.add_argument(
426433
"--outline-detection-off",
427434
dest="outline_detection",

deepl/translator.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ def translate_text(
376376
str, GlossaryInfo, MultilingualGlossaryInfo, None
377377
] = None,
378378
tag_handling: Optional[str] = None,
379+
tag_handling_version: Optional[str] = None,
379380
outline_detection: Optional[bool] = None,
380381
non_splitting_tags: Union[str, List[str], None] = None,
381382
splitting_tags: Union[str, List[str], None] = None,
@@ -414,6 +415,8 @@ def translate_text(
414415
translation.
415416
:param tag_handling: (Optional) Type of tags to parse before
416417
translation, only "xml" and "html" are currently available.
418+
:param tag_handling_version: (Optional) Version of tag handling
419+
algorithm to use, "v1" or "v2".
417420
:param outline_detection: (Optional) Set to False to disable automatic
418421
tag detection.
419422
:param non_splitting_tags: (Optional) XML tags that should not split a
@@ -469,6 +472,8 @@ def translate_text(
469472
request_data["preserve_formatting"] = bool(preserve_formatting)
470473
if tag_handling is not None:
471474
request_data["tag_handling"] = tag_handling
475+
if tag_handling_version is not None:
476+
request_data["tag_handling_version"] = tag_handling_version
472477
if outline_detection is not None:
473478
request_data["outline_detection"] = bool(outline_detection)
474479
if model_type is not None:

tests/test_translate_text.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,25 @@ def test_custom_instructions(translator):
443443
assert (
444444
custom_instructions_result.text != no_custom_instructions_result.text
445445
)
446+
447+
448+
@needs_real_server
449+
def test_tag_handling_version_v1(translator):
450+
text = "<p>Hello world</p>"
451+
result = translator.translate_text(
452+
text, target_lang="DE", tag_handling="html", tag_handling_version="v1"
453+
)
454+
assert isinstance(result, deepl.TextResult)
455+
assert result.text is not None
456+
assert len(result.text) > 0
457+
458+
459+
@needs_real_server
460+
def test_tag_handling_version_v2(translator):
461+
text = "<p>Hello world</p>"
462+
result = translator.translate_text(
463+
text, target_lang="DE", tag_handling="html", tag_handling_version="v2"
464+
)
465+
assert isinstance(result, deepl.TextResult)
466+
assert result.text is not None
467+
assert len(result.text) > 0

0 commit comments

Comments
 (0)