Skip to content

Commit 012d30c

Browse files
author
Lucas Mathis
committed
feat: add support for extra request params
1 parent 5c9dea1 commit 012d30c

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
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

7+
## [Unreleased]
8+
9+
### Added
10+
* Added `extraRequestParameters` option to text and document translation methods to pass arbitrary parameters in the request body. This can be used to access beta features or override built-in parameters (such as `target_lang`, `source_lang`, etc.).
11+
712
## [1.12.0] - 2025-04-25
813
### Added
914
* Added support for the /v3 Multilingual Glossary APIs in the client library

src/TranslateDocumentOptions.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@ final class TranslateDocumentOptions
2727

2828
/** Set to `true` in order to use Document Minification for translation, if available. */
2929
public const ENABLE_DOCUMENT_MINIFICATION = 'enable_document_minification';
30+
31+
/** Dictionary of extra parameters to pass in the body of the HTTP request.
32+
* Can be used to access beta features, override built-in parameters, or for testing purposes.
33+
* Keys in this array will be added to the request body and can override existing keys.
34+
* Values must be of string type.
35+
*/
36+
public const EXTRA_BODY_PARAMETERS = 'extra_body_parameters';
3037
}

src/TranslateTextOptions.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,11 @@ class TranslateTextOptions
7979
* - 'latency_optimized': Use translation models that have been optimized for translation speed.
8080
*/
8181
public const MODEL_TYPE = 'model_type';
82+
83+
/** Dictionary of extra parameters to pass in the body of the HTTP request.
84+
* Can be used to access beta features, override built-in parameters, or for testing purposes.
85+
* Keys in this array will be added to the request body and can override existing keys.
86+
* Values must be of string type.
87+
*/
88+
public const EXTRA_BODY_PARAMETERS = 'extra_body_parameters';
8289
}

src/Translator.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ public function uploadDocument(
272272
$options[TranslateDocumentOptions::GLOSSARY] ?? null
273273
);
274274

275+
$this->applyExtraBodyParameters(
276+
$params,
277+
$options[TranslateDocumentOptions::EXTRA_BODY_PARAMETERS] ?? null
278+
);
279+
275280
$response = $this->client->sendRequestWithBackoff(
276281
'POST',
277282
'/v2/document',
@@ -688,6 +693,23 @@ private function validateAndAppendTextOptions(array &$params, ?array $options):
688693
$params[TranslateTextOptions::IGNORE_TAGS] =
689694
$this->joinTagList($options[TranslateTextOptions::IGNORE_TAGS]);
690695
}
696+
$this->applyExtraBodyParameters(
697+
$params,
698+
$options[TranslateTextOptions::EXTRA_BODY_PARAMETERS] ?? null
699+
);
700+
}
701+
702+
/**
703+
* Adds extra body parameters to the params array. Extra parameters can override existing keys.
704+
* Values are converted to strings.
705+
*/
706+
private function applyExtraBodyParameters(array &$params, ?array $extraParams): void
707+
{
708+
if ($extraParams !== null) {
709+
foreach ($extraParams as $key => $value) {
710+
$params[$key] = (string)$value;
711+
}
712+
}
691713
}
692714

693715
/**

tests/TranslateTextTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,4 +395,24 @@ public function testTagHandlingHTML(?ClientInterface $httpClient)
395395
$this->assertStringContainsString('<h1>Meine erste Überschrift</h1>', $result->text);
396396
$this->assertStringContainsString('<p translate="no">My first paragraph.</p>', $result->text);
397397
}
398+
399+
/**
400+
* @dataProvider provideHttpClient
401+
*/
402+
public function testExtraBodyParams(?ClientInterface $httpClient)
403+
{
404+
$translator = $this->makeTranslator([TranslatorOptions::HTTP_CLIENT => $httpClient]);
405+
406+
$extra = ['target_lang' => 'FR', 'debug' => '1'];
407+
$result = $translator->translateText(
408+
DeepLTestBase::EXAMPLE_TEXT['en'],
409+
null,
410+
'de',
411+
[TranslateTextOptions::EXTRA_BODY_PARAMETERS => $extra]
412+
);
413+
414+
$this->assertEquals(DeepLTestBase::EXAMPLE_TEXT['fr'], $result->text);
415+
$this->assertEquals('en', $result->detectedSourceLang);
416+
$this->assertEquals(mb_strlen(DeepLTestBase::EXAMPLE_TEXT['en']), $result->billedCharacters);
417+
}
398418
}

0 commit comments

Comments
 (0)