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 7f0be75

Browse filesBrowse files
Migrate published samples (GoogleCloudPlatform#2759)
Migrate from tmp-generated-samples branch 615c08e Remove boilerplate Update copyright date Blacken Remove unused imports Shorten docstrings Remove CLI Set defaults in function definition Add link to supported types guide Inline function arguments Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>
1 parent 027f0e5 commit 7f0be75
Copy full SHA for 7f0be75

File tree

Expand file treeCollapse file tree

2 files changed

+64
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+64
-0
lines changed
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
# https://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]
16+
from google.cloud import translate
17+
18+
19+
def sample_translate_text(project_id="[Google Cloud Project ID]"):
20+
"""Translating Text."""
21+
22+
client = translate.TranslationServiceClient()
23+
24+
parent = client.location_path(project_id, "global")
25+
26+
# Detail on supported types can be found here:
27+
# https://cloud.google.com/translate/docs/supported-formats
28+
response = client.translate_text(
29+
parent=parent,
30+
contents=["Hello, world!"],
31+
mime_type="text/plain", # mime types: text/plain, text/html
32+
source_language_code="en-US",
33+
target_language_code="fr",
34+
)
35+
# Display the translation for each input text provided
36+
for translation in response.translations:
37+
print(u"Translated text: {}".format(translation.translated_text))
38+
39+
40+
# [END translate_v3_translate_text]
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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_translate_text
17+
18+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
19+
20+
21+
def test_translate_text(capsys):
22+
translate_v3_translate_text.sample_translate_text(PROJECT_ID)
23+
out, _ = capsys.readouterr()
24+
assert "Bonjour le monde" in out

0 commit comments

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