This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Translating using source and target
Percy Mamedy edited this page Feb 1, 2016
·
5 revisions
You can use the FindBrok\WatsonTranslate\Contracts\TranslatorInterface to perform translations, this interface is resolved by the IOC and you can inject it in your controller methods.
Translating a sentence or single input.
<?php
namespace App\Http\Controllers;
...
use FindBrok\WatsonTranslate\Contracts\TranslatorInterface as WatsonTranslator;
class IndexController extends Controller {
/**
* Translate demo
*
* @param WatsonTranslator $translator
*/
public function index(WatsonTranslator $translator)
{
//Text to translate
$text = 'Daniel\'s parents love the beach. Daniel and his sister and brother love the beach. The family\'s dog loves the beach very much.';
//Translate to french
$results = $translator->from('en')->to('fr')->textTranslate($text);
....
}
}
Translating multiple sentences, can be used as a way to translate paragraphs as well.
<?php
namespace App\Http\Controllers;
...
use FindBrok\WatsonTranslate\Contracts\TranslatorInterface as WatsonTranslator;
class IndexController extends Controller {
/**
* Translate demo
*
* @param WatsonTranslator $translator
*/
public function index(WatsonTranslator $translator)
{
//Array of sentences to translate
$text = [
'Every Saturday, Daniel and his family go to the beach. They live far from the beach, but once a week the family gets into the car and Daniel\'s father drives for hours until they arrive.',
'Daniel\'s parents love the beach. Daniel and his sister and brother love the beach. The family\'s dog loves the beach very much.',
];
//Translate to french
$results = $translator->from('en')->to('fr')->bulkTranslate($text);
....
}
}
Alternatively you can use the WatsonTranslate Facade for achieving the same results
<?php
namespace App\Http\Controllers;
...
use WatsonTranslate;
class IndexController extends Controller {
/**
* Translate demo
*/
public function index()
{
//Array of sentences to translate
$text = [
'Every Saturday, Daniel and his family go to the beach. They live far from the beach, but once a week the family gets into the car and Daniel\'s father drives for hours until they arrive.',
'Daniel\'s parents love the beach. Daniel and his sister and brother love the beach. The family\'s dog loves the beach very much.',
];
//Translate to french
$results = WatsonTranslate::from('en')->to('fr')->bulkTranslate($text);
....
}
}