Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate: migrate published glossaries samples #2769

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions translate/cloud-client/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 translate/cloud-client/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