We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4310
    • 2,310 Posts
    For those that don’t want/need a full blown multi-lingual solution then taking advantage of Google’s Translate feature could be the answer.
    This snippet creates a set of images/links with language pair values, you decide on the base language and add as many translate option languages as you want.
    Clicking one of the images/links passes your page url to Google with the language pair, e.g. English & French.
    Google Translate then renders that page, with menus, text etc. in French.
    The whole site is then navigable whilst being rendered in French, at any time you can return to your actual site.
    See a demo at http://www.3913.co.uk.
    Here’s the snippet code, feedback with any improvements or suggestions, it’s a work in progress.
    <?php
    /*
    GoogleTransltor
    ****************
    
    Version: Beta for testing
    Created by: David Bunker (bunk58)
    Demo @ : http://www.3913.co.uk
    Created on: 10th June 2009
    
    Description
    ************
    Use Google Translate (see http://translate.google.com/translate_t?hl=en&sl=en&tl=fr#) for a quick and easy multi-lingual site.
    
    "...to instantly translate your webpage or blog into other languages..."
    
    Codes & Languages available
    ***************************
    sq => Albanian
    ar => Arabic
    bg => Bulgarian
    ca => Catalan
    zh-CN => Chinese (Simplified)
    zh-TW => Chinese (Traditional)
    hr => Croatian
    cs => Czech
    da => Danish
    nl => Dutch
    en => English
    et => Estonian
    tl => Filipino
    fi => Finnish
    fr => French
    gl => Galician
    de => German
    el => Greek
    iw => Hebrew
    hi => Hindi
    hu => Hungarian
    id => Indonesian
    it => Italian
    ja => Japanese
    ko => Korean
    lv => Latvian
    lt => Lithuanian
    mt => Maltese
    no => Norwegian
    pl => Polish
    pt => Portuguese
    ro => Romanian
    ru => Russian
    sr => Serbian
    sk => Slovak
    sl => Slovenian
    es => Spanish
    sv => Swedish
    th => Thai
    tr => Turkish
    uk => Ukrainian
    vi => Vietnamese
    
    Example snippet call
    *********************
    
    [!GoogleTranslator? &base_lang=`en` &trans_langs=`de,es,ru,ja,zh` &image_path=`assets/images/translator/` &title_tag=`Translate this page into` &alt_tag=`Flag` &image_type=`.gif` &main_tpl=`google_translator_tpl`!]
    
    Any unspecified snippet parameters will have the default value used.
    
    Example template chunk
    ***********************
    
    [+gt.open_a_tag+]
    <img src="[+gt.image_path+][+gt.lang_code+][+gt.image_type+]" alt="[+gt.alt_tag+] - [+gt.lang_full+]" />
    [+gt.close_a_tag+]
    
    If "&main_tpl" chunk is not specified in the snippet parameters, the default_tpl will be used.
    
    */
    $output = "";
    $trans_langs = isset($trans_langs) ? $trans_langs:"de,es,ja,ru,zh-TW";
    $title_tag = isset($title_tag) ? $title_tag:"Translate this page into";
    $alt_tag = isset($alt_tag) ? $alt_tag:"Flag";
    $image_path = isset($image_path) ? $image_path:"assets/images/translator/";
    $image_type = isset($image_type) ? $image_type:".gif";
    $default_tpl = '[+gt.open_a_tag+]
    <img src="[+gt.image_path+][+gt.lang_code+][+gt.image_type+]" alt="[+gt.alt_tag+] - [+gt.lang_full+]" />
    [+gt.close_a_tag+]';
    $base_lang = isset($base_lang) ? $base_lang:"en";
    if(!isset($main_tpl)){
    $template =  $default_tpl;       
    }
    else{
    $template = $modx->getChunk($main_tpl); 
    }
    $trans_langs = explode(',', $trans_langs);
    $page_id = $modx->documentIdentifier;
    $modx_url= $modx->makeUrl($page_id, '', '', 'full');
    foreach($trans_langs as $ind_lang){
    // Must be a better way of doing this next bit!
    if($ind_lang == 'de'){
    $ind_lang_full = 'German';
    }
    if($ind_lang == 'es'){
    $ind_lang_full = 'Spanish';
    }
    if($ind_lang == 'ja'){
    $ind_lang_full = 'Japanese';
    }
    if($ind_lang == 'ru'){
    $ind_lang_full = 'Russian';
    }
    if($ind_lang == 'zh-TW' || $ind_lang == 'zh-CN'){
    $ind_lang_full = 'Chinese';
    }
    //Next bit lifted from AjaxSearch by KyleJ & Coroico (Thanks!)
    $gtPlaceholders = array(
       '[+gt.open_a_tag+]' => "<a href=\"http://translate.google.com/translate?client=tmpg&hl={$base_lang}&u={$modx_url}&langpair={$base_lang}|{$ind_lang}\" title=\"{$title_tag} {$ind_lang_full}\">",
       '[+gt.close_a_tag+]' => "</a>",
       '[+gt.lang_code+]' => $ind_lang,
       '[+gt.lang_full+]' => $ind_lang_full,
       '[+gt.image_path+]' => $image_path,
       '[+gt.image_type+]' => $image_type,
       '[+gt.alt_tag+]' => $alt_tag,
       '[+gt.title_tag+]' => $title_tag,         
        );
    $output .= str_replace(array_keys($gtPlaceholders),array_values($gtPlaceholders),$template);
     }       
    return $output;
    ?>
      • 16183
      • 1,390 Posts
      David,

      Thx for sharing smiley Always good to have alternatives

      Is there a way to control it so that if I jump from say German to French, the google frame on top of the page is auto closed for the previous language? At the moment, if I view one language, then click on another language, the frames all pile up pushing the content lower and lower on the page.. (hope am clear)

      Also couldn’t figure out how to close the frames?

      /k
        • 4310
        • 2,310 Posts
        I see what you mean if you click one of the flags again yet another frame appears.
        I wouldn’t think there’s anything that can be done to stop it as it’s Google controlling the page.
        Hopefully people would soon get bored of doing that wink
          • 20413
          • 2,877 Posts
          Tnx! Very nice and USEFUL snippet!! smiley

          But Google translates
          2009 © HAW Productions, Mr. HAW CSS ...
          to
          2009 © HAGTORNSBÄR Productions, Mr HAGTORNSBÄR CSS ...
          (some kind of berry) and that is not cool! laugh
            @hawproductions | http://mrhaw.com/

            Infograph: MODX Advanced Install in 7 steps:
            http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

            Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
            http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
            • 4310
            • 2,310 Posts
            That’s because in English there is a haw berry laugh
            The translations aren’t perfect, but then again not all professional ones are that good huh
              • 20413
              • 2,877 Posts
              Lol! I didn’t know that! laugh
                @hawproductions | http://mrhaw.com/

                Infograph: MODX Advanced Install in 7 steps:
                http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

                Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
                http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                I’ve made a few modification to this; the languages array is the full array taken from Google. The link generation code was changed to a more simple foreach function using the key => value pair from the array.

                To-do:
                1. match optional languages parameter to the array keys. For now, the only way to limit the language selection is to edit the array in the snippet code.
                2. Set javascript to get rid of the frameset. You can add target=\"_blank\" to the URL to open the translation in a new tab or window.
                3. Bundle a bunch of free flag icons.
                <?php
                /*
                GoogleTranslator
                ****************
                
                Version: Beta for testing
                Created by: David Bunker (bunk58)
                Demo @ : http://www.3913.co.uk
                Created on: 10th June 2009
                
                Modified 16th July 2010
                Modified by: Susan Ottwell (sottwell)
                Changed:
                    loaded language sets from Google into an array
                    explode optional language parameter in the parameter check
                    looped through the array as key -> value to set the placeholders
                
                Description
                ************
                Use Google Translate (see http://translate.google.com/translate_t?hl=en&sl=en&tl=fr#) for a quick and easy multi-lingual site.
                
                "...to instantly translate your webpage or blog into other languages..."
                Example snippet call
                *********************
                
                [!GoogleTranslator? &base_lang=`en` &image_path=`assets/images/translator/` &title_tag=`Translate this page into` &alt_tag=`Flag` &image_type=`.gif` &main_tpl=`google_translator_tpl`!]
                
                The &trans_langs=`de,es,ru,ja,zh` parameter is non-functional in this version.
                To-do: map exploded parameter values to language array keys.
                
                Any unspecified snippet parameters will have the default value used.
                
                Example template chunk
                ***********************
                
                [+gt.open_a_tag+]
                <img src="[+gt.image_path+][+gt.lang_code+][+gt.image_type+]" alt="[+gt.alt_tag+] - [+gt.lang_full+]" />
                [+gt.close_a_tag+]
                
                If "&main_tpl" chunk is not specified in the snippet parameters, the default_tpl will be used.
                
                Add language sets to the array as Google adds languages (this list from 16 July 2010)
                */
                $lang_array = array(
                'af' => 'Afrikaans',
                'sq' => 'Albanian',
                'ar' => 'Arabic',
                'hy' => 'Armenian',
                'az' => 'Azerbaijani',
                'eu' => 'Basque',
                'be' => 'Belarusian',
                'bg' => 'Bulgarian',
                'ca' => 'Catalan',
                'zh-CN' => 'Chinese (Simplified)',
                'zh-TW' => 'Chinese (Traditional)',
                'hr' => 'Croatian',
                'cs' => 'Czech',
                'da' => 'Danish',
                'nl' => 'Dutch',
                'en' => 'English',
                'et' => 'Estonian',
                'tl' => 'Filipino',
                'fi' => 'Finnish',
                'fr' => 'French',
                'gl' => 'Galician',
                'ka' => 'Georgian',
                'de' => 'German',
                'el' => 'Greek',
                'ht' => 'Haitian Creole',
                'iw' => 'Hebrew',
                'hi' => 'Hindi',
                'hu' => 'Hungarian',
                'is' => 'Icelandic',
                'id' => 'Indonesian',
                'ga' => 'Irish',
                'it' => 'Italian',
                'ja' => 'Japanese',
                'ko' => 'Korean',
                'lv' => 'Latvian',
                'lt' => 'Lithuanian',
                'mk' => 'Macedonian',
                'ms' => 'Malay',
                'mt' => 'Maltese',
                'no' => 'Norwegian',
                'fa' => 'Persian',
                'pl' => 'Polish',
                'pt' => 'Portuguese',
                'ro' => 'Romanian',
                'ru' => 'Russian',
                'sr' => 'Serbian',
                'sk' => 'Slovak',
                'sl' => 'Slovenian',
                'es' => 'Spanish',
                'sw' => 'Swahili',
                'sv' => 'Swedish',
                'th' => 'Thai',
                'tr' => 'Turkish',
                'uk' => 'Ukrainian',
                'ur' => 'Urdu',
                'vi' => 'Vietnamese',
                'cy' => 'Welsh',
                'yi' => 'Yiddish'
                );
                $trans_langs = $lang_array;
                $title_tag = isset($title_tag) ? $title_tag:"Translate this page into";
                $alt_tag = isset($alt_tag) ? $alt_tag:"Flag";
                $image_path = isset($image_path) ? $image_path:"assets/images/translator/";
                $image_type = isset($image_type) ? $image_type:".gif";
                $default_tpl = '[+gt.open_a_tag+]
                <img src="[+gt.image_path+][+gt.lang_code+][+gt.image_type+]" alt="[+gt.alt_tag+] - [+gt.lang_full+]" />
                [+gt.close_a_tag+]';
                $base_lang = isset($base_lang) ? $base_lang:"en";
                if(!isset($main_tpl)){
                $template =  $default_tpl;       
                }
                else{
                $template = $modx->getChunk($main_tpl); 
                }
                $page_id = $modx->documentIdentifier;
                $modx_url= $modx->makeUrl($page_id, '', '', 'full');
                $output = "";
                foreach($trans_langs as $ind_lang => $ind_lang_full){
                //Next bit lifted from AjaxSearch by KyleJ & Coroico (Thanks!)
                    $gtPlaceholders = array(
                       '[+gt.open_a_tag+]' => "<a href=\"http://translate.google.com/translate?client=tmpg&hl={$base_lang}&u={$modx_url}&langpair={$base_lang}|{$ind_lang}\" title=\"{$title_tag} {$ind_lang_full}\">",
                       '[+gt.close_a_tag+]' => "</a>",
                       '[+gt.lang_code+]' => $ind_lang,
                       '[+gt.lang_full+]' => $ind_lang_full,
                       '[+gt.image_path+]' => $image_path,
                       '[+gt.image_type+]' => $image_type,
                       '[+gt.alt_tag+]' => $alt_tag,
                       '[+gt.title_tag+]' => $title_tag,         
                    );
                    $output .= str_replace(array_keys($gtPlaceholders),array_values($gtPlaceholders),$template);
                     }    
                return $output;
                ?>


                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 4310
                  • 2,310 Posts
                  smiley
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Fixed the parameter bit; exploded the parameter value, used array_fill_keys to create an array with these values as keys (the values in this array are irrelevant), then used array_intersect_keys() to pull out the matching elements from the main languages array. Fun!

                    http://www.php.net/manual/en/function.array-fill-keys.php
                    http://www.php.net/manual/en/function.array-intersect-key.php

                    <?php
                    /*
                    GoogleTranslator
                    ****************
                    
                    Version: Beta for testing
                    Created by: David Bunker (bunk58)
                    Demo @ : http://www.3913.co.uk
                    Created on: 10th June 2009
                    
                    Modified 16th July 2010
                    Modified by: Susan Ottwell (sottwell)
                    Changed:
                        loaded language sets from Google into an array
                        explode optional language parameter in the parameter check
                        looped through the array as key -> value to set the placeholders
                    
                    Description
                    ************
                    Use Google Translate (see http://translate.google.com/translate_t?hl=en&sl=en&tl=fr#) for a quick and easy multi-lingual site.
                    
                    "...to instantly translate your webpage or blog into other languages..."
                    Example snippet call
                    *********************
                    
                    [!GoogleTranslator? &base_lang=`en` &trans_langs=`de,es,ru,ja,zh` &image_path=`assets/images/translator/` &title_tag=`Translate this page into` &alt_tag=`Flag` &image_type=`.gif` &main_tpl=`google_translator_tpl`!]
                    
                    Any unspecified snippet parameters will have the default value used.
                    
                    Example template chunk
                    ***********************
                    
                    [+gt.open_a_tag+]
                    <img src="[+gt.image_path+][+gt.lang_code+][+gt.image_type+]" alt="[+gt.alt_tag+] - [+gt.lang_full+]" />
                    [+gt.close_a_tag+]
                    
                    If "&main_tpl" chunk is not specified in the snippet parameters, the default_tpl will be used.
                    
                    Add language sets to the array as Google adds languages (this list from 16 July 2010)
                    */
                    $lang_array = array(
                    'af' => 'Afrikaans',
                    'sq' => 'Albanian',
                    'ar' => 'Arabic',
                    'hy' => 'Armenian',
                    'az' => 'Azerbaijani',
                    'eu' => 'Basque',
                    'be' => 'Belarusian',
                    'bg' => 'Bulgarian',
                    'ca' => 'Catalan',
                    'zh-CN' => 'Chinese (Simplified)',
                    'zh-TW' => 'Chinese (Traditional)',
                    'hr' => 'Croatian',
                    'cs' => 'Czech',
                    'da' => 'Danish',
                    'nl' => 'Dutch',
                    'en' => 'English',
                    'et' => 'Estonian',
                    'tl' => 'Filipino',
                    'fi' => 'Finnish',
                    'fr' => 'French',
                    'gl' => 'Galician',
                    'ka' => 'Georgian',
                    'de' => 'German',
                    'el' => 'Greek',
                    'ht' => 'Haitian Creole',
                    'iw' => 'Hebrew',
                    'hi' => 'Hindi',
                    'hu' => 'Hungarian',
                    'is' => 'Icelandic',
                    'id' => 'Indonesian',
                    'ga' => 'Irish',
                    'it' => 'Italian',
                    'ja' => 'Japanese',
                    'ko' => 'Korean',
                    'lv' => 'Latvian',
                    'lt' => 'Lithuanian',
                    'mk' => 'Macedonian',
                    'ms' => 'Malay',
                    'mt' => 'Maltese',
                    'no' => 'Norwegian',
                    'fa' => 'Persian',
                    'pl' => 'Polish',
                    'pt' => 'Portuguese',
                    'ro' => 'Romanian',
                    'ru' => 'Russian',
                    'sr' => 'Serbian',
                    'sk' => 'Slovak',
                    'sl' => 'Slovenian',
                    'es' => 'Spanish',
                    'sw' => 'Swahili',
                    'sv' => 'Swedish',
                    'th' => 'Thai',
                    'tr' => 'Turkish',
                    'uk' => 'Ukrainian',
                    'ur' => 'Urdu',
                    'vi' => 'Vietnamese',
                    'cy' => 'Welsh',
                    'yi' => 'Yiddish'
                    );
                    if(isset($trans_langs)) {
                        // convert comma-delimited parameter string to a simple array
                        $langs = explode(',', $trans_langs);
                        // create new associative array with $langs values as its key
                        $lang_arr = array_fill_keys($langs, 'lang');
                        // pull out the elements with the same key from $lang_array
                        $trans_langs = array_intersect_key($lang_array, $lang_arr);
                    }
                    // if no &trans_langs parameter was set, go with the whole enchilada
                    $trans_langs = isset($trans_langs) ? $trans_langs : $lang_array;
                    $title_tag = isset($title_tag) ? $title_tag:"Translate this page into";
                    $alt_tag = isset($alt_tag) ? $alt_tag:"Flag";
                    $image_path = isset($image_path) ? $image_path:"assets/images/translator/";
                    $image_type = isset($image_type) ? $image_type:".gif";
                    $default_tpl = '[+gt.open_a_tag+]
                    <img src="[+gt.image_path+][+gt.lang_code+][+gt.image_type+]" alt="[+gt.alt_tag+] - [+gt.lang_full+]" />
                    [+gt.close_a_tag+]';
                    $base_lang = isset($base_lang) ? $base_lang:"en";
                    if(!isset($main_tpl)){
                    $template =  $default_tpl;       
                    }
                    else{
                    $template = $modx->getChunk($main_tpl); 
                    }
                    $page_id = $modx->documentIdentifier;
                    $modx_url= $modx->makeUrl($page_id, '', '', 'full');
                    $output = "";
                    foreach($trans_langs as $ind_lang => $ind_lang_full){
                    //Next bit lifted from AjaxSearch by KyleJ & Coroico (Thanks!)
                        $gtPlaceholders = array(
                           '[+gt.open_a_tag+]' => "<a target =\"_blank\" href=\"http://translate.google.com/translate?client=tmpg&hl={$base_lang}&u={$modx_url}&langpair={$base_lang}|{$ind_lang}\" title=\"{$title_tag} {$ind_lang_full}\">",
                           '[+gt.close_a_tag+]' => "</a>",
                           '[+gt.lang_code+]' => $ind_lang,
                           '[+gt.lang_full+]' => $ind_lang_full,
                           '[+gt.image_path+]' => $image_path,
                           '[+gt.image_type+]' => $image_type,
                           '[+gt.alt_tag+]' => $alt_tag,
                           '[+gt.title_tag+]' => $title_tag,         
                        );
                        $output .= str_replace(array_keys($gtPlaceholders),array_values($gtPlaceholders),$template);
                         }    
                    return $output;
                    ?>
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org