Thinkphp3.2.3 Multi-lingual translation and using Baidu translation

Source: Internet
Author: User
Tags echo translate error status code

One, thinkphp multi-lingual translation

First write the following in the common/conf/config.php file:

<?phpreturn Array (//' config item ' = + ' config value '//Language Pack config ' lang_switch_on ' + true,//turn on the Language pack feature ' Lang_auto_detect ' =&G T True,//Auto Detect language Open multilingual function after effective ' lang_list ' = ' zh-cn,en-us,zh-tw ',//must be written in the list of allowed languages ' var_language ' = ' + ',//default language switch changed volume);? >

Then create a new tags.php file under the Common/conf folder and write the contents:

<?php/** * Language Pack configuration file */return Array (//Add the following line definition can ' app_begin ' = = Array (' Behavior\checklangbehavior '),); >

Of course also write your own language pack file, under think/lang/There are four language pack files, you can be in these four or new language pack documentation can, or you create a new Lang folder under the module, such as Home/lang, the following new zh-cn.php, en-us.php, etc. language pack files. The Simplified Chinese Language pack can be written like this:

<?php/** * Simplified Chinese Language Pack */return array (' _login ' = "login",);

The English language pack can be written like this:

<?php/** * English Language Pack */return array (' _login ' = ' Login ',);

Then on a page, write the following:

{$Think. Lang._login}

Finally, in the browser to access the page, you can see, such as the address bar to enter the following URL:

Http://localhost/index.php?l=zh-cnhttp://localhost/index.php?l=en-us

So you can see the Chinese and English switch, you can try it yourself.


Second, the use of Baidu translation

I said here Baidu translation is not in http://fanyi.baidu.com/this URL input content Click Translation, here I am in the form of code to use curl for translation.

The translation files are placed in the/home/common/function.php, as follows:

<?phpclass Translate {    /**     *  supported languages       *  @var  ArrayAccess     */     static  $Lang  = Array  (         ' auto '  =>   ' Automatic detection ',         ' en '  =>  ' English ',          ' zh '  =>  ' Chinese ',         ' cht '  =>  ' Chinese Traditional ',    );     /**     *   Get supported languages      *  @return  array  back to supported languages       */    static function getlang ()  {         return self:: $Lang;    }    /**      *  performing text translation      *  @param  string  $text   text to translate      * @ param string  $from   Original language   default: Chinese      *  @param  string   $to   Target languages   default: English      *  @return  boolean string  translation failure: false  translation success: Translation results      */    static function exec ($ text,  $from  =  ' zh ',  $to  =  ' en ')  {          $url  =  "Http://fanyi.baidu.com/v2transapi";         $ data = array  (             ' from ')  =>  $from,             ' to '  =>   $to,             ' query '  => $ Text        );         $data  = http_build_query   (  $data  );         $ch  = curl_init  ();         curl_setopt  (  $ch, curlopt_url,  $url   );        curl_setopt  (  $ch, curlopt_referer,  " Http://fanyi.baidu.com " );        curl_setopt  (  $ch,  CURLOPT_USERAGENT,  ' mozilla/5.0  (windows nt 6.1; rv:37.0)  gecko/20100101  firefox/37.0 '  );        curl_setopt  (  $ch,  curlopt_header, 0 );        curl_setopt  (  $ch,  CURLOPT_POST, 1 );        curl_setopt  (  $ch,  curlopt_postfields,  $data  );        curl_setopt  (  $ch,  curlopt_ returntransfer, 1 );        curl_setopt  (  $ch,  CURLOPT_TIMEOUT, 5 );         $result  = curl_ exec  (  $ch  );        curl_close  (  $ch  );          $result  = json_decode  (  $result,  true  );        //  Error status Code  999         if  ($result  [' error ')  {             return false;        }         return  $result  [' trans_result '] [' data '] [' 0 '] [' DST ';     }}? >

You can modify it according to your own needs.

echo translate::exec ("Hello everyone", "zh", "en"); Here the output is: Hello everyone

This translates into success.

Hope to help you!!! 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0071.gif "alt=" J_0071.gif "/>650" this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0078.gif "alt=" J_0078.gif "/>650" this.width=650; "src=" http://img.baidu.com/hi/ Jx2/j_0079.gif "alt=" J_0079.gif "/>



This article is from the "Gaovan" blog, make sure to keep this source http://gaowanyao.blog.51cto.com/11272977/1951473

Thinkphp3.2.3 Multi-lingual translation and using Baidu translation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.