Example #1
0
#!/usr/bin/php
<?php 
$config = ['require_services' => ['guzzle'], 'git_urls' => ['https://github.com/yfix/google-translate-php.git' => 'google-translate-php/'], 'autoload_config' => ['google-translate-php/src/Stichoza/GoogleTranslate/' => 'Stichoza\\GoogleTranslate'], 'example' => function () {
    $tr = new Stichoza\GoogleTranslate\TranslateClient('en', 'ru');
    echo $tr->translate('Hello World!');
}];
if ($return_config) {
    return $config;
}
require_once __DIR__ . '/_yf_autoloader.php';
new yf_autoloader($config);
Example #2
-1
 /**
  */
 function google_translate($text, $lang_from, $lang_to, $params = [], &$cache_used = false)
 {
     if (!strlen($text) || !$lang_from || !$lang_to) {
         return false;
     }
     $md5 = md5($lang_from . '|' . $lang_to . '|' . $text);
     $table = 'cache_google_translate';
     $cached = db()->from($table)->where('lang_from', $lang_from)->where('lang_to', $lang_to)->where('md5', $md5)->get();
     if (isset($cached['translated'])) {
         $cache_used = true;
         return $cached['translated'];
     } else {
         $this->require_php_lib('google_translate');
         try {
             $translated = Stichoza\GoogleTranslate\TranslateClient::translate($lang_from, $lang_to, $text);
         } catch (Exception $e) {
             echo 'Error: exception caught: ' . $e->getMessage() . PHP_EOL;
         }
         db()->insert_safe($table, ['md5' => $md5, 'lang_from' => $lang_from, 'lang_to' => $lang_to, 'source' => $text, 'translated' => $translated, 'date' => date('Y-m-d H:i:s')]);
     }
     return $translated;
 }