Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion translate/api/src/TranslateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,25 @@ protected function configure()
InputOption::VALUE_REQUIRED,
'The ISO 639-1 code of language to translate to, eg. \'en\'.'
)
->addOption(
'model',
null,
InputOption::VALUE_REQUIRED,
'The model to use, "base" for standard and "nmt" for premium.'
)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$text = $input->getArgument('text');
$targetLanguage = $input->getOption('target-language');
require(__DIR__ . '/snippets/translate.php');
$model = $input->getOption('model');

if ($model) {
require(__DIR__ . '/snippets/translate_with_model.php');
} else {
require(__DIR__ . '/snippets/translate.php');
}
}
}
36 changes: 36 additions & 0 deletions translate/api/src/snippets/translate_with_model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


namespace Google\Cloud\Samples\Translate;

// [START translate_text_with_model]
use Google\Cloud\Translate\TranslateClient;

// $text = 'The text to translate.';
// $targetLanguage = 'ja'; // Which language to translate to?
// $model = 'nmt'; // "base" for standard edition, "nmt" for premium

$translate = new TranslateClient();
$result = $translate->translate($text, [
'target' => $targetLanguage,
'model' => $model,
]);
print("Source language: $result[source]\n");
print("Translation: $result[text]\n");
print("Model: $result[model]\n");
// [END translate_text_with_model]
20 changes: 20 additions & 0 deletions translate/api/test/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ public function testTranslateBadLanguage()
);
}

public function testTranslateWithModel()
{
$application = new Application();
$application->add(new TranslateCommand());
$commandTester = new CommandTester($application->get('translate'));
$commandTester->execute(
[
'text' => 'Hello.',
'-t' => 'ja',
'--model' => 'nmt',
],
['interactive' => false]
);
$this->assertEquals(0, $commandTester->getStatusCode());
$display = $this->getActualOutput();
$this->assertContains('Source language: en', $display);
$this->assertContains('Translation:', $display);
$this->assertContains('Model: nmt', $display);
}

public function testDetectLanguage()
{
$application = new Application();
Expand Down