Google Language Translator Shortcodes

Below, you’ll find a detailed guide on how to add the Google Language Translator Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Google Language Translator Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Google Language Translator Plugin and the shortcodes it provides:

Plugin Icon
Translate WordPress – Google Language Translator

"Translate WordPress – Google Language Translator is a powerful plugin that enables seamless translation of your WordPress site into multiple languages, enhancing global usability."

★★★★☆ (531) Active Installs: 100000+ Tested with: 6.1.4 PHP Version: false
Included Shortcodes:
  • [google-translator]
  • [glt]

Google Language Translator [google-translator] Shortcode

The Google Language Translator shortcode is a versatile tool for website localization. It enables the display of the Google translator in either a vertical or horizontal layout. This shortcode checks the display option set in the WordPress backend. If ‘Vertical’ or ‘SIMPLE’ is selected, it calls the function to display the translator vertically. If ‘Horizontal’ is chosen, it displays the translator horizontally.

Shortcode: [google-translator]

Examples and Usage

Basic example – A simple usage of the ‘google-translator’ shortcode to display the translator on your website.

[google-translator]

PHP Function Code

In case you have difficulties debugging what causing issues with [google-translator] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'google-translator',array(&$this, 'google_translator_shortcode'));

Shortcode PHP function:

function google_translator_shortcode() {

    if (get_option('googlelanguagetranslator_display')=='Vertical' || get_option('googlelanguagetranslator_display')=='SIMPLE'){
        return $this->googlelanguagetranslator_vertical();
    }
    elseif(get_option('googlelanguagetranslator_display')=='Horizontal'){
        return $this->googlelanguagetranslator_horizontal();
    }
  }

Code file location:

google-language-translator/google-language-translator/google-language-translator.php

Google Language Translator [glt] Shortcode

The Google Language Translator (GLT) shortcode allows the customization of language options on your website. It controls the display of languages, labels, and flags. The ‘glt’ shortcode sets the default language to Spanish. It also enables the display of a language label and defines its size. The label can be text or an image. The shortcode also adjusts the flag displayed based on user settings. For example, if the language is English, the flag can be set to Canadian or US. Similarly, for Spanish, the flag can be Mexican, and for Portuguese, it can be Brazilian. Lastly, the shortcode modifies the website’s URL based on the chosen language, improving SEO.

Shortcode: [glt]

Parameters

Here is a list of all possible glt shortcode parameters and attributes:

  • language – sets the language for the translator
  • label – sets the text label for the language choice
  • image – decides if a flag image will be shown or not
  • text – decides if the language label will be displayed or not
  • image_size – sets the size of the flag image

Examples and Usage

Basic example – The shortcode will generate a link to translate the website into Spanish. The link will be labeled in Spanish and will not display an image.

[glt language="Spanish" label="Español" image="no" text="yes"]

Advanced examples

Using the shortcode to generate a link to translate the website into French. The link will be labeled in French and will display an image with a size of 24 pixels.

[glt language="French" label="Français" image="yes" text="yes" image_size="24"]

Using the shortcode to generate a link to translate the website into Italian. The link will not be labeled and will display an image with a size of 32 pixels.

[glt language="Italian" label="" image="yes" text="no" image_size="32"]

Using the shortcode to generate a link to translate the website into German. The link will be labeled in German and will not display an image.

[glt language="German" label="Deutsch" image="no" text="yes"]

PHP Function Code

In case you have difficulties debugging what causing issues with [glt] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'glt', array(&$this, 'google_translator_menu_language'));

Shortcode PHP function:

function google_translator_menu_language($atts, $content = '') {
    extract(shortcode_atts(array(
      "language" => 'Spanish',
      "label" => 'Spanish',
      "image" => 'no',
      "text" => 'yes',
      "image_size" => '24',
      "label" => html_entity_decode('Español')
    ), $atts));

    $glt_url_structure = get_option('googlelanguagetranslator_url_structure');
    $glt_seo_active = get_option('googlelanguagetranslator_seo_active');
    $default_language = get_option('googlelanguagetranslator_language');
    $english_flag_choice = get_option('googlelanguagetranslator_english_flag_choice');
    $spanish_flag_choice = get_option('googlelanguagetranslator_spanish_flag_choice');
    $portuguese_flag_choice = get_option('googlelanguagetranslator_portuguese_flag_choice');
    $language_code = array_search($language,$this->languages_array);
    $language_name = $language;
    $language_name_flag = $language_name;

    if ( $language_name == 'English' && $english_flag_choice == 'canadian_flag') {
      $language_name_flag = 'canada';
    }
    if ( $language_name == "English" && $english_flag_choice == 'us_flag') {
          $language_name_flag = 'united-states';
    }
    if ( $language_name == 'Spanish' && $spanish_flag_choice == 'mexican_flag') {
      $language_name_flag = 'mexico';
    }
    if ( $language_name == 'Portuguese' && $portuguese_flag_choice == 'brazilian_flag') {
      $language_name_flag = 'brazil';
    }

    $href = '#';
    if($glt_seo_active == '1') {
        $current_url = network_home_url(add_query_arg(null, null));
        switch($glt_url_structure) {
            case 'sub_directory':
                $href = ($language_code == $default_language) ? $current_url : '/' . $language_code . $_SERVER['REQUEST_URI'];
                break;
            case 'sub_domain':
                $domain = str_replace('www.', '', $_SERVER['HTTP_HOST']);
                $href = ($language_code == $default_language) ? $current_url : str_ireplace('://' . $_SERVER['HTTP_HOST'], '://' . $language_code . '.' . $domain, $current_url);
                break;
            default:
                break;
        }
    }

    return "<a href='".esc_url($href)."' class='nturl notranslate ".esc_attr($language_code)." ".esc_attr($language_name_flag)." single-language flag' title='".esc_attr($language)."'>".($image=='yes' ? "<span class='flag size".esc_attr($image_size)."'></span>" : '') .($text=='yes' ? htmlspecialchars($label) : '')."</a>";
  }

Code file location:

google-language-translator/google-language-translator/google-language-translator.php

Conclusion

Now that you’ve learned how to embed the Google Language Translator Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *