内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>javascript - How to use google translator on a web site without using the combo box to change language? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use google translator on a web site without using the combo box to change language? - Stack Overflow

programmeradmin0浏览0评论

I want to add the multi languages features on my website so I've added the google translate tool (Translate Tool) but I want to translate the website only when the user click on a button in the page and not when it uses the bo! Is there any way to change automatically the value of the bo when a button is clicked?

This is the code of the translate tools:

 <div id="google_translate_element"></div><script type="text/javascript">
      function googleTranslateElementInit() {
      new google.translate.TranslateElement({ pageLanguage: 'it', autoDisplay: false },'google_translate_element');
                                         }
 </script>
 <script type="text/javascript" src="//translate.google/translate_a/element.js?cb=googleTranslateElementInit"></script>

I'm trying to change the value of the containing the languages.

<script>
window.onclick = function() {
    var container = document.getElementById('google_translate_element');
    var select = container.getElementsByTagName('select')[0];
    select.value="en";
}
</script>

This code works and the selected value changes but only that! The page is not translated. I think that I have to validate this operation! Anyone knows how I can do that?

I want to add the multi languages features on my website so I've added the google translate tool (Translate Tool) but I want to translate the website only when the user click on a button in the page and not when it uses the bo! Is there any way to change automatically the value of the bo when a button is clicked?

This is the code of the translate tools:

 <div id="google_translate_element"></div><script type="text/javascript">
      function googleTranslateElementInit() {
      new google.translate.TranslateElement({ pageLanguage: 'it', autoDisplay: false },'google_translate_element');
                                         }
 </script>
 <script type="text/javascript" src="//translate.google./translate_a/element.js?cb=googleTranslateElementInit"></script>

I'm trying to change the value of the containing the languages.

<script>
window.onclick = function() {
    var container = document.getElementById('google_translate_element');
    var select = container.getElementsByTagName('select')[0];
    select.value="en";
}
</script>

This code works and the selected value changes but only that! The page is not translated. I think that I have to validate this operation! Anyone knows how I can do that?

Share Improve this question edited Jan 9, 2014 at 10:45 Magic AndWebDev asked Jan 8, 2014 at 14:39 Magic AndWebDevMagic AndWebDev 1371 gold badge5 silver badges16 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

I created a PHP class for an open source project:

---// source code of google_translate.php //----

<?php

class google_translate{

    public $original_language;
    public $translate;
    public $domain;


    function __construct ($original_language, $translate, $domain){
        $this->original_language = $original_language;      
        $this->translate = $translate;
        $this->domain = $domain;
    }   

    function translate(){


        $url = "http://translate.googleusercontent./translate_c?act=url&depth=1&hl=" . $this->original_language;
        $url .= "&ie=UTF8&prev=_t&rurl=translate.google.&sl=" .  $this->original_language;
        $url .= "&tl=". $this->translate;
        $url .= "&u=" . $this->domain;


        echo "<script language=\"javascript\">document.location=\"$url\"</script>";             


    }


}
?>

---// source code of google_translate.php //----

This script works in bination with another script, named translate.php.

---// source code of translate.php //----

<?php


include_once("include/google_translate.php");
?>
<form method="post">
<table width="640" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><img src="images/language/choose_your_language.jpg" width="640" height="360" /></td>
  </tr>
    <tr>
    <td valign="top" align="left">
    <select name="language">
    <?php echo $default_language; ?>
        <option value="ar">Arabic</option>
        <option value="be">Belarusian</option>
        <option value="bn">Bengali</option>
        <option value="bs">Bosnian</option>
        <option value="bg">Bulgarian</option>
        <option value="ca">Catalan</option>
        <option value="zh-CN">Chinese (China)</option>
        <option value="zh-TW">Chinese (Taiwan)</option>
        <option value="hr">Croatian</option>
        <option value="cs">Czech</option>
        <option value="da">Danish</option>
        <option value="nl">Dutch</option>
        <option value="en">English</option>
        <option value="fil">Filipino</option>
        <option value="fi">Finnish</option>
        <option value="fr">French</option>
        <option value="de">German</option>
        <option value="el">Greek</option>
        <option value="gu">Gujarati</option>
        <option value="he">Hebrew</option>
        <option value="hi">Hindi</option>
        <option value="hu">Hungarian</option>
        <option value="id">Indonesian</option>
        <option value="it">Italian</option>
        <option value="ja">Japanese</option>
        <option value="kn">Kannada</option>
        <option value="ko">Korean</option>
        <option value="lv">Latvian</option>
        <option value="lt">Lithuanian</option>
       <option value="mr">Marathi</option>       
        <option value="no">Norwegian</option>
        <option value="fa">Persian</option>
        <option value="pl">Polish</option>
        <option value="pt-BR">Portuguese (Brazil)</option>
        <option value="pt-PT">Portuguese (Portugal)</option>
        <option value="ro">Romanian</option>
        <option value="ru">Russian</option>
        <option value="sr">Serbian</option>
        <option value="sk">Slovak</option>
        <option value="sl">Slovenian</option>
        <option value="es">Spanish</option>
        <option value="sv">Swedish</option>
        <option value="sw">Swahili</option>
        <option value="ta">Tamil</option>
        <option value="te">Telugu</option>
        <option value="th">Thai</option>
        <option value="tr">Turkish</option>
        <option value="uk">Ukrainian</option>
        <option value="vi">Vietnamese</option>
      </select>
    </td>   
    </tr>
    <tr>
     <td valign="top" align="left">
      <input name="submit" type="submit" value="Translate" /></td>
  </tr>
 </table>
</form>

---// source code of translate.php //----

And below you see an example of the code how it's used in Horizon QCMS:

---// how to use the code //----

<?php

include_once('translate.php');

# Google translator
# script generated with Horizon QCMS 4


if(isset($_POST['submit'])){

    $language='en';
    $translate = new google_translate('en', $_POST['language'], 'http://www.hnqcms./wiki/');
    $translate->translate();
}

---// how to use the code //----

A working example can be found here: http://www.hnqcms./wiki/index-page-28-category-28.html

P.s. You can of course adjust the script a bit so that the user doesn't have to click on the "submit" button.

I posted this answer which may help you. It uses two javascript functions to set and get the cookie 'googtrans'. If present, Google Website Translator gets its value and translates the page accordingly. The case in the answer applies to just one flag in a link to change language, but it can easily be extended.

Manually: url redirects to google translate

http://translate.google./translate?hl=en&sl=de&tl=es&u=www.google.de&sandbox=1

only works online

here code jsfiddle

otherwise

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论