Skip to content

Commit 4cb603f

Browse files
Move code into snippets.
1 parent 2f6a45d commit 4cb603f

File tree

8 files changed

+145
-97
lines changed

8 files changed

+145
-97
lines changed

translate/api/src/DetectLanguageCommand.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818

1919
namespace Google\Cloud\Samples\Translate;
2020

21-
// [START translate_detect_language]
22-
use Google\Cloud\Translate\TranslateClient;
23-
// [END translate_detect_language]
2421
use Symfony\Component\Console\Command\Command;
2522
use Symfony\Component\Console\Input\InputArgument;
2623
use Symfony\Component\Console\Input\InputOption;
@@ -61,25 +58,8 @@ protected function configure()
6158

6259
protected function execute(InputInterface $input, OutputInterface $output)
6360
{
64-
$this->detectLanguage(
65-
$input->getOption('api-key'),
66-
$input->getArgument('text')
67-
);
61+
$apiKey = $input->getOption('api-key');
62+
$text = $input->getArgument('text');
63+
require(__DIR__ . '/snippets/detect_language.php');
6864
}
69-
70-
// [START translate_detect_language]
71-
/***
72-
* @param $apiKey string Your API key.
73-
* @param $text string The text for which to detect the language.
74-
*/
75-
protected function detectLanguage($apiKey, $text)
76-
{
77-
$translate = new TranslateClient([
78-
'key' => $apiKey,
79-
]);
80-
$result = $translate->detectLanguage($text);
81-
print("Language code: $result[languageCode]\n");
82-
print("Confidence: $result[confidence]\n");
83-
}
84-
// [END translate_detect_language]
8565
}

translate/api/src/ListCodesCommand.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818

1919
namespace Google\Cloud\Samples\Translate;
2020

21-
// [START translate_list_codes]
22-
use Google\Cloud\Translate\TranslateClient;
23-
// [END translate_list_codes]
2421
use Symfony\Component\Console\Command\Command;
2522
use Symfony\Component\Console\Input\InputInterface;
2623
use Symfony\Component\Console\Input\InputOption;
@@ -55,21 +52,7 @@ protected function configure()
5552

5653
protected function execute(InputInterface $input, OutputInterface $output)
5754
{
58-
$this->listCodes($input->getOption('api-key'));
55+
$apiKey = $input->getOption('api-key');
56+
require(__DIR__ . '/snippets/list_codes.php');
5957
}
60-
61-
// [START translate_list_codes]
62-
/**
63-
* @param $apiKey string Your API key.
64-
*/
65-
protected function listCodes($apiKey)
66-
{
67-
$translate = new TranslateClient([
68-
'key' => $apiKey,
69-
]);
70-
foreach ($translate->languages() as $code) {
71-
print("$code\n");
72-
}
73-
}
74-
// [END translate_list_codes]
7558
}

translate/api/src/ListLanguagesCommand.php

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818

1919
namespace Google\Cloud\Samples\Translate;
2020

21-
// [START translate_list_language_names]
22-
use Google\Cloud\Translate\TranslateClient;
23-
// [END translate_list_language_names]
2421
use Symfony\Component\Console\Command\Command;
2522
use Symfony\Component\Console\Input\InputInterface;
2623
use Symfony\Component\Console\Input\InputOption;
@@ -61,29 +58,8 @@ protected function configure()
6158

6259
protected function execute(InputInterface $input, OutputInterface $output)
6360
{
64-
$this->listLanguage(
65-
$input->getOption('api-key'),
66-
$input->getOption('target-language')
67-
);
61+
$apiKey = $input->getOption('api-key');
62+
$targetLanguage = $input->getOption('target-language');
63+
require(__DIR__ . '/snippets/list_languages.php');
6864
}
69-
70-
// [START translate_list_language_names]
71-
/**
72-
* @param $apiKey string Your API key.
73-
* @param $targetLanguage string Language code: Print the names of the
74-
* language in which language?
75-
*/
76-
protected function listLanguage($apiKey, $targetLanguage)
77-
{
78-
$translate = new TranslateClient([
79-
'key' => $apiKey,
80-
]);
81-
$result = $translate->localizedLanguages([
82-
'target' => $targetLanguage,
83-
]);
84-
foreach ($result as $lang) {
85-
print("$lang[code]: $lang[name]\n");
86-
}
87-
}
88-
// [END translate_list_language_names]
8965
}

translate/api/src/TranslateCommand.php

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818

1919
namespace Google\Cloud\Samples\Translate;
2020

21-
// [START translate_translate_text]
22-
use Google\Cloud\Translate\TranslateClient;
23-
// [END translate_translate_text]
2421
use Symfony\Component\Console\Command\Command;
2522
use Symfony\Component\Console\Input\InputArgument;
2623
use Symfony\Component\Console\Input\InputInterface;
@@ -66,30 +63,9 @@ protected function configure()
6663

6764
protected function execute(InputInterface $input, OutputInterface $output)
6865
{
69-
$this->translate(
70-
$input->getOption('api-key'),
71-
$input->getArgument('text'),
72-
$input->getOption('target-language'),
73-
$output
74-
);
66+
$apiKey = $input->getOption('api-key');
67+
$text = $input->getArgument('text');
68+
$targetLanguage = $input->getOption('target-language');
69+
require(__DIR__ . '/snippets/translate.php');
7570
}
76-
77-
// [START translate_translate_text]
78-
/**
79-
* @param $apiKey string Your API key.
80-
* @param $text string The text to translate.
81-
* @param $targetLanguage string The target language code.
82-
*/
83-
protected function translate($apiKey, $text, $targetLanguage)
84-
{
85-
$translate = new TranslateClient([
86-
'key' => $apiKey
87-
]);
88-
$result = $translate->translate($text, [
89-
'target' => $targetLanguage,
90-
]);
91-
print("Source language: $result[source]\n");
92-
print("Translation: $result[text]\n");
93-
}
94-
// [END translate_translate_text]
9571
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
3+
/**
4+
* Copyright 2016 Google Inc. All Rights Reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// [START translate_detect_language]
20+
use Google\Cloud\Translate\TranslateClient;
21+
22+
// $apiKey = 'YOUR-API-KEY';
23+
// $text = 'The text whose language to detect. This will be detected as en.';
24+
25+
$translate = new TranslateClient([
26+
'key' => $apiKey,
27+
]);
28+
$result = $translate->detectLanguage($text);
29+
print("Language code: $result[languageCode]\n");
30+
print("Confidence: $result[confidence]\n");
31+
// [END translate_detect_language]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
3+
/**
4+
* Copyright 2016 Google Inc. All Rights Reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// [START translate_list_codes]
20+
use Google\Cloud\Translate\TranslateClient;
21+
22+
// $apiKey = 'YOUR-API-KEY';
23+
24+
$translate = new TranslateClient([
25+
'key' => $apiKey,
26+
]);
27+
foreach ($translate->languages() as $code) {
28+
print("$code\n");
29+
}
30+
// [END translate_list_codes]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
3+
/**
4+
* Copyright 2016 Google Inc. All Rights Reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace Google\Cloud\Samples\Translate;
20+
21+
// [START translate_list_language_names]
22+
use Google\Cloud\Translate\TranslateClient;
23+
24+
// $apiKey = 'YOUR-API-KEY'
25+
// $targetLanguage = 'en'; // Print the names of the languages in which language?
26+
27+
$translate = new TranslateClient([
28+
'key' => $apiKey,
29+
]);
30+
$result = $translate->localizedLanguages([
31+
'target' => $targetLanguage,
32+
]);
33+
foreach ($result as $lang) {
34+
print("$lang[code]: $lang[name]\n");
35+
}
36+
// [END translate_list_language_names]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
3+
/**
4+
* Copyright 2016 Google Inc. All Rights Reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace Google\Cloud\Samples\Translate;
20+
21+
// [START translate_translate_text]
22+
use Google\Cloud\Translate\TranslateClient;
23+
24+
// $apiKey = 'YOUR-API-KEY'
25+
// $text = 'The text to translate."
26+
// $targetLanguage = 'ja'; // Which language to translate to?
27+
28+
$translate = new TranslateClient([
29+
'key' => $apiKey
30+
]);
31+
$result = $translate->translate($text, [
32+
'target' => $targetLanguage,
33+
]);
34+
print("Source language: $result[source]\n");
35+
print("Translation: $result[text]\n");
36+
// [END translate_translate_text]

0 commit comments

Comments
 (0)