8000 feat(translate): add region tag 'import_client_library' and refactor … · yan283/python-docs-samples@5ef6469 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ef6469

Browse files
feat(translate): add region tag 'import_client_library' and refactor sample (GoogleCloudPlatform#13255)
* chore(translate): delete remaining markdown files from repo migration * feat(translate): add region tag 'import_client_library' and style fixing * fix(translate): refactor sample 'translate_v3_translate_text' and its test * fix(translate): apply feedback from 'glasnt' - Comment: GoogleCloudPlatform#13255 (review) * fix(translate): restore comment related to supported MIME types GoogleCloudPlatform#13255 (comment) * fix(translate): restore import to 'translate_v3' after testing compatibility with '_v3beta1' * fix(translate): update to latest dependencies, and allow testing on Python 3.9+ * fix(translate): revert updating to latest dependencies, as it's breaking compatibility with vertexai * fix(translate): remove comment about library not compatible with Python 3.12 even when it was tested locally
1 parent 5756bee commit 5ef6469

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
lines changed

translate/samples/AUTHORING_GUIDE.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

translate/samples/CONTRIBUTING.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

translate/samples/snippets/noxfile_config.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
# Google-cloud-translate doesn't work with Python 3.12 for now.
26-
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.12", "3.13"],
25+
"ignored_versions": ["2.7", "3.7", "3.8"],
2726
# 10000 Old samples are opted out of enforcing Python type hints
2827
# All new samples should feature them
2928
"enforce_type_hints": True,

translate/samples/snippets/translate_v3_translate_text.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,62 @@
1313
# limitations under the License.
1414

1515
# [START translate_v3_translate_text]
16-
# Imports the Google Cloud Translation library
1716
import os
1817

18+
# Import the Google Cloud Translation library.
19+
# [START translate_v3_import_client_library]
1920
from google.cloud import translate_v3
21+
# [END translate_v3_import_client_library]
2022

2123
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
2224

2325

24-
# Initialize Translation client
2526
def translate_text(
2627
text: str = "YOUR_TEXT_TO_TRANSLATE",
27-
language_code: str = "fr",
28+
source_language_code: str = "en-US",
29+
target_language_code: str = "fr",
2830
) -> translate_v3.TranslationServiceClient:
29-
"""Translating Text from English.
31+
"""Translate Text from a Source language to a Target language.
3032
Args:
3133
text: The content to translate.
32-
language_code: The language code for the translation.
33-
E.g. "fr" for French, "es" for Spanish, etc.
34-
Available languages: https://cloud.google.com/translate/docs/languages#neural_machine_translation_model
34+
source_language_code: The code of the source language.
35+
target_language_code: The code of the target language.
36+
For example: "fr" for French, "es" for Spanish, etc.
37+
Find available languages and codes here:
38+
https://cloud.google.com/translate/docs/languages#neural_machine_translation_model
3539
"""
3640

41+
# Initialize Translation client.
3742
client = translate_v3.TranslationServiceClient()
3843
parent = f"projects/{PROJECT_ID}/locations/global"
39-
# Translate text from English to chosen language
40-
# Supported mime types: # https://cloud.google.com/translate/docs/supported-formats
44+
45+
# MIME type of the content to translate.
46+
# Supported MIME types:
47+
# https://cloud.google.com/translate/docs/supported-formats
48+
mime_type = "text/plain"
49+
50+
# Translate text from the source to the target language.
4151
response = client.translate_text(
4252
contents=[text],
43-
target_language_code=language_code,
4453
parent=parent,
45-
mime_type="text/plain",
46-
source_language_code="en-US",
54+
mime_type=mime_type,
55+
source_language_code=source_language_code,
56+
target_language_code=target_language_code,
4757
)
4858

49-
# Display the translation for each input text provided
59+
# Display the translation for the text.
60+
# For example, for "Hello! How are you doing today?":
61+
# Translated text: Bonjour comment vas-tu aujourd'hui?
5062
for translation in response.translations:
5163
print(f"Translated text: {translation.translated_text}")
52-
# Example response:
53-
# Translated text: Bonjour comment vas-tu aujourd'hui?
5464

5565
return response
56-
57-
5866
# [END translate_v3_translate_text]
5967

68+
6069
if __name__ == "__main__":
61-
translate_text(text="Hello! How are you doing today?", language_code="fr")
70+
translate_text(
71+
text="Hello! How are you doing today?",
72+
source_language_code="en-US",
73+
target_language_code="fr"
74+
)

translate/samples/snippets/translate_v3_translate_text_test.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import pytest
16-
1715
import translate_v3_translate_text
1816

1917

20-
def test_translate_text(capsys: pytest.LogCaptureFixture) -> None:
21-
response = translate_v3_translate_text.translate_text("Hello World!", "fr")
22-
out, _ = capsys.readouterr()
18+
def test_translate_text() -> None:
19+
response = translate_v3_translate_text.translate_text("Hello World!", "en-US", "fr")
2320
assert "Bonjour le monde" in response.translations[0].translated_text

0 commit comments

Comments
 (0)
0