Conditional Shortcode 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 conditinal 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';
    }
    
?>

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 on 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 between two shortcode blcoks

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

Result English vs Spanish

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