نمایش نتایج: از شماره 1 تا 10 , از مجموع 11

موضوع: استفاده از api گوگل در ترجمه خودکار

Threaded View

پست قبلی پست قبلی   پست بعدی پست بعدی
  1. #7
    عضو انجمن rezaonline.net آواتار ها
    تاریخ عضویت
    Apr 2012
    محل سکونت
    Sanandaj | Tehran
    نوشته ها
    939
    تشکر تشکر کرده 
    1,556
    تشکر تشکر شده 
    2,338
    تشکر شده در
    1,230 پست

    پیش فرض پاسخ : استفاده از api گوگل در ترجمه خودکار

    کد PHP:
    <?php

    /*
    $source = 'en';
    $target = 'fa';
    $text = 'Hi , this is sample test by rezaonline.net';

    $trans = new GoogleTranslate();
    $result = $trans->translate($source, $target, $text);
    echo "<meta charset=utf-8><pre dir=rtl>";
    echo $result;

    */












    /**
     * GoogleTranslate.class.php
     *
     * Class to talk with Google Translator for free.
     *
     * @package PHP Google Translate Free;
     * @category Translation
     * @author Adrián Barrio Andrés
     * @author Paris N. Baltazar Salguero <sieg.sb@gmail.com>
     * @copyright 2016 Adrián Barrio Andrés
     * @license https://opensource.org/licenses/GPL-3.0 GNU General Public License 3.0
     * @version 2.0
     * @link https://statickidz.com/
     */

    /**
     * Main class GoogleTranslate
     *
     * @package GoogleTranslate
     *
     */
    class GoogleTranslate
    {

        
    /**
         * Retrieves the translation of a text
         *
         * @param string $source
         *            Original language of the text on notation xx. For example: es, en, it, fr...
         * @param string $target
         *            Language to which you want to translate the text in format xx. For example: es, en, it, fr...
         * @param string $text
         *            Text that you want to translate
         *
         * @return string a simple string with the translation of the text in the target language
         */
        
    public static function translate($source$target$text)
        {
            
    // Request translation
            
    $response self::requestTranslation($source$target$text);

            
    // Get translation text
            // $response = self::getStringBetween("onmouseout=\"this.style.backgroundColor='#fff'\">", "</span></div>", strval($response));

            // Clean translation
            
    $translation self::getSentencesFromJSON($response);

            return 
    $translation;
        }

        
    /**
         * Internal function to make the request to the translator service
         *
         * @internal
         *
         * @param string $source
         *            Original language taken from the 'translate' function
         * @param string $target
         *            Target language taken from the ' translate' function
         * @param string $text
         *            Text to translate taken from the 'translate' function
         *
         * @return object[] The response of the translation service in JSON format
         */
        
    protected static function requestTranslation($source$target$text)
        {

            
    // Google translate URL
            
    $url "https://translate.google.com/translate_a/single?client=at&dt=t&dt=ld&dt=qca&dt=rm&dt=bd&dj=1&hl=es-ES&ie=UTF-8&oe=UTF-8&inputm=2&otf=2&iid=1dd3b944-fa62-4b55-b330-74909a99969e";

            
    $fields = array(
                
    'sl' => urlencode($source),
                
    'tl' => urlencode($target),
                
    'q' => urlencode($text)
            );

            if(
    strlen($fields['q'])>=5000)
                throw new \
    Exception("Maximum number of characters exceeded: 5000");
            
            
    // URL-ify the data for the POST
            
    $fields_string "";
            foreach (
    $fields as $key => $value) {
                
    $fields_string .= $key '=' $value '&';
            }

            
    rtrim($fields_string'&');

            
    // Open connection
            
    $ch curl_init();

            
    // Set the url, number of POST vars, POST data
            
    curl_setopt($chCURLOPT_URL$url);
            
    curl_setopt($chCURLOPT_POSTcount($fields));
            
    curl_setopt($chCURLOPT_POSTFIELDS$fields_string);
            
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
            
    curl_setopt($chCURLOPT_ENCODING'UTF-8');
            
    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
            
    curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse);
            
    curl_setopt($chCURLOPT_USERAGENT'AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1');

            
    // Execute post
            
    $result curl_exec($ch);

            
    // Close connection
            
    curl_close($ch);

            return 
    $result;
        }

        
    /**
         * Dump of the JSON's response in an array
         *
         * @param string $json
         *            The JSON object returned by the request function
         *
         * @return string A single string with the translation
         */
        
    protected static function getSentencesFromJSON($json)
        {
            
    $sentencesArray json_decode($jsontrue);
            
    $sentences "";

            foreach (
    $sentencesArray["sentences"] as $s) {
                
    $sentences .= isset($s["trans"]) ? $s["trans"] : '';
            }

            return 
    $sentences;
        }
    }
    روش استفاده
    کد PHP:
    $source 'en';
    $target 'fa';
    $text 'Hi , this is sample test by rezaonline.net';

    $trans = new GoogleTranslate();
    $result $trans->translate($source$target$text);
    echo 
    "<meta charset=utf-8><pre dir=rtl>";
    echo 
    $result

  2. تعداد تشکر ها ازrezaonline.net به دلیل پست مفید


اطلاعات موضوع

کاربرانی که در حال مشاهده این موضوع هستند

در حال حاضر 1 کاربر در حال مشاهده این موضوع است. (0 کاربران و 1 مهمان ها)

موضوعات مشابه

  1. پاسخ ها: 59
    آخرين نوشته: January 18th, 2015, 16:02
  2. سوال - بستن رنج ای پی با استفاده دسترسی روت به سرور مجازی
    توسط Admin_chatBaran.ir در انجمن سوالات و مشکلات
    پاسخ ها: 2
    آخرين نوشته: January 8th, 2015, 19:53
  3. پاسخ ها: 84
    آخرين نوشته: July 28th, 2014, 02:59
  4. پاسخ ها: 2
    آخرين نوشته: January 19th, 2013, 00:12
  5. پاسخ ها: 20
    آخرين نوشته: February 28th, 2010, 03:39

مجوز های ارسال و ویرایش

  • شما نمیتوانید موضوع جدیدی ارسال کنید
  • شما امکان ارسال پاسخ را ندارید
  • شما نمیتوانید فایل پیوست کنید.
  • شما نمیتوانید پست های خود را ویرایش کنید
  •