-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Update Translate samples to establish canonical template #1734
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,35 +15,109 @@ | |
| # limitations under the License. | ||
|
|
||
|
|
||
| import snippets | ||
| def test_detect_language(capsys): | ||
| # [START translate_detect_language] | ||
| from google.cloud import translate | ||
|
|
||
| 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. | ||
| text = 'Hæ sæta' | ||
| result = translate_client.detect_language(text) | ||
|
|
||
| print('Text: {}'.format(text)) | ||
| print('Confidence: {}'.format(result['confidence'])) | ||
| print('Language: {}'.format(result['language'])) | ||
| # [END translate_detect_language] | ||
|
|
||
| def test_detect_language(capsys): | ||
| snippets.detect_language('Hæ sæta') | ||
| out, _ = capsys.readouterr() | ||
| assert 'is' in out | ||
|
|
||
|
|
||
| def test_list_languages(capsys): | ||
| snippets.list_languages() | ||
| # [START translate_list_codes] | ||
| from google.cloud import translate | ||
|
|
||
| translate_client = translate.Client() | ||
|
|
||
| results = translate_client.get_languages() | ||
|
|
||
| for language in results: | ||
| print(u'{name} ({language})'.format(**language)) | ||
| # [END translate_list_codes] | ||
|
|
||
| out, _ = capsys.readouterr() | ||
| assert 'Icelandic (is)' in out | ||
|
|
||
|
|
||
| def test_list_languages_with_target(capsys): | ||
| snippets.list_languages_with_target('is') | ||
| # [START translate_list_language_names] | ||
| from google.cloud import translate | ||
|
|
||
| translate_client = translate.Client() | ||
|
|
||
| target = 'is' # Target must be an ISO 639-1 language code. | ||
| results = translate_client.get_languages(target_language=target) | ||
|
|
||
| for language in results: | ||
| print(u'{name} ({language})'.format(**language)) | ||
| # [END translate_list_language_names] | ||
|
|
||
| out, _ = capsys.readouterr() | ||
| assert u'íslenska (is)' in out | ||
|
|
||
|
|
||
| def test_translate_text(capsys): | ||
| snippets.translate_text('is', 'Hello world') | ||
| # [START translate_text_with_model] | ||
| import six | ||
| from google.cloud import translate | ||
|
|
||
| translate_client = translate.Client() | ||
|
|
||
| text = 'Hello world' | ||
| target = 'is' # Target must be an ISO 639-1 language code. | ||
| model = translate.NMT | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is |
||
| if isinstance(text, six.binary_type): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a u prefix on text and it'll always be the Unicode type. |
||
| text = text.decode('utf-8') | ||
|
|
||
| # Text can also be a sequence of strings, in which case this method | ||
| # will return a sequence of results for each text. | ||
| result = translate_client.translate( | ||
| text, target_language=target, model=model) | ||
|
|
||
| print(u'Text: {}'.format(result['input'])) | ||
| print(u'Translation: {}'.format(result['translatedText'])) | ||
| print(u'Detected source language: {}'.format( | ||
| result['detectedSourceLanguage'])) | ||
| # [END translate_text_with_model] | ||
|
|
||
| out, _ = capsys.readouterr() | ||
| assert u'Halló heimur' in out | ||
|
|
||
|
|
||
| def test_translate_utf8(capsys): | ||
| # [START translate_translate_text] | ||
| import six | ||
| from google.cloud import translate | ||
|
|
||
| translate_client = translate.Client() | ||
|
|
||
| target = 'en' # Target must be an ISO 639-1 language code. | ||
| text = u'나는 파인애플을 좋아한다.' | ||
| snippets.translate_text('en', text) | ||
| if isinstance(text, six.binary_type): | ||
| text = text.decode('utf-8') | ||
|
|
||
| # Text can also be a sequence of strings, in which case this method | ||
| # will return a sequence of results for each text. | ||
| result = translate_client.translate( | ||
| text, target_language=target) | ||
|
|
||
| print(u'Text: {}'.format(result['input'])) | ||
| print(u'Translation: {}'.format(result['translatedText'])) | ||
| print(u'Detected source language: {}'.format( | ||
| result['detectedSourceLanguage'])) | ||
| # [END translate_translate_text] | ||
|
|
||
| out, _ = capsys.readouterr() | ||
| assert u'I like pineapples.' in out | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just reviewed another PR that does the same update to a new canonical template, but the placeholder variables looked different there. There was a
# TODO(developer)comment. Should that be added above these placeholder variables?Also, should this be: