Hi friends, Today we will learn How to use google language translator in PHP. Mostly we use this when we get data from another website or we can make a translator for our own website.


Note - follow the steps.

We have two ways to convert language in php.

1 -  php-google-translate-free Package 

2-   use class file of google translate.


 We will discuss both of them. which is easy to use you can use ЁЯШД


1 php-google-translate-free Package 


 First, you have to install this package with the composer by your command line. Open your terminal and go to your folder were is your projects and paste here this command below.

Example -   

      phpProject is my folder run this command
  •  composer require statickidz/php-google-translate-free

 ShortTutorials-MacBook-Air:phpProject $ composer require statickidz/php-google-translate-free


Or go to your project folder and edit your composer.json file and add this

"require": { 
      "statickidz/php-google-translate-free": "^1.1" 
    }

Now create a file translater.php and paste this code.


 <?php

   require_once ('vendor/autoload.php');  // include autoload.php

     use \Statickidz\GoogleTranslate;   use this package 


    $from = 'zh';   // language code who you want to convert  

    $to = 'en';    // language code in which language you want to change

    $text = 'ц╢▓цЩ╢цШ╛чд║хЩи';  // language text 


    $trans = new GoogleTranslate(); //  make object 

    $result = $trans->translate($from, $to, $text);


    echo $result; // output -> LCD monitor


   ?>


Example -  If you want to convert zh (Chinese (Simplified)) to en (english)  Language.

2-   Use the class file of google translate.

        Go to your project folder and create a class file named google_translate.class.php

        Copy this code and paste it to your google_translate.class.php


<?php


class GoogleTranslate

{


    /**

     * @param string $source

     * @param string $target

     * @param string $text

     * @return string

     */

    public static function translate($from, $to, $text) {


        // Request translation

        $response = self::requestTranslation($from, $to, $text);


        // Clean translation

        $translation = self::getSentencesFromJSON($response);


        return $translation;

    }


    /**

     * @param string $source

     * @param string $target

     * @param string $text

     * @return array

     */

    protected static function requestTranslation($from, $to, $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($from),

            'tl' => urlencode($to),

            'q' => urlencode($text)

        );


        // 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($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, count($fields));

        curl_setopt($ch, CURLOPT_POSTFIELDS

, $fields_string);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        curl_setopt($ch, CURLOPT_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;

    }



    /**

     * @param string $json

     * @return string

     */

    protected static function getSentencesFromJSON($json) {

        $sentencesArray = json_decode($json, true);

        $sentences = "";


        foreach ($sentencesArray["sentences"] as $s) {

            $sentences .= $s["trans"];

        }


        return $sentences;

    }


}

?>
       

 After pasting  this code create another file named translate.php in the same folder


     Copy this code 


    include_once ('google_translate.class.php'); //include class file


         $from = 'zh-CN';     // Chinese language code

    $to = 'en';  // English code 

    $text = "х╛охЮЛчФ╡шДСф╕╗цЬ║";  // text 

    call class file function translate

         $lng = GoogleTranslate::translate($source,$target,$text);

    echo $lng   // Output -> Microcomputer host

 

Yeah! You have learned ЁЯШК

I hope you understood this and like this article. if you like it so, please give feedback in the comment section.


If any mistakes I made here, so please let me know and improve me.

If you have any query, feel free to ask me  ЁЯШК

Recommended Post


Post a Comment

Please do not enter any spam link in the comment section.

Previous Post Next Post