Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Translate API no longer requires an API key. [(#659)](GoogleCloudPlat…
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry authored and danoscarmike committed Jul 31, 2020
1 parent 6e8f7d4 commit 76c9a72
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 41 deletions.
5 changes: 1 addition & 4 deletions samples/snippets/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ def run_quickstart():
# Imports the Google Cloud client library
from google.cloud import translate

# Your Translate API key
api_key = 'YOUR_API_KEY'

# Instantiates a client
translate_client = translate.Client(api_key=api_key)
translate_client = translate.Client()

# The text to translate
text = u'Hello, world!'
Expand Down
19 changes: 1 addition & 18 deletions samples/snippets/quickstart_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from google.cloud import translate
import mock
import pytest

import quickstart


@pytest.fixture
def mock_client(cloud_config):
original_client_ctor = translate.Client

def new_client_ctor(api_key):
# Strip api_key argument and replace with our api key.
return original_client_ctor(cloud_config.api_key)

with mock.patch(
'google.cloud.translate.Client',
side_effect=new_client_ctor):
yield


def test_quickstart(mock_client, capsys):
def test_quickstart(capsys):
quickstart.run_quickstart()
out, _ = capsys.readouterr()
assert u'Привет мир!' in out
29 changes: 14 additions & 15 deletions samples/snippets/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
from google.cloud import translate


def detect_language(api_key, text):
def detect_language(text):
"""Detects the text's language."""
translate_client = translate.Client(api_key=api_key)
translate_client = translate.Client()

# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
Expand All @@ -39,39 +39,39 @@ def detect_language(api_key, text):
print('Language: {}'.format(result['language']))


def list_languages(api_key):
def list_languages():
"""Lists all available languages."""
translate_client = translate.Client(api_key=api_key)
translate_client = translate.Client()

results = translate_client.get_languages()

for language in results:
print(u'{name} ({language})'.format(**language))


def list_languages_with_target(api_key, target):
def list_languages_with_target(target):
"""Lists all available languages and localizes them to the target language.
Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
translate_client = translate.Client(api_key=api_key)
translate_client = translate.Client()

results = translate_client.get_languages(target_language=target)

for language in results:
print(u'{name} ({language})'.format(**language))


def translate_text_with_model(api_key, target, text, model=translate.BASE):
def translate_text_with_model(target, text, model=translate.BASE):
"""Translates text into the target language.
Make sure your project is whitelisted.
Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
translate_client = translate.Client(api_key=api_key)
translate_client = translate.Client()

# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
Expand All @@ -86,13 +86,13 @@ def translate_text_with_model(api_key, target, text, model=translate.BASE):
result['detectedSourceLanguage']))


def translate_text(api_key, target, text):
def translate_text(target, text):
"""Translates text into the target language.
Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
translate_client = translate.Client(api_key=api_key)
translate_client = translate.Client()

# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
Expand All @@ -110,7 +110,6 @@ def translate_text(api_key, target, text):
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('api_key', help='Your API key.')
subparsers = parser.add_subparsers(dest='command')

detect_langage_parser = subparsers.add_parser(
Expand All @@ -132,10 +131,10 @@ def translate_text(api_key, target, text):
args = parser.parse_args()

if args.command == 'detect-language':
detect_language(args.api_key, args.text)
detect_language(args.text)
elif args.command == 'list-languages':
list_languages(args.api_key)
list_languages()
elif args.command == 'list-languages-with-target':
list_languages_with_target(args.api_key, args.target)
list_languages_with_target(args.target)
elif args.command == 'translate-text':
translate_text(args.api_key, args.target, args.text)
translate_text(args.target, args.text)
8 changes: 4 additions & 4 deletions samples/snippets/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@


def test_detect_language(cloud_config, capsys):
snippets.detect_language(cloud_config.api_key, 'Hæ sæta')
snippets.detect_language('Hæ sæta')
out, _ = capsys.readouterr()
assert 'is' in out


def test_list_languages(cloud_config, capsys):
snippets.list_languages(cloud_config.api_key)
snippets.list_languages()
out, _ = capsys.readouterr()
assert 'Icelandic (is)' in out


def test_list_languages_with_target(cloud_config, capsys):
snippets.list_languages_with_target(cloud_config.api_key, 'is')
snippets.list_languages_with_target('is')
out, _ = capsys.readouterr()
assert u'íslenska (is)' in out


def test_translate_text(cloud_config, capsys):
snippets.translate_text(cloud_config.api_key, 'is', 'Hello world')
snippets.translate_text('is', 'Hello world')
out, _ = capsys.readouterr()
assert u'Halló heimur' in out

0 comments on commit 76c9a72

Please sign in to comment.