Skip to content

Commit ccba286

Browse files
feat: add context parameter (alpha feature)
1 parent c7637e4 commit ccba286

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77

88
## [Unreleased]
9+
### Added
10+
* Add optional `context` parameter for text translation, that specifies
11+
additional context to influence translations, that is not translated itself.
912
### Changed
1013
* Added notice in Readme that starting in 2024 the library will drop support for
1114
Python versions that are officially end-of-life.

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ arguments are:
154154
- `glossary`: specifies a glossary to use with translation, either as a string
155155
containing the glossary ID, or a `GlossaryInfo` as returned by
156156
`get_glossary()`.
157+
- `context`: specifies additional context to influence translations, that is not
158+
translated itself. Note this is an **alpha feature**: it may be deprecated at
159+
any time, or incur charges if it becomes generally available.
160+
See the [API documentation][api-docs-context-param] for more information and
161+
example usage.
157162
- `tag_handling`: type of tags to parse before translation, options are `'html'`
158163
and `'xml'`.
159164

@@ -600,6 +605,8 @@ environment variables defined referring to the mock-server.
600605

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

608+
[api-docs-context-param]: https://www.deepl.com/docs-api/translating-text/?utm_source=github&utm_medium=github-python-readme
609+
603610
[api-docs-lang-list]: https://www.deepl.com/docs-api/translating-text/?utm_source=github&utm_medium=github-python-readme
604611

605612
[api-docs-glossary-lang-list]: https://www.deepl.com/docs-api/managing-glossaries/?utm_source=github&utm_medium=github-python-readme

deepl/__main__.py

+6
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ def add_common_arguments(subparser: argparse.ArgumentParser):
283283
"text", help="translate text(s)", description="translate text(s)"
284284
)
285285
add_common_arguments(parser_text)
286+
parser_text.add_argument(
287+
"--context",
288+
type=str,
289+
help="additional contextual text to improve translations, see API docs"
290+
" for information",
291+
)
286292
parser_text.add_argument(
287293
"--split-sentences",
288294
type=str,

deepl/translator.py

+8
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ def translate_text(
337337
*,
338338
source_lang: Union[str, Language, None] = None,
339339
target_lang: Union[str, Language],
340+
context: Optional[str] = None,
340341
split_sentences: Union[str, SplitSentences, None] = None,
341342
preserve_formatting: Optional[bool] = None,
342343
formality: Union[str, Formality, None] = None,
@@ -357,6 +358,11 @@ def translate_text(
357358
language. If a glossary is used, source_lang must be specified.
358359
:param target_lang: language code to translate text into, for example
359360
"DE", "EN-US", "FR".
361+
:param context: (Optional) Additional contextual text to influence
362+
translations, that is not translated itself. Note: this is an alpha
363+
feature: it may be deprecated at any time, or incur charges if it
364+
becomes generally available. See the API documentation for more
365+
information and example usage.
360366
:param split_sentences: (Optional) Controls how the translation engine
361367
should split input into sentences before translation, see
362368
:class:`SplitSentences`.
@@ -406,6 +412,8 @@ def translate_text(
406412
)
407413
request_data["text"] = text
408414

415+
if context is not None:
416+
request_data["context"] = context
409417
if split_sentences is not None:
410418
request_data["split_sentences"] = str(split_sentences)
411419
if preserve_formatting is not None:

tests/test_translate_text.py

+14
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,20 @@ def test_preserve_formatting(translator):
236236
)
237237

238238

239+
def test_context(translator):
240+
# In German, "scharf" can mean:
241+
# - spicy/hot when referring to food, or
242+
# - sharp when referring to other objects such as a knife (Messer).
243+
text = "Das ist scharf!"
244+
_ = translator.translate_text(text, target_lang="en-US")
245+
# Result: "That is hot!"
246+
247+
_ = translator.translate_text(
248+
text, target_lang="en-US", context="Das ist ein Messer"
249+
)
250+
# Result: "That is sharp!"
251+
252+
239253
def test_split_sentences_basic(translator):
240254
text = """If the implementation is hard to explain, it's a bad idea.
241255
If the implementation is easy to explain, it may be a good idea."""

0 commit comments

Comments
 (0)