Skip to content

Commit

Permalink
feat: initial support for xml handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ontl committed May 30, 2021
1 parent 1f527d0 commit 8a68165
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Please refer to the linked documentation for instructions on how to get started
## Not Implemented

- Support for the [(beta) document translation endpoint](https://www.deepl.com/docs-api/translating-documents/).
- Support for the [XML handling flags](https://www.deepl.com/docs-api/translating-text/) in the translation endpoint.
- Support for advanced parameters of [Handling XML](https://www.deepl.com/docs-api/handling-xml/) in the translation endpoint (outline_detection, splitting_tags, non_splitting_tags, ignore_tags).

## See Also

Expand Down
4 changes: 4 additions & 0 deletions deepl_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def translate(
split_sentences: SplitSentences = None,
preserve_formatting: bool = None,
formality: Formality = None,
handle_xml: bool = None,
texts: list,
) -> list:
"""
Expand Down Expand Up @@ -208,6 +209,9 @@ def translate(

if formality != None:
payload["formality"] = formality.value

if handle_xml:
payload["tag_handling"] = "xml"

data = self._api_call("/translate", payload)

Expand Down
3 changes: 3 additions & 0 deletions deepl_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def languages():
@click.option("-p", "--preserve-formatting", default=None, is_flag=True)
@click.option("-m", "--formality-more", default=None, is_flag=True)
@click.option("-l", "--formality-less", default=None, is_flag=True)
@click.option("-x", "--handle-xml", default=None, is_flag=True)
def translate(
source_language,
target_language,
Expand All @@ -74,6 +75,7 @@ def translate(
preserve_formatting,
formality_more,
formality_less,
handle_xml
):
try:
deepl = _get_instance()
Expand All @@ -94,6 +96,7 @@ def translate(
target_language=target_language,
preserve_formatting=preserve_formatting,
formality=formality,
handle_xml=handle_xml,
texts=[text],
)

Expand Down
27 changes: 27 additions & 0 deletions tests/test_deepl_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,33 @@ def test_translate():
},
],
},
{
"args": {
"source_language": "EN",
"target_language": "FR",
"handle_xml": True,
"texts": ["A delicious <i>apple</i>."],
},
"result": [
{
"detected_source_language": "EN",
"text": "Une <i>pomme</i> délicieuse.",
},
],
},
{
"args": {
"source_language": "EN",
"target_language": "FR",
"texts": ["A delicious <i>apple</i>."],
},
"result": [
{
"detected_source_language": "EN",
"text": "Une délicieuse <i>pomme</i>.",
},
],
},
]

for test in tests:
Expand Down

0 comments on commit 8a68165

Please sign in to comment.