PHP Classes

File: examples/translate.php

Recommend this page to a friend!
  Classes of Sergey Karavay   PHPolyglot   examples/translate.php   Download  
File: examples/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: 827 bytes
 

Contents

Class file image Download
<?php

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

use
GinoPane\PHPolyglot\PHPolyglot;

try {
   
$phpolyglot = new PHPolyglot();

   
$textToTranslate = 'Hello world';

   
$languages = ['it', 'de', 'es', 'ru', 'fi', 'be', 'en'];
   
$languageFrom = 'en';

    foreach (
$languages as $languageTo) {
       
$response = $phpolyglot->translate($textToTranslate, $languageTo, $languageFrom);

        echo
sprintf(
           
"%s (%s) => (%s) %s\n",
           
$textToTranslate,
           
$languageFrom,
           
$languageTo,
           
$response
       
);

       
$textToTranslate = $response->getTranslations()[0];

       
$languageFrom = $languageTo;
    }
} catch (
Exception $exception) {
   
$errorMessage = $exception->getMessage();

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

echo
PHP_EOL;