Делаем переводчик с помощью Google Translate API (Рейтинг: +17)

Печать RSS
Функция google API переводчика для большого количества текста

---------------------------------------------------------------------------

Как сделать переводчик для сайта?
Все довольно просто.
Вставляем класс и функцию google_transl в нужное нам место и
делаем запрос в функцию:
$translate_text = google_transl($original_text, $inp_lan, $out_lan);
Где:
$original_text - текст который надо перевести
$inp_lan - язык текста который переводим (если передано пустое значение будет произведена попытка определить язык автоматически)
$out_lan - язык на который нужно перевести
На выходе получаем переведенный текст который исходя с примера описаного выше будет записан в переменную $translate_text
Скачать функцию в архиве можна по ссылке: translate.zip.

---------------------------------------------------------------------------

Код класса и сама функция:
<?php
/**
* Translating language with Google API
* @author gabe@fijiwebdesign.com mod BY GANJAR icq:993770 http://mytu.ru
* @version $Id$
* @license - Share-Alike 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
*
* Google requires attribution for their Language API, please see: http://code.google.com/apis/ajaxlanguage/documentation/#Branding
*
*/
class Google_Translate_API {
    /**
    * Translate a piece of text with the Google Translate API
    * @return String
    * @param $text String
    * @param $from String[optional] Original language of $text. An empty String will let google decide the language of origin
    * @param $to String[optional] Language to translate $text to
    */
    function translate($text, $from = '', $to = 'en') {
        $url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='.rawurlencode($text).'&langpair='.rawurlencode($from.'|'.$to);
        $response = file_get_contents(
            $url,
            null,
            stream_context_create(
                array(
                    'http'=>array(
                        'method'=>"GET",
                        'header'=>"Referer: http://".$_SERVER['HTTP_HOST']."/\r\n"
                    )
                )
            )
        );
        if (preg_match("/{\"translatedText\":\"([^\"]+)\"/i", $response, $matches)) {
            return self::_unescapeUTF8EscapeSeq($matches[1]);
        }
        return false;
    }
    /**
    * Convert UTF-8 Escape sequences in a string to UTF-8 Bytes. Old version.
    * @return UTF-8 String
    * @param $str String
    */
    function __unescapeUTF8EscapeSeq($str) {
        return preg_replace_callback("/\\\u([0-9a-f]{4})/i", create_function('$matches', 'return html_entity_decode(\'&#x\'.$matches[1].\';\', ENT_NOQUOTES, \'UTF-8\');'), $str);
    }
    /**
    * Convert UTF-8 Escape sequences in a string to UTF-8 Bytes
    * @return UTF-8 String
    * @param $str String
    */
    function _unescapeUTF8EscapeSeq($str) {
        return preg_replace_callback("/\\\u([0-9a-f]{4})/i", create_function('$matches', 'return Google_Translate_API::_bin2utf8(hexdec($matches[1]));'), $str);
    }
    /**
    * Convert binary character code to UTF-8 byte sequence
    * @return String
    * @param $bin Mixed Interger or Hex code of character
    */
    function _bin2utf8($bin) {
        if ($bin <= 0x7F) {
            return chr($bin);
        } else if ($bin >= 0x80 && $bin <= 0x7FF) {
            return pack("C*", 0xC0 | $bin >> 6, 0x80 | $bin & 0x3F);
        } else if ($bin >= 0x800 && $bin <= 0xFFF) {
            return pack("C*", 0xE0 | $bin >> 11, 0x80 | $bin >> 6 & 0x3F, 0x80 | $bin & 0x3F);
        } else if ($bin >= 0x10000 && $bin <= 0x10FFFF) {
            return pack("C*", 0xE0 | $bin >> 17, 0x80 | $bin >> 12 & 0x3F, 0x80 | $bin >> 6& 0x3F, 0x80 | $bin & 0x3F);
        }
    }
}

function google_transl($original_text, $inp_lan = '', $out_lan){
    $output_text = ''; //Значение на вывод
    if(mb_strlen($original_text ,'UTF-8')>300){
        preg_match_all('!(.{200,300})(\s|,|\.|-|\?|\!|\(|\)|\")!Us', $original_text, $text);
        $text_2 = $text;
        $text_2 = array_pop($text_2[1]);
        preg_match_all('!'.preg_quote ( $text_2 ).'(.*)$!Us', $original_text, $text_end);
        $text[0][] = array_pop($text_end[1]);
        $text = $text[0];
        foreach($text AS $key=>$value){
            $trans_text = Google_Translate_API::translate($value, $inp_lan, $out_lan);
            if ($trans_text !== false) {
                $output_text .= $trans_text.' ';
            }
        }
    } else {
        $trans_text = Google_Translate_API::translate($original_text, $inp_lan, $out_lan);
        if ($trans_text !== false) {
            $output_text = $trans_text;
        }
    }
    return str_replace('  ', ' ',str_replace('<br>', "\n",str_replace('\n', ' ',str_replace('\r', ' ',$output_text))));
}
?>
Добавил:
Рейтинг: +17
Просмотры: 4677
Комментарии (17) »