Documentation > Restrict by language > Conditional Shortcode Based on Language

Conditional Shortcode Based on Language

Browse Docs
Search Documentation

Translate all hard-to-reach or non-visible strings using the TranslatePress shortcode presented below.

How to use the TranslatePress conditional shortcode to display content based on language

Things like emails sent to users by different plugins cannot be translated using the visual translation interface because they do not appear in the front-end.

For these other types of content you can use the TranslatePress conditional shortcode to display content based on language:

[trp_language language="en_US"] English content only [/trp_language]

This can be done also with PHP:

<?php
    $current_language = get_locale();

    if( $current_language == 'en_EN' ){
      echo 'text in language en_EN';
    }
    
    if( $current_language == 'fr_FR' ){
      echo 'text in language fr_FR';
    }
    
?>

Display images based on language with the conditional shortcode

Another use case of the conditional shortcode is for translating images, basically displaying different images for different languages.

[trp_language language="en_US"] 
<‍img src="https://example.com/image_en.png" /> 
[/trp_language]
[trp_language language="fr_FR"]
<‍img src="https://example.com/image_fr.png" />
[/trp_language]

However, this can now be easily achieved directly from the front-end using the built-in image translation functionality.

It is also possible to use the conditional shortcode to display Gutenberg blocks only in the desired language.

To do this, you will have to wrap the Gutenberg block between two shortcode blocks. The first shortcode block will contain the start of our conditional shortcode ([trp_language language="en_US"]) and the second block will contain the end of our conditional shortcode ([/trp_language]).

Wrap content between two TranslatePress shortcode blocks

The result can be seen in the following images. The first page is the default English version and the second one is the Spanish version of the same page.

Result conditional shortcode EnglishResult conditional shortcode Spanish

Another alternative to the TranslatePress conditional shortcode is the String Translation interface where you can access gettext strings (such as emails) and easily translate them from a list.

TranslatePress Multilingual

The best website localization service for WordPress websites. Manage everything from a visual interface.