Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b00f00d

Browse filesBrowse files
translate-v3: samples (GoogleCloudPlatform#3034)
* translate with custom model, get supported langs * inlined small nit * added encoding to model test * added missing region tags and link to supported langs * inlined text-to-translate * directly inlined contents * revert text-translate vars * reversed inlined text params * small nit Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>
1 parent e8266a9 commit b00f00d
Copy full SHA for b00f00d
Expand file treeCollapse file tree

8 files changed

+147
-8
lines changed

‎translate/cloud-client/translate_v3_get_supported_languages.py

Copy file name to clipboardExpand all lines: translate/cloud-client/translate_v3_get_supported_languages.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def sample_get_supported_languages(project_id="YOUR_PROJECT_ID"):
2323

2424
parent = client.location_path(project_id, "global")
2525

26+
# Supported language codes: https://cloud.google.com/translate/docs/languages
2627
response = client.get_supported_languages(parent=parent)
2728

2829
# List language codes of supported languages.
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START translate_v3_get_supported_languages_for_target]
16+
from google.cloud import translate
17+
18+
19+
def get_supported_languages_with_target(project_id="YOUR_PROJECT_ID"):
20+
"""Listing supported languages with target language name."""
21+
22+
client = translate.TranslationServiceClient()
23+
parent = client.location_path(project_id, "global")
24+
25+
# Supported language codes: https://cloud.google.com/translate/docs/languages
26+
response = client.get_supported_languages(
27+
display_language_code="is", # target language code
28+
parent=parent
29+
)
30+
# List language codes of supported languages
31+
for language in response.languages:
32+
print(u"Language Code: {}".format(language.language_code))
33+
print(u"Display Name: {}".format(language.display_name))
34+
35+
# [END translate_v3_get_supported_languages_for_target]
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import translate_v3_get_supported_languages_with_target as get_supported_langs
17+
18+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
19+
20+
21+
def test_list_languages_with_target(capsys):
22+
get_supported_langs.get_supported_languages_with_target(
23+
PROJECT_ID
24+
)
25+
out, _ = capsys.readouterr()
26+
assert u"Language Code: sq" in out
27+
assert u"Display Name: albanska" in out

‎translate/cloud-client/translate_v3_translate_text.py

Copy file name to clipboardExpand all lines: translate/cloud-client/translate_v3_translate_text.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from google.cloud import translate
1717

1818

19-
def sample_translate_text(project_id="YOUR_PROJECT_ID"):
19+
def sample_translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"):
2020
"""Translating Text."""
2121

2222
client = translate.TranslationServiceClient()
@@ -27,7 +27,7 @@ def sample_translate_text(project_id="YOUR_PROJECT_ID"):
2727
# https://cloud.google.com/translate/docs/supported-formats
2828
response = client.translate_text(
2929
parent=parent,
30-
contents=["Hello, world!"],
30+
contents=[text],
3131
mime_type="text/plain", # mime types: text/plain, text/html
3232
source_language_code="en-US",
3333
target_language_code="fr",

‎translate/cloud-client/translate_v3_translate_text_test.py

Copy file name to clipboardExpand all lines: translate/cloud-client/translate_v3_translate_text_test.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020

2121
def test_translate_text(capsys):
22-
translate_v3_translate_text.sample_translate_text(PROJECT_ID)
22+
translate_v3_translate_text.sample_translate_text(
23+
"Hello World!", PROJECT_ID)
2324
out, _ = capsys.readouterr()
2425
assert "Bonjour le monde" in out

‎translate/cloud-client/translate_v3_translate_text_with_glossary.py

Copy file name to clipboardExpand all lines: translate/cloud-client/translate_v3_translate_text_with_glossary.py
+4-5Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,23 @@
2020
def translate_text_with_glossary(
2121
text="YOUR_TEXT_TO_TRANSLATE",
2222
project_id="YOUR_PROJECT_ID",
23-
glossary_id="YOUR_GLOSSARY_ID"
23+
glossary_id="YOUR_GLOSSARY_ID",
2424
):
2525
"""Translates a given text using a glossary."""
2626

2727
client = translate_v3.TranslationServiceClient()
28-
29-
contents = [text]
3028
parent = client.location_path(project_id, "us-central1")
3129

3230
glossary = client.glossary_path(
3331
project_id, "us-central1", glossary_id # The location of the glossary
3432
)
3533

36-
glossary_config = translate_v3.types.TranslateTextGlossaryConfig(glossary=glossary)
34+
glossary_config = translate_v3.types.TranslateTextGlossaryConfig(
35+
glossary=glossary)
3736

3837
# Supported language codes: https://cloud.google.com/translate/docs/languages
3938
response = client.translate_text(
40-
contents,
39+
contents=[text],
4140
target_language_code="ja",
4241
source_language_code="en",
4342
parent=parent,
+48Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START translate_v3_translate_text_with_model]
16+
17+
from google.cloud import translate
18+
19+
20+
def translate_text_with_model(
21+
text="YOUR_TEXT_TO_TRANSLATE",
22+
project_id="YOUR_PROJECT_ID",
23+
model_id="YOUR_MODEL_ID",
24+
):
25+
"""Translates a given text using Translation custom model."""
26+
27+
client = translate.TranslationServiceClient()
28+
29+
parent = client.location_path(project_id, "us-central1")
30+
model_path = "projects/{}/locations/{}/models/{}".format(
31+
project_id, "us-central1", model_id
32+
)
33+
34+
# Supported language codes: https://cloud.google.com/translate/docs/languages
35+
response = client.translate_text(
36+
contents=[text],
37+
target_language_code="ja",
38+
model=model_path,
39+
source_language_code="en",
40+
parent=parent,
41+
mime_type="text/plain", # mime types: text/plain, text/html
42+
)
43+
# Display the translation for each input text provided
44+
for translation in response.translations:
45+
print(u"Translated text: {}".format(translation.translated_text))
46+
47+
48+
# [END translate_v3_translate_text_with_model]
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- encoding: utf-8 -*-
2+
# Copyright 2020 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import os
17+
import translate_v3_translate_text_with_model
18+
19+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
20+
MODEL_ID = "TRL3128559826197068699"
21+
22+
23+
def test_translate_text_with_model(capsys):
24+
translate_v3_translate_text_with_model.translate_text_with_model(
25+
"That' il do it.", PROJECT_ID, MODEL_ID
26+
)
27+
out, _ = capsys.readouterr()
28+
assert "それはそうだ" or "それじゃあ" in out

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.