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 d3cfead

Browse filesBrowse files
Translate: migrate published samples (GoogleCloudPlatform#2768)
Migrate from tmp-generated-samples branch 615c08e Remove boilerplate Update copyright date Blacken Remove unused imports
1 parent cffeff2 commit d3cfead
Copy full SHA for d3cfead

File tree

Expand file treeCollapse file tree

4 files changed

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

4 files changed

+125
-0
lines changed
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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_detect_language]
16+
from google.cloud import translate
17+
18+
19+
def sample_detect_language(project_id="YOUR_PROJECT_ID"):
20+
"""Detecting the language of a text string."""
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.detect_language(
29+
content="Hello, world!",
30+
parent=parent,
31+
mime_type="text/plain", # mime types: text/plain, text/html
32+
)
33+
34+
# Display list of detected languages sorted by detection confidence.
35+
# The most probable language is first.
36+
for language in response.languages:
37+
# The language detected
38+
print(u"Language code: {}".format(language.language_code))
39+
# Confidence of detection result for this language
40+
print(u"Confidence: {}".format(language.confidence))
41+
42+
43+
# [END translate_v3_detect_language]
+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_detect_language
17+
18+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
19+
20+
21+
def test_detect_language(capsys):
22+
translate_v3_detect_language.sample_detect_language(PROJECT_ID)
23+
out, _ = capsys.readouterr()
24+
assert "en" in out
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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_get_supported_languages]
16+
from google.cloud import translate
17+
18+
19+
def sample_get_supported_languages(project_id="YOUR_PROJECT_ID"):
20+
"""Getting a list of supported language codes."""
21+
22+
client = translate.TranslationServiceClient()
23+
24+
parent = client.location_path(project_id, "global")
25+
26+
response = client.get_supported_languages(parent=parent)
27+
28+
# List language codes of supported languages.
29+
print("Supported Languages:")
30+
for language in response.languages:
31+
print(u"Language Code: {}".format(language.language_code))
32+
33+
34+
# [END translate_v3_get_supported_languages]
+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_get_supported_languages
17+
18+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
19+
20+
21+
def test_list_languages(capsys):
22+
translate_v3_get_supported_languages.sample_get_supported_languages(PROJECT_ID)
23+
out, _ = capsys.readouterr()
24+
assert "zh-CN" in out

0 commit comments

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