Conditional Shortcode Based on Language

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.

Looking to go multilingual?

Get full access to TranslatePress and join 200.000+ website owners that grow their multilingual traffic.

Unlock PREMIUM Features

Or download FREE version