When building a multilingual website, you may sometimes need more control over what content appears in different languages. For example, you might want to show a promotion, display different legal text depending on language, or completely hide content in specific translations.
TranslatePress makes this possible using dedicated shortcodes that let you include or exclude content based on the active language.
There are two shortcodes available for restricting content by language:
[language-include lang="fr_FR"] Bonjour [/language-include]
[language-exclude lang="fr_FR"] Hello [/language-exclude]
These shortcodes give you full control over when and where specific content should appear, and whether it should be translatable or displayed exactly as written.
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'; } ?>
The [language-include] shortcode displays the content inside it only for the languages you specify.
[language-include lang="en_US, fr_FR, es_ES"] Your content here [/language-include]
Attributes:
The [language-exclude] shortcode hides the content inside it for the specified languages.
[language-exclude lang="en_US, es_ES"] Your content here [/language-exclude]
Attributes:
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 (eg. [language-include]) and the second block will contain the end of our conditional shortcode (eg. [/language-include]).
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.
The [trp_language] shortcode was originally used to display content only for a specific language. For example:
[trp_language language=”en_US”] English content only [/trp_language]
Starting with TranslatePress version 2.9.21, this shortcode is considered deprecated. While it will continue to work and will not be removed, we encourage you to use the new [language-include] and [language-exclude] shortcodes instead.
The new shortcodes provide more flexibility, give you control over whether the content should be translatable or not, and are the recommended way to manage conditional content going forward.
If you are already using [trp_language], there is no need to update your site immediately. Everything will still function as before, but for future projects we suggest switching to the new shortcodes.
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]).
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.
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.