Skip to content

Commit

Permalink
Translate: migrate published glossaries samples [(#2769)](#2769)
Browse files Browse the repository at this point in the history
Migrate from tmp-generated-samples branch fef998b
Remove boilerplate
Update copyright date
Blacken
Remove unused imports
Shorten docstrings
Remove CLI
  • Loading branch information
texasmichelle authored and dandhlee committed Nov 18, 2022
1 parent 65a5ca4 commit 8a24a79
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
40 changes: 40 additions & 0 deletions translation/samples/snippets/translate_v3_list_glossary.py
Original file line number Diff line number Diff line change
@@ -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]
46 changes: 46 additions & 0 deletions translation/samples/snippets/translate_v3_list_glossary_test.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8a24a79

Please sign in to comment.