Skip to content

Commit 0ddcbd5

Browse files
authored
add translate model premium feature (GoogleCloudPlatform#324)
1 parent 7cef51e commit 0ddcbd5

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

translate/api/src/TranslateCommand.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,25 @@ protected function configure()
5252
InputOption::VALUE_REQUIRED,
5353
'The ISO 639-1 code of language to translate to, eg. \'en\'.'
5454
)
55+
->addOption(
56+
'model',
57+
null,
58+
InputOption::VALUE_REQUIRED,
59+
'The model to use, "base" for standard and "nmt" for premium.'
60+
)
5561
;
5662
}
5763

5864
protected function execute(InputInterface $input, OutputInterface $output)
5965
{
6066
$text = $input->getArgument('text');
6167
$targetLanguage = $input->getOption('target-language');
62-
require(__DIR__ . '/snippets/translate.php');
68+
$model = $input->getOption('model');
69+
70+
if ($model) {
71+
require(__DIR__ . '/snippets/translate_with_model.php');
72+
} else {
73+
require(__DIR__ . '/snippets/translate.php');
74+
}
6375
}
6476
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
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+
19+
namespace Google\Cloud\Samples\Translate;
20+
21+
// [START translate_text_with_model]
22+
use Google\Cloud\Translate\TranslateClient;
23+
24+
// $text = 'The text to translate.';
25+
// $targetLanguage = 'ja'; // Which language to translate to?
26+
// $model = 'nmt'; // "base" for standard edition, "nmt" for premium
27+
28+
$translate = new TranslateClient();
29+
$result = $translate->translate($text, [
30+
'target' => $targetLanguage,
31+
'model' => $model,
32+
]);
33+
print("Source language: $result[source]\n");
34+
print("Translation: $result[text]\n");
35+
print("Model: $result[model]\n");
36+
// [END translate_text_with_model]

translate/api/test/CommandTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ public function testTranslateBadLanguage()
5959
);
6060
}
6161

62+
public function testTranslateWithModel()
63+
{
64+
$application = new Application();
65+
$application->add(new TranslateCommand());
66+
$commandTester = new CommandTester($application->get('translate'));
67+
$commandTester->execute(
68+
[
69+
'text' => 'Hello.',
70+
'-t' => 'ja',
71+
'--model' => 'nmt',
72+
],
73+
['interactive' => false]
74+
);
75+
$this->assertEquals(0, $commandTester->getStatusCode());
76+
$display = $this->getActualOutput();
77+
$this->assertContains('Source language: en', $display);
78+
$this->assertContains('Translation:', $display);
79+
$this->assertContains('Model: nmt', $display);
80+
}
81+
6282
public function testDetectLanguage()
6383
{
6484
$application = new Application();

0 commit comments

Comments
 (0)