Skip to content

Commit 63d0331

Browse files
feat: add context parameter (alpha feature)
1 parent 1a067dd commit 63d0331

5 files changed

Lines changed: 46 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +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+
78
## [Unreleased]
9+
### Added
10+
* Add optional `context` parameter for text translation, that specifies
11+
additional context to influence translations, that is not translated itself.
812
### Fixed
913
* Remove unused `commons-math` dependency
1014

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ a `TextTranslationOptions`, with the following setters:
159159
returned by glossary lookup functions, for example `listGlossaries()`).
160160
- `setGlossaryId()` is also available for backward-compatibility, accepting
161161
a string containing the glossary ID.
162+
- `setContext()`: specifies additional context to influence translations, that is not
163+
translated itself. Note this is an **alpha feature**: it may be deprecated at
164+
any time, or incur charges if it becomes generally available.
165+
See the [API documentation][api-docs-context-param] for more information and
166+
example usage.
162167
- `setTagHandling()`: type of tags to parse before translation, options are
163168
`"html"` and `"xml"`.
164169

@@ -623,6 +628,8 @@ tests using `./gradlew test` with the `DEEPL_MOCK_SERVER_PORT` and
623628

624629
[api-docs]: https://www.deepl.com/docs-api?utm_source=github&utm_medium=github-java-readme
625630

631+
[api-docs-context-param]: https://www.deepl.com/docs-api/translating-text/?utm_source=github&utm_medium=github-java-readme
632+
626633
[api-docs-csv-format]: https://www.deepl.com/docs-api/managing-glossaries/supported-glossary-formats/?utm_source=github&utm_medium=github-java-readme
627634

628635
[api-docs-xml-handling]: https://www.deepl.com/docs-api/handling-xml/?utm_source=github&utm_medium=github-java-readme

deepl-java/src/main/java/com/deepl/api/TextTranslationOptions.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class TextTranslationOptions {
1818
private String glossaryId;
1919
private SentenceSplittingMode sentenceSplittingMode;
2020
private boolean preserveFormatting = false;
21+
private String context;
2122
private String tagHandling;
2223
private boolean outlineDetection = true;
2324
private Iterable<String> ignoreTags;
@@ -63,6 +64,16 @@ public TextTranslationOptions setGlossary(String glossaryId) {
6364
return this;
6465
}
6566

67+
/**
68+
* Specifies additional context to influence translations, that is not translated itself. Note:
69+
* this is an alpha feature: it may be deprecated at any time, or incur charges if it becomes
70+
* generally available. See the API documentation for more information and example usage.
71+
*/
72+
public TextTranslationOptions setContext(String context) {
73+
this.context = context;
74+
return this;
75+
}
76+
6677
/**
6778
* Specifies how input translation text should be split into sentences. By default, this value is
6879
* <code>null</code> and the default sentence splitting mode is used.
@@ -151,6 +162,11 @@ public boolean isPreserveFormatting() {
151162
return preserveFormatting;
152163
}
153164

165+
/** Gets the current context. */
166+
public String getContext() {
167+
return context;
168+
}
169+
154170
/** Gets the current tag handling setting. */
155171
public String getTagHandling() {
156172
return tagHandling;

deepl-java/src/main/java/com/deepl/api/Translator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,9 @@ private static ArrayList<KeyValuePair<String, String>> createHttpParams(
803803
if (options.isPreserveFormatting()) {
804804
params.add(new KeyValuePair<>("preserve_formatting", "1"));
805805
}
806+
if (options.getContext() != null) {
807+
params.add(new KeyValuePair<>("context", options.getContext()));
808+
}
806809
if (options.getTagHandling() != null) {
807810
params.add(new KeyValuePair<>("tag_handling", options.getTagHandling()));
808811
}

deepl-java/src/test/java/com/deepl/api/TranslateTextTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,22 @@ void testFormality() throws DeepLException, InterruptedException {
186186
}
187187
}
188188

189+
@Test
190+
void testContext() throws DeepLException, InterruptedException {
191+
// In German, "scharf" can mean:
192+
// - spicy/hot when referring to food, or
193+
// - sharp when referring to other objects such as a knife (Messer).
194+
Translator translator = createTranslator();
195+
String text = "Das ist scharf!";
196+
197+
translator.translateText(text, null, "de");
198+
// Result: "That is hot!"
199+
200+
translator.translateText(
201+
text, null, "de", new TextTranslationOptions().setContext("Das ist ein Messer."));
202+
// Result: "That is sharp!"
203+
}
204+
189205
@Test
190206
void testSplitSentences() throws DeepLException, InterruptedException {
191207
Assumptions.assumeTrue(isMockServer);

0 commit comments

Comments
 (0)