public translate ( string $word, string $toLang, null $fromLang = null ) : null | string | ||
$word | string | Sentence or word to translate |
$toLang | string | Target language |
$fromLang | null | Source language (if set to null, translator will try to guess) |
return | null | string | The translated sentence or null if an error occurs |
public function translate($word, $toLang, $fromLang = null)
{
try {
if (is_null($fromLang) && !is_null($this->default_language)) {
$fromLang = $this->default_language;
}
$translation = $this->bingTranslator->translate($word, $toLang, $fromLang);
return $translation->getBody();
} catch (\MicrosoftTranslator\Exception $e) {
if (!(strpos($e->getMessage(), 'Unable to generate a new access token') === false)) {
throw $e;
}
}
return null;
}