PHP Classes

File: examples/lookup-translate.php

Recommend this page to a friend!
  Classes of Sergey Karavay   PHPolyglot   examples/lookup-translate.php   Download  
File: examples/lookup-translate.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHPolyglot
Translate, correct spelling and speak a given text
Author: By
Last change: Updates:
- fixed some minor issues.
Date: 6 years ago
Size: 865 bytes
 

Contents

Class file image Download
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use
GinoPane\PHPolyglot\PHPolyglot;

try {
   
$phpolyglot = new PHPolyglot();

   
$textToLookup = 'Hello!';

   
$languageFrom = 'en';
   
$languageTo = 'ru';

   
$response = $phpolyglot->lookup($textToLookup, $languageFrom, $languageTo)->getEntries();

    if (empty(
$response)) {
        throw new
Exception('Nothing returned! Maybe API has changed?');
    }

    echo
"Word to translate: $textToLookup \n";

    echo
"Translations: \n";

    foreach (
$response as $entry) {
        echo
$entry->getTextTo();

        if (
$meanings = $entry->getMeanings()) {
            echo
" (" . implode(", ", $meanings) . ")";
        }

        echo
"\n";
    }
} catch (
Exception $exception) {
   
$errorMessage = $exception->getMessage();

    echo
sprintf("Error happened: %s", $errorMessage);
}

echo
PHP_EOL;