Skip to content

Commit 44f93b8

Browse files
Accomodate code review comments.
1 parent 8a7cbba commit 44f93b8

File tree

7 files changed

+111
-69
lines changed

7 files changed

+111
-69
lines changed

translate/api/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ Translate API.
1212
2. Click `Credentials` -> `Create Credentials` -> `API key`. Copy the
1313
API key.
1414

15-
3. Replace `YOUR-API-KEY` in `translate.php` with your api key copied
16-
in step 2.
17-
18-
4. Run:
15+
3. Run:
1916
```
2017
$ php translate.php
2118
Console Tool

translate/api/src/DetectLanguageCommand.php

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

1919
namespace Google\Cloud\Samples\Translate;
2020

21+
// [START translate_detect_language]
2122
use Google\Cloud\Translate\TranslateClient;
23+
// [END translate_detect_language]
2224
use Symfony\Component\Console\Command\Command;
2325
use Symfony\Component\Console\Input\InputArgument;
26+
use Symfony\Component\Console\Input\InputOption;
2427
use Symfony\Component\Console\Input\InputInterface;
2528
use Symfony\Component\Console\Output\OutputInterface;
2629

@@ -29,21 +32,16 @@
2932
*/
3033
class DetectLanguageCommand extends Command
3134
{
32-
public function __construct($apiKey)
33-
{
34-
parent::__construct();
35-
$this->apiKey = $apiKey;
36-
}
3735
protected function configure()
3836
{
3937
$this
4038
->setName('detect')
4139
->setDescription('Detect which language text was written in using '
4240
. 'Google Cloud Translate API')
43-
->setHelp(<<<'EOF'
41+
->setHelp(<<
4442
The %command.name% command detects which language text was written in using the Google Cloud Translate API.
4543
46-
php %command.full_name% "Your text here"
44+
php %command.full_name% -k YOUR-API-KEY "Your text here"
4745
4846
EOF
4947
)
@@ -52,23 +50,36 @@ protected function configure()
5250
InputArgument::REQUIRED,
5351
'The text to examine.'
5452
)
53+
->addOption(
54+
'api-key',
55+
'k',
56+
InputOption::VALUE_REQUIRED,
57+
'Your API key.'
58+
)
5559
;
5660
}
5761

5862
protected function execute(InputInterface $input, OutputInterface $output)
5963
{
60-
$this->detectLanguage($input->getArgument('text'), $output);
64+
$this->detectLanguage(
65+
$input->getOption('api-key'),
66+
$input->getArgument('text')
67+
);
6168
}
6269

6370
// [START translate_detect_language]
64-
protected function detectLanguage($text, OutputInterface $output)
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)
6576
{
6677
$translate = new TranslateClient([
67-
'key' => $this->apiKey,
78+
'key' => $apiKey,
6879
]);
6980
$result = $translate->detectLanguage($text);
70-
$output->writeln("Language code: $result[languageCode]");
71-
$output->writeln("Confidence: $result[confidence]");
81+
print("Language code: $result[languageCode]\n");
82+
print("Confidence: $result[confidence]\n");
7283
}
7384
// [END translate_detect_language]
7485
}

translate/api/src/ListCodesCommand.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,57 @@
1818

1919
namespace Google\Cloud\Samples\Translate;
2020

21+
// [START translate_list_codes]
2122
use Google\Cloud\Translate\TranslateClient;
23+
// [END translate_list_codes]
2224
use Symfony\Component\Console\Command\Command;
2325
use Symfony\Component\Console\Input\InputInterface;
26+
use Symfony\Component\Console\Input\InputOption;
2427
use Symfony\Component\Console\Output\OutputInterface;
2528

2629
/**
2730
* Command line utility to translate.
2831
*/
2932
class ListCodesCommand extends Command
3033
{
31-
public function __construct($apiKey)
32-
{
33-
parent::__construct();
34-
$this->apiKey = $apiKey;
35-
}
3634
protected function configure()
3735
{
3836
$this
3937
->setName('list-codes')
4038
->setDescription('List all the language codes in the ' .
4139
'Google Cloud Translate API')
42-
->setHelp(<<<'EOF'
40+
->setHelp(<<
4341
The %command.name% command lists all the language codes in the Google Cloud Translate API.
4442
45-
php %command.full_name%
43+
php %command.full_name% -k YOUR-API-KEY
4644
4745
EOF
4846
)
47+
->addOption(
48+
'api-key',
49+
'k',
50+
InputOption::VALUE_REQUIRED,
51+
'Your API key.'
52+
)
4953
;
5054
}
5155

5256
protected function execute(InputInterface $input, OutputInterface $output)
5357
{
54-
$this->listCodes($output);
58+
$this->listCodes($input->getOption('api-key'));
5559
}
5660

5761
// [START translate_list_codes]
58-
protected function listCodes(OutputInterface $output)
62+
/**
63+
* @param $apiKey string Your API key.
64+
*/
65+
protected function listCodes($apiKey)
5966
{
6067
$translate = new TranslateClient([
61-
'key' => $this->apiKey,
68+
'key' => $apiKey,
6269
]);
6370
foreach ($translate->languages() as $code) {
64-
$output->writeln($code);
71+
print("$code\n");
6572
}
6673
}
6774
// [END translate_list_codes]

translate/api/src/ListLanguagesCommand.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
namespace Google\Cloud\Samples\Translate;
2020

21+
// [START translate_list_language_names]
2122
use Google\Cloud\Translate\TranslateClient;
23+
// [END translate_list_language_names]
2224
use Symfony\Component\Console\Command\Command;
2325
use Symfony\Component\Console\Input\InputInterface;
2426
use Symfony\Component\Console\Input\InputOption;
@@ -29,24 +31,25 @@
2931
*/
3032
class ListLanguagesCommand extends Command
3133
{
32-
public function __construct($apiKey)
33-
{
34-
parent::__construct();
35-
$this->apiKey = $apiKey;
36-
}
3734
protected function configure()
3835
{
3936
$this
4037
->setName('list-langs')
4138
->setDescription('List language codes and names in the '
4239
. 'Google Cloud Translate API')
43-
->setHelp(<<<'EOF'
40+
->setHelp(<<
4441
The %command.name% lists language codes and names in the Google Cloud Translate API.
4542
46-
php %command.full_name% -t en
43+
php %command.full_name% -k YOUR-API-KEY -t en
4744
4845
EOF
4946
)
47+
->addOption(
48+
'api-key',
49+
'k',
50+
InputOption::VALUE_REQUIRED,
51+
'Your API key.'
52+
)
5053
->addOption(
5154
'target-language',
5255
't',
@@ -58,20 +61,28 @@ protected function configure()
5861

5962
protected function execute(InputInterface $input, OutputInterface $output)
6063
{
61-
$this->listLanguage($input->getOption('target-language'), $output);
64+
$this->listLanguage(
65+
$input->getOption('api-key'),
66+
$input->getOption('target-language')
67+
);
6268
}
6369

6470
// [START translate_list_language_names]
65-
protected function listLanguage($targetLanguage, OutputInterface $output)
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)
6677
{
6778
$translate = new TranslateClient([
68-
'key' => $this->apiKey,
79+
'key' => $apiKey,
6980
]);
7081
$result = $translate->localizedLanguages([
7182
'target' => $targetLanguage,
7283
]);
7384
foreach ($result as $lang) {
74-
$output->writeln("$lang[code]: $lang[name]");
85+
print("$lang[code]: $lang[name]\n");
7586
}
7687
}
7788
// [END translate_list_language_names]

translate/api/src/TranslateCommand.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
namespace Google\Cloud\Samples\Translate;
2020

21+
// [START translate_translate_text]
2122
use Google\Cloud\Translate\TranslateClient;
23+
// [END translate_translate_text]
2224
use Symfony\Component\Console\Command\Command;
2325
use Symfony\Component\Console\Input\InputArgument;
2426
use Symfony\Component\Console\Input\InputInterface;
@@ -30,23 +32,24 @@
3032
*/
3133
class TranslateCommand extends Command
3234
{
33-
public function __construct($apiKey)
34-
{
35-
parent::__construct();
36-
$this->apiKey = $apiKey;
37-
}
3835
protected function configure()
3936
{
4037
$this
4138
->setName('translate')
4239
->setDescription('Translate text using Google Cloud Translate API')
43-
->setHelp(<<<'EOF'
40+
->setHelp(<<
4441
The %command.name% command transcribes audio using the Google Cloud Translate API.
4542
46-
php %command.full_name% -t ja "Hello World."
43+
php %command.full_name% -k YOUR-API-KEY -t ja "Hello World."
4744
4845
EOF
4946
)
47+
->addOption(
48+
'api-key',
49+
'k',
50+
InputOption::VALUE_REQUIRED,
51+
'Your API key.'
52+
)
5053
->addArgument(
5154
'text',
5255
InputArgument::REQUIRED,
@@ -64,23 +67,29 @@ protected function configure()
6467
protected function execute(InputInterface $input, OutputInterface $output)
6568
{
6669
$this->translate(
70+
$input->getOption('api-key'),
6771
$input->getArgument('text'),
6872
$input->getOption('target-language'),
6973
$output
7074
);
7175
}
7276

7377
// [START translate_translate_text]
74-
protected function translate($text, $targetLanguage, OutputInterface $output)
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)
7584
{
7685
$translate = new TranslateClient([
77-
'key' => $this->apiKey,
86+
'key' => $apiKey
7887
]);
7988
$result = $translate->translate($text, [
8089
'target' => $targetLanguage,
8190
]);
82-
$output->writeln("Source language: $result[source]");
83-
$output->writeln("Translation: $result[text]");
91+
print("Source language: $result[source]\n");
92+
print("Translation: $result[text]\n");
8493
}
8594
// [END translate_translate_text]
8695
}

0 commit comments

Comments
 (0)