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 May 27, 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)->collectResults();
/*outputs
Collection {#171 ▼
#items: array:3 [▼
"translations" => array:1 [▼
0 => array:1 [▼
"translation" => "Les parents de Daniel adore la plage. Daniel et sa soeur et son frère aime la plage. Le chien de la famille aime la plage beaucoup."
]
]
"word_count" => 22
"character_count" => 124
]
}
*/
...
}
}
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)->collectResults();
/*outputs
Collection {#156 ▼
#items: array:3 [▼
"translations" => array:2 [▼
0 => array:1 [▼
"translation" => "Chaque samedi, Daniel et sa famille vont à la plage. Ils vivent loin de la plage, mais une fois par semaine la famille reçoit dans la voiture et les unités du père Daniel pour heures jusqu'à leur arrivée."
]
1 => array:1 [▼
"translation" => "Les parents de Daniel adore la plage. Daniel et sa soeur et son frère aime la plage. Le chien de la famille aime la plage beaucoup."
]
]
"word_count" => 57
"character_count" => 308
]
}
*/
....
}
}
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)->collectResults();
/*outputs
Collection {#156 ▼
#items: array:3 [▼
"translations" => array:2 [▼
0 => array:1 [▼
"translation" => "Chaque samedi, Daniel et sa famille vont à la plage. Ils vivent loin de la plage, mais une fois par semaine la famille reçoit dans la voiture et les unités du père Daniel pour heures jusqu'à leur arrivée."
]
1 => array:1 [▼
"translation" => "Les parents de Daniel adore la plage. Daniel et sa soeur et son frère aime la plage. Le chien de la famille aime la plage beaucoup."
]
]
"word_count" => 57
"character_count" => 308
]
}
*/
....
}
}
You may also be interested in getting only the translation or translations, instead of collectResults use
//Get only translation
$translator->from('en')->to('fr')->bulkTranslate($text)->getTranslation();
/*outputs
"Les parents de Daniel adore la plage. Daniel et sa soeur et son frère aime la plage. Le chien de la famille aime la plage beaucoup."
*/
You can also get results as array or raw json
//Get results as array
$translator->from('en')->to('fr')->bulkTranslate($text)->arrayResults();
//Get raw json results
$translator->from('en')->to('fr')->bulkTranslate($text)->getResults();