插件97:Google 翻译

<?php // Plug-in 97: Google Translate
/*
* Google翻译
* 插件说明:
* 插件接受一个字符串,把它从一种语言翻译到另一种语言。
* 若操作成功,则返回译文,否则返回FALSE。
* 它需要以下参数:
* $text 需要翻译的文本
* $lang1 源语言
* $lang2 目标语言
*/
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

echo '<html><head><meta http-equiv="Content-Type" ' .
     'content="text/html; charset=utf-8" /></head><body>';

$text = "We hold these truths to be self-evident, that all " .
        "men are created equal, that they are endowed by " .
        "their creator with certain unalienable rights, that " .
        "among these are life, liberty and the pursuit of " .
        "happiness. That to secure these rights, governments " .
        "are instituted among men, deriving their just powers " .
        "from the consent of the governed.";
$from = 'English';
$to   = 'German';

echo "<b>Original</b>: $text<br /><br />";
echo "<i>Translated from $from to $to:</i><br /><br />";

$result = PIPHP_GoogleTranslate($text, $from, $to);
if (!$result) echo "Translation failed.";
else echo "<b>Translation</b>: $result";

function PIPHP_GoogleTranslate($text, $lang1, $lang2)
{
   // Plug-in 97: Google Translate (Updated)
   //
   // This plug-in uses Google Translate to translate text from
   // one language to another. Upon success it returns the
   // translation using UTF-8 to enable accented and other
   // characters. On failure it returns FALSE. It requires these
   // arguments:
   //
   //    $text:  Text to translate
   //    $lang1: The source language
   //    $lang2: The destination language
   //
   // $lang1 and $lang2 must each be one of: Arabic, Bulgarian,
   // Simplified Chinese, Traditional Chinese, Croatian, Czech,
   // Danish, Dutch, English, Finnish, French, German, Greek,
   // Hindi, Italian, Japanese, Korean, Polish, Portuguese,
   // Romanian, Russian, Spanish or Swedish.

   $langs = array(
      'arabic'              => 'ar',
      'bulgarian'           => 'bg',
      'simplified chinese'  => 'zh-cn',
      'traditional chinese' => 'zh-tw',
      'croatian'            => 'hr',
      'czech'               => 'cs',
      'danish'              => 'da',
      'dutch'               => 'nl',
      'english'             => 'en',
      'finnish'             => 'fi',
      'french'              => 'fr',
      'german'              => 'de',
      'greek'               => 'el',
      'hindi'               => 'hi',
      'italian'             => 'it',
      'japanese'            => 'ja',
      'korean'              => 'ko',
      'polish'              => 'pl',
      'portuguese'          => 'pt',
      'romanian'            => 'ro',
      'russian'             => 'ru',
      'spanish'             => 'es',
      'swedish'             => 'sv');

   $lang1 = strtolower($lang1);
   $lang2 = strtolower($lang2);
   $root  = 'http://ajax.googleapis.com/ajax/services';
   $url   = $root . '/language/translate?v=1.0&q=';
   
   if (!isset($langs[$lang1]) || !isset($langs[$lang2]))
      return FALSE;

   $json = @file_get_contents($url . urlencode($text) .
           '&langpair='. $langs[$lang1] . '%7C' .
           $langs[$lang2]);

   if (!strlen($json)) return FALSE;
   
   $result = json_decode($json);
   return $result->responseData->translatedText;
 }

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值