Skip to content

Commit e41751e

Browse files
[translation] support translation kwargs for single translation overload (Azure#19851)
* support kwargs in the single overload * add docstrings for kwargs * pylint and changelog * tests and fixes * don't expose storage source kwargs for now
1 parent 8d60603 commit e41751e

10 files changed

+1820
-8
lines changed

sdk/translation/azure-ai-translation-document/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
### Features Added
66

7+
- The single translation input version of `begin_translation(source, target, target_language_code)` now accepts keyword arguments
8+
`storage_type`, `glossaries`, `category_id`, `prefix`, `suffix`, and `source_language_code`.
9+
710
### Breaking Changes
811

912
- Changed: renamed kwargs `translated_before` and `translated_after` to `created_before` and `created_after`, respectively,

sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_client.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ def begin_translation(
114114
): # pylint: disable=client-method-missing-type-annotations
115115
"""Begin translating the document(s) in your source container to your target container
116116
in the given language. To perform a single translation from source to target, pass the `source_url`,
117-
`target_url`, and `target_language_code` parameters. To pass multiple inputs for translation, including
118-
other translation options, pass the `inputs` parameter as a list of
117+
`target_url`, and `target_language_code` parameters including any optional keyword arguments.
118+
To pass multiple inputs for translation, pass the `inputs` parameter as a list of
119119
:class:`~azure.ai.translation.document.DocumentTranslationInput`.
120120
121121
For supported languages and document formats, see the service documentation:
@@ -132,6 +132,19 @@ def begin_translation(
132132
source URL to documents and can contain multiple TranslationTargets (one for each language)
133133
for the destination to write translated documents.
134134
:type inputs: List[~azure.ai.translation.document.DocumentTranslationInput]
135+
:keyword str source_language_code: Language code for the source documents.
136+
If none is specified, the source language will be auto-detected for each document.
137+
:keyword str prefix: A case-sensitive prefix string to filter documents in the source path for
138+
translation. For example, when using a Azure storage blob Uri, use the prefix to restrict
139+
sub folders for translation.
140+
:keyword str suffix: A case-sensitive suffix string to filter documents in the source path for
141+
translation. This is most often use for file extensions.
142+
:keyword storage_type: Storage type of the input documents source string. Possible values
143+
include: "Folder", "File".
144+
:paramtype storage_type: str or ~azure.ai.translation.document.StorageInputType
145+
:keyword str category_id: Category / custom model ID for using custom translation.
146+
:keyword glossaries: Glossaries to apply to translation.
147+
:paramtype glossaries: list[~azure.ai.translation.document.TranslationGlossary]
135148
:return: An instance of a DocumentTranslationLROPoller. Call `result()` on the poller
136149
object to return a pageable of DocumentStatus. A DocumentStatus will be
137150
returned for each translation on a document.

sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_helpers.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
BatchRequest as _BatchRequest,
1515
SourceInput as _SourceInput,
1616
TargetInput as _TargetInput,
17+
DocumentFilter as _DocumentFilter,
1718
)
1819
from ._models import DocumentTranslationInput
1920

@@ -44,14 +45,35 @@ def get_translation_input(args, kwargs, continuation_token):
4445
target_language_code = kwargs.pop("target_language_code", None)
4546
if not target_language_code:
4647
target_language_code = args[2]
48+
49+
# Additional kwargs
50+
source_language_code = kwargs.pop("source_language_code", None)
51+
prefix = kwargs.pop("prefix", None)
52+
suffix = kwargs.pop("suffix", None)
53+
storage_type = kwargs.pop("storage_type", None)
54+
category_id = kwargs.pop("category_id", None)
55+
glossaries = kwargs.pop("glossaries", None)
56+
4757
request = [
4858
_BatchRequest(
49-
source=_SourceInput(source_url=source_url),
59+
source=_SourceInput(
60+
source_url=source_url,
61+
filter=_DocumentFilter(
62+
prefix=prefix,
63+
suffix=suffix
64+
),
65+
language=source_language_code,
66+
),
5067
targets=[
5168
_TargetInput(
52-
target_url=target_url, language=target_language_code
69+
target_url=target_url,
70+
language=target_language_code,
71+
glossaries=[g._to_generated() for g in glossaries] # pylint: disable=protected-access
72+
if glossaries else None,
73+
category=category_id,
5374
)
5475
],
76+
storage_type=storage_type
5577
)
5678
]
5779
except (AttributeError, TypeError, IndexError):

sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_client_async.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ async def begin_translation(
123123
): # pylint: disable=client-method-missing-type-annotations
124124
"""Begin translating the document(s) in your source container to your target container
125125
in the given language. To perform a single translation from source to target, pass the `source_url`,
126-
`target_url`, and `target_language_code` parameters. To pass multiple inputs for translation, including
127-
other translation options, pass the `inputs` parameter as a list of
126+
`target_url`, and `target_language_code` parameters including any optional keyword arguments.
127+
To pass multiple inputs for translation, pass the `inputs` parameter as a list of
128128
:class:`~azure.ai.translation.document.DocumentTranslationInput`.
129129
130130
For supported languages and document formats, see the service documentation:
@@ -141,6 +141,19 @@ async def begin_translation(
141141
source URL to documents and can contain multiple TranslationTargets (one for each language)
142142
for the destination to write translated documents.
143143
:type inputs: List[~azure.ai.translation.document.DocumentTranslationInput]
144+
:keyword str source_language_code: Language code for the source documents.
145+
If none is specified, the source language will be auto-detected for each document.
146+
:keyword str prefix: A case-sensitive prefix string to filter documents in the source path for
147+
translation. For example, when using a Azure storage blob Uri, use the prefix to restrict
148+
sub folders for translation.
149+
:keyword str suffix: A case-sensitive suffix string to filter documents in the source path for
150+
translation. This is most often use for file extensions.
151+
:keyword storage_type: Storage type of the input documents source string. Possible values
152+
include: "Folder", "File".
153+
:paramtype storage_type: str or ~azure.ai.translation.document.StorageInputType
154+
:keyword str category_id: Category / custom model ID for using custom translation.
155+
:keyword glossaries: Glossaries to apply to translation.
156+
:paramtype glossaries: list[~azure.ai.translation.document.TranslationGlossary]
144157
:return: An instance of an AsyncDocumentTranslationLROPoller. Call `result()` on the poller
145158
object to return a pageable of DocumentStatus. A DocumentStatus will be
146159
returned for each translation on a document.

0 commit comments

Comments
 (0)