Skip to content

Commit ba8fd72

Browse files
feat: Add tag_handling_version parameter to translate_text
1 parent c21247f commit ba8fd72

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-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 `tag_handling_version` parameter to `translateText()` specify which version of the tag handling algorithm to use. Options are `v1` and `v2`.
810

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

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ using the following keys:
132132
- `'prefer_more'`: use formal, more polite language if available, otherwise default.
133133
- `tag_handling`: type of tags to parse before translation, options are `'html'`
134134
and `'xml'`.
135+
- `tag_handling_version`: specifies which version of the tag handling algorithm to use.
136+
Options are `v1` and `v2`. This parameter is only relevant when `tag_handling` is set to
137+
`'html'` or `'xml'`.
135138
- `context`: specifies additional context to influence translations, that is not
136139
translated itself. Characters in the `context` parameter are not counted toward billing.
137140
See the [API documentation][api-docs-context-param] for more information and

src/TranslateTextOptions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class TranslateTextOptions
4545
/** Type of tags to parse before translation, options are 'html' and 'xml'. */
4646
public const TAG_HANDLING = 'tag_handling';
4747

48+
/** Version of tag handling algorithm to use, options are 'v1' and 'v2'. */
49+
public const TAG_HANDLING_VERSION = 'tag_handling_version';
50+
4851
/** Set to false to disable automatic tag detection, default is true. */
4952
public const OUTLINE_DETECTION = 'outline_detection';
5053

src/Translator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,9 @@ private function validateAndAppendTextOptions(array &$params, ?array $options):
671671
if (isset($options[TranslateTextOptions::TAG_HANDLING])) {
672672
$params[TranslateTextOptions::TAG_HANDLING] = $options[TranslateTextOptions::TAG_HANDLING];
673673
}
674+
if (isset($options[TranslateTextOptions::TAG_HANDLING_VERSION])) {
675+
$params[TranslateTextOptions::TAG_HANDLING_VERSION] = $options[TranslateTextOptions::TAG_HANDLING_VERSION];
676+
}
674677
if (isset($options[TranslateTextOptions::OUTLINE_DETECTION])) {
675678
$params[TranslateTextOptions::OUTLINE_DETECTION] =
676679
$this->toBoolString($options[TranslateTextOptions::OUTLINE_DETECTION]);

tests/TranslateTextTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,32 @@ public function testHandlingResponseWithInvalidUtf8(?ClientInterface $httpClient
9797
$this->assertInstanceOf(TextResult::class, $result);
9898
}
9999

100+
/**
101+
* @dataProvider provideHttpClient
102+
*/
103+
public function testTagHandlingVersion(?ClientInterface $httpClient)
104+
{
105+
$this->needsRealServer();
106+
$translator = $this->makeTranslator([TranslatorOptions::HTTP_CLIENT => $httpClient]);
107+
$text = '<p>Hello world</p>';
108+
109+
$resultV1 = $translator->translateText($text, 'en', 'de', [
110+
TranslateTextOptions::TAG_HANDLING => 'html',
111+
TranslateTextOptions::TAG_HANDLING_VERSION => 'v1',
112+
]);
113+
$this->assertInstanceOf(TextResult::class, $resultV1);
114+
$this->assertNotNull($resultV1->text);
115+
$this->assertNotEmpty($resultV1->text);
116+
117+
$resultV2 = $translator->translateText($text, 'en', 'de', [
118+
TranslateTextOptions::TAG_HANDLING => 'html',
119+
TranslateTextOptions::TAG_HANDLING_VERSION => 'v2',
120+
]);
121+
$this->assertInstanceOf(TextResult::class, $resultV2);
122+
$this->assertNotNull($resultV2->text);
123+
$this->assertNotEmpty($resultV2->text);
124+
}
125+
100126
/**
101127
* @dataProvider provideHttpClient
102128
*/

0 commit comments

Comments
 (0)