Google Translate

Compatible with: R6.0
Current version: 0.2
Credits: ascrsoft, WikiAdmin

Adds Google Translate to your page header.


It takes the page language and translates it into the target language. In the $languages array you can define all target languages you want to offer in the drop-down box, which must be of course available for translation.

1. Documentation

Google Translate
Wikipedia: Google Translate

1.1. Header

theme/yourtheme/appearance/header.php

<?php
 
// Google Translate
if ($this->has_access('read', '', GUEST))
{
    // custom language array
    $languages = [
        'af' => 'Afrikaans',
        'sq' => 'Albanian',
        'am' => 'Amharic',
        'ar' => 'Arabic',
        'hy' => 'Armenian',
        'az' => 'Azerbaijani',
        'eu' => 'Basque',
        'be' => 'Belarusian',
        'bn' => 'Bengali',
        'bs' => 'Bosnian',
        'bg' => 'Bulgarian',
        'ca' => 'Catalan',
        'ceb' => 'Cebuano',
        'ny' => 'Chichewa',
        'zh-CN' => 'Chinese (Simplified)',
        'zh-TW' => 'Chinese (Traditional)',
        'co' => 'Corsican',
        'hr' => 'Croatian',
        'cs' => 'Czech',
        'da' => 'Danish',
        'nl' => 'Dutch',
        'en" selected="selected' => 'English',
        'eo' => 'Esperanto',
        'et' => 'Estonian',
        'tl' => 'Filipino',
        'fi' => 'Finnish',
        'fr' => 'French',
        'fy' => 'Frisian',
        'gl' => 'Galician',
        'ka' => 'Georgian',
        'de' => 'German',
        'el' => 'Greek',
        'gu' => 'Gujarati',
        'ht' => 'Haitian Creole',
        'ha' => 'Hausa',
        'haw' => 'Hawaiian',
        'iw' => 'Hebrew',
        'hi' => 'Hindi',
        'hmn' => 'Hmong',
        'hu' => 'Hungarian',
        'is' => 'Icelandic',
        'ig' => 'Igbo',
        'id' => 'Indonesian',
        'ga' => 'Irish',
        'it' => 'Italian',
        'ja' => 'Japanese',
        'jw' => 'Javanese',
        'kn' => 'Kannada',
        'kk' => 'Kazakh',
        'km' => 'Khmer',
        'ko' => 'Korean',
        'ku' => 'Kurdish (Kurmanji)',
        'ky' => 'Kyrgyz',
        'lo' => 'Lao',
        'la' => 'Latin',
        'lv' => 'Latvian',
        'lt' => 'Lithuanian',
        'lb' => 'Luxembourgish',
        'mk' => 'Macedonian',
        'mg' => 'Malagasy',
        'ms' => 'Malay',
        'ml' => 'Malayalam',
        'mt' => 'Maltese',
        'mi' => 'Maori',
        'mr' => 'Marathi',
        'mn' => 'Mongolian',
        'my' => 'Myanmar (Burmese)',
        'ne' => 'Nepali',
        'no' => 'Norwegian',
        'ps' => 'Pashto',
        'fa' => 'Persian',
        'pl' => 'Polish',
        'pt' => 'Portuguese',
        'pa' => 'Punjabi',
        'ro' => 'Romanian',
        'ru' => 'Russian',
        'sm' => 'Samoan',
        'gd' => 'Scots Gaelic',
        'sr' => 'Serbian',
        'st' => 'Sesotho',
        'sn' => 'Shona',
        'sd' => 'Sindhi',
        'si' => 'Sinhala',
        'sk' => 'Slovak',
        'sl' => 'Slovenian',
        'so' => 'Somali',
        'es' => 'Spanish',
        'su' => 'Sundanese',
        'sw' => 'Swahili',
        'sv' => 'Swedish',
        'tg' => 'Tajik',
        'ta' => 'Tamil',
        'te' => 'Telugu',
        'th' => 'Thai',
        'tr' => 'Turkish',
        'uk' => 'Ukrainian',
        'ur' => 'Urdu',
        'uz' => 'Uzbek',
        'vi' => 'Vietnamese',
        'cy' => 'Welsh',
        'xh' => 'Xhosa',
        'yi' => 'Yiddish',
        'yo' => 'Yoruba',
        'zu' => 'Zulu',
    ];
    // or localized lang array, see lang/wacko.en.php
    #$languages = $this->_t('LanguageArray')
 
    $tpl->enter('gt_');
    $tpl->lang = $this->page['page_lang'];
    
    foreach ($languages as $lang => $language)
    {
        $tpl->o_lang        = $lang;
        $tpl->o_language    = $language;
        
        if ($lang == $this->page['page_lang'])
        {
            $tpl->o_selected = ' selected';
        }
    }
    
    $tpl->leave(); // gt_
}

1.2. Template


theme/yourtheme/appearance/template/header.tpl

<div id="login-box">
    [= gt =
        <!-- Google Translate -->
        [ ' _t: Language ' ]:
        <select id="translateCombo" onchange="translate_web(this);">
            [= o =
                <option value="[ ' lang ' ]"[ ' selected ' ]>[ ' language | e ' ]</option>
            =]
        </select>
        <script>
            var lang    = '[ ' lang | js ' ]';
            function translate_web(language)
            { 
                document.location = 'https://translate.google.com/translate?langpair='+lang+'|'+ language.value+'&sl='+lang+'&tl='+ language.value+'&u=' + escape(document.location);
            }
 
            if (top.frames.length != 0)
            {
                document.getElementById("translateCombo").disabled=true;
            }
        </script>
    =]
</div>

2. Changelog

0.1 Intitial version
0.2 adds template, uses page language as default

3. To Do

  • disable for protected pages
  • add link to google documentation
  • the script can be also placed for instance in default.js