diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_list_glossary.py b/packages/google-cloud-translate/samples/snippets/translate_v3_list_glossary.py new file mode 100644 index 000000000000..e52d6b260ed9 --- /dev/null +++ b/packages/google-cloud-translate/samples/snippets/translate_v3_list_glossary.py @@ -0,0 +1,40 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3_list_glossary] +from google.cloud import translate + + +def sample_list_glossaries(project_id="YOUR_PROJECT_ID"): + """List Glossaries.""" + + client = translate.TranslationServiceClient() + + parent = client.location_path(project_id, "us-central1") + + # Iterate over all results + for glossary in client.list_glossaries(parent): + print("Name: {}".format(glossary.name)) + print("Entry count: {}".format(glossary.entry_count)) + print("Input uri: {}".format(glossary.input_config.gcs_source.input_uri)) + + # Note: You can create a glossary using one of two modes: + # language_code_set or language_pair. When listing the information for + # a glossary, you can only get information for the mode you used + # when creating the glossary. + for language_code in glossary.language_codes_set.language_codes: + print("Language code: {}".format(language_code)) + + +# [END translate_v3_list_glossary] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_list_glossary_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_list_glossary_test.py new file mode 100644 index 000000000000..de77daba8028 --- /dev/null +++ b/packages/google-cloud-translate/samples/snippets/translate_v3_list_glossary_test.py @@ -0,0 +1,46 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import pytest +import translate_v3_create_glossary +import translate_v3_delete_glossary +import translate_v3_list_glossary +import uuid + +PROJECT_ID = os.environ["GCLOUD_PROJECT"] +GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" + + +@pytest.fixture(scope="session") +def glossary(): + """Get the ID of a glossary available to session (do not mutate/delete).""" + glossary_id = "must-start-with-letters-" + str(uuid.uuid1()) + translate_v3_create_glossary.create_glossary( + PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id + ) + + yield glossary_id + + try: + translate_v3_delete_glossary.sample_delete_glossary(PROJECT_ID, glossary_id) + except Exception: + pass + + +def test_list_glossary(capsys, glossary): + translate_v3_list_glossary.sample_list_glossaries(PROJECT_ID) + out, _ = capsys.readouterr() + assert glossary in out + assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out