Skip to content

Commit 0ad8fcd

Browse files
Translate: migrate published glossaries samples (#2769)
Migrate from tmp-generated-samples branch fef998b Remove boilerplate Update copyright date Blacken Remove unused imports Shorten docstrings Remove CLI
1 parent d3cfead commit 0ad8fcd

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines 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_list_glossary]
16+
from google.cloud import translate
17+
18+
19+
def sample_list_glossaries(project_id="YOUR_PROJECT_ID"):
20+
"""List Glossaries."""
21+
22+
client = translate.TranslationServiceClient()
23+
24+
parent = client.location_path(project_id, "us-central1")
25+
26+
# Iterate over all results
27+
for glossary in client.list_glossaries(parent):
28+
print("Name: {}".format(glossary.name))
29+
print("Entry count: {}".format(glossary.entry_count))
30+
print("Input uri: {}".format(glossary.input_config.gcs_source.input_uri))
31+
32+
# Note: You can create a glossary using one of two modes:
33+
# language_code_set or language_pair. When listing the information for
34+
# a glossary, you can only get information for the mode you used
35+
# when creating the glossary.
36+
for language_code in glossary.language_codes_set.language_codes:
37+
print("Language code: {}".format(language_code))
38+
39+
40+
# [END translate_v3_list_glossary]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 pytest
17+
import translate_v3_create_glossary
18+
import translate_v3_delete_glossary
19+
import translate_v3_list_glossary
20+
import uuid
21+
22+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
23+
GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv"
24+
25+
26+
@pytest.fixture(scope="session")
27+
def glossary():
28+
"""Get the ID of a glossary available to session (do not mutate/delete)."""
29+
glossary_id = "must-start-with-letters-" + str(uuid.uuid1())
30+
translate_v3_create_glossary.create_glossary(
31+
PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
32+
)
33+
34+
yield glossary_id
35+
36+
try:
37+
translate_v3_delete_glossary.sample_delete_glossary(PROJECT_ID, glossary_id)
38+
except Exception:
39+
pass
40+
41+
42+
def test_list_glossary(capsys, glossary):
43+
translate_v3_list_glossary.sample_list_glossaries(PROJECT_ID)
44+
out, _ = capsys.readouterr()
45+
assert glossary in out
46+
assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out

0 commit comments

Comments
 (0)