Skip to content

Commit 9bd5c6a

Browse files
author
Jon Wayne Parrott
committed
Add translate samples
Change-Id: I73675bc01e019f9f377d42fbeac602946a09c169
1 parent 4c639cc commit 9bd5c6a

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

translate/cloud-client/snippets.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This application demonstrates how to perform basic operations with the
18+
Google Cloud Translate API
19+
20+
For more information, the documentation at
21+
https://cloud.google.com/translate/docs.
22+
"""
23+
24+
import argparse
25+
26+
from google.cloud import translate
27+
28+
29+
def detect_language(api_key, text):
30+
"""Detects the text's language."""
31+
translate_client = translate.Client(api_key)
32+
33+
# Text can also be a sequence of strings, in which case this method
34+
# will return a sequence of results for each text.
35+
result = translate_client.detect_language(text)
36+
37+
print('Text: {}'.format(text))
38+
print('Confidence: {}'.format(result['confidence']))
39+
print('Language: {}'.format(result['language']))
40+
41+
42+
def list_languages(api_key):
43+
"""Lists all available langauges."""
44+
translate_client = translate.Client(api_key)
45+
46+
results = translate_client.get_languages()
47+
48+
for language in results:
49+
print(u'{name} ({language})'.format(**language))
50+
51+
52+
def list_languages_with_target(api_key, target):
53+
"""Lists all available langauges and localizes them to the target language.
54+
55+
Target must be an ISO 639-1 language code.
56+
"""
57+
translate_client = translate.Client(api_key)
58+
59+
results = translate_client.get_languages(target_language=target)
60+
61+
for language in results:
62+
print(u'{name} ({language})'.format(**language))
63+
64+
65+
def translate_text(api_key, target, text):
66+
"""Translates text into the target language.
67+
68+
Target must be an ISO 639-1 language code.
69+
"""
70+
translate_client = translate.Client(api_key)
71+
72+
# Text can also be a sequence of strings, in which case this method
73+
# will return a sequence of results for each text.
74+
result = translate_client.translate(text, target_language=target)
75+
76+
print(u'Text: {}'.format(result['input']))
77+
print(u'Translation: {}'.format(result['translatedText']))
78+
print(u'Detected source language: {}'.format(
79+
result['detectedSourceLanguage']))
80+
81+
82+
if __name__ == '__main__':
83+
parser = argparse.ArgumentParser(
84+
description=__doc__,
85+
formatter_class=argparse.RawDescriptionHelpFormatter)
86+
parser.add_argument('api_key', help='Your API key.')
87+
subparsers = parser.add_subparsers(dest='command')
88+
89+
detect_langage_parser = subparsers.add_parser(
90+
'detect-language', help=detect_language.__doc__)
91+
detect_langage_parser.add_argument('text')
92+
93+
list_languages_parser = subparsers.add_parser(
94+
'list-languages', help=list_languages.__doc__)
95+
96+
list_languages_with_target_parser = subparsers.add_parser(
97+
'list-languages-with-target', help=list_languages_with_target.__doc__)
98+
list_languages_with_target_parser.add_argument('target')
99+
100+
translate_text_parser = subparsers.add_parser(
101+
'translate-text', help=translate_text.__doc__)
102+
translate_text_parser.add_argument('target')
103+
translate_text_parser.add_argument('text')
104+
105+
args = parser.parse_args()
106+
107+
if args.command == 'detect-language':
108+
detect_language(args.api_key, args.text)
109+
elif args.command == 'list-languages':
110+
list_languages(args.api_key)
111+
elif args.command == 'list-languages-with-target':
112+
list_languages_with_target(args.api_key, args.target)
113+
elif args.command == 'translate-text':
114+
translate_text(args.api_key, args.target, args.text)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright 2016 Google, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
import snippets
19+
20+
21+
def test_detect_language(cloud_config, capsys):
22+
snippets.detect_language(cloud_config.api_key, 'Hæ sæta')
23+
out, _ = capsys.readouterr()
24+
assert 'is' in out
25+
26+
27+
def test_list_languages(cloud_config, capsys):
28+
snippets.list_languages(cloud_config.api_key)
29+
out, _ = capsys.readouterr()
30+
assert 'Icelandic (is)' in out
31+
32+
33+
def test_list_languages_with_target(cloud_config, capsys):
34+
snippets.list_languages_with_target(cloud_config.api_key, 'is')
35+
out, _ = capsys.readouterr()
36+
assert u'íslenska (is)' in out
37+
38+
39+
def test_translate_text(cloud_config, capsys):
40+
snippets.translate_text(cloud_config.api_key, 'is', 'Hello world')
41+
out, _ = capsys.readouterr()
42+
assert u'Halló heimur' in out

0 commit comments

Comments
 (0)