Documentazione > Restringi per lingua > Codice di scorciatoia condizionale basato sulla lingua

Codice di scorciatoia condizionale basato sulla lingua

Sfoglia documenti
Cerca Documentazione

Quando si crea un sito web multilingue, a volte potresti aver bisogno di un maggiore controllo su quali contenuti appaiono nelle diverse lingue. Ad esempio, potresti voler mostrare una promozione, visualizzare testi legali diversi a seconda della lingua o nascondere completamente contenuti in traduzioni specifiche.

TranslatePress rende tutto questo possibile grazie a shortcode dedicati che ti consentono di includi o escludere contenuti basati sulla lingua attiva. 

Come utilizzare gli shortcode condizionali TranslatePress per visualizzare contenuti in base alla lingua

Ci sono due shortcode disponibili per limitare i contenuti in base alla lingua:

  • [language-include] – displays content only in the selected languages.
[language-include lang="fr_FR"] Bonjour [/language-include]
  • [language-exclude] – hides content in the selected languages.
[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 also be done 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';
    }

?>

[language-include]

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:

  • lang A comma-separated list of language codes where the content should be displayed.
    Example: lang=”en_US, fr_FR, es_ES”

The language codes can be found in General tab, within the Code column associated with each language.

[language-exclude]

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:

  • lang A comma-separated list of language codes where the content should be hidden.
    Example: lang=”en_US, es_ES”

The language codes can be found in General tab, within the Code column associated with each language.

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]).

Il risultato è visibile nelle seguenti immagini. La prima pagina è la versione inglese predefinita e la seconda è la versione spagnola della stessa pagina.

Mostra video in base alla lingua

Puoi usare gli shortcode condizionali per visualizzare contenuti video diversi per ogni lingua sul tuo sito web.

Per fare ciò, è sufficiente racchiudere ciascun blocco video tra due blocchi shortcode per la lingua di destinazione. Il primo blocco di shortcode conterrà l'inizio del nostro shortcode condizionale (es. ).

I risultati possono essere visti nelle seguenti immagini. La prima pagina visualizza il video nella versione inglese predefinita e la seconda pagina visualizza il video nella versione spagnola della stessa pagina.

[trp_language] shortcode (!DEPRECATED)

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] e [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.

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 traduzione di immagini functionality.

It is also possible to use the conditional shortcode to display Blocchi di Gutenberg 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]).

Il risultato è visibile nelle seguenti immagini. La prima pagina è la versione inglese predefinita e la seconda è la versione spagnola della stessa pagina.

Un'altra alternativa allo shortcode condizionale TranslatePress è il Traduzione stringa interfaccia in cui è possibile accedere alle stringhe gettext (come le email) e tradurle facilmente da un elenco.

TranslatePress Multilingue

Il miglior servizio di localizzazione di siti web per siti web WordPress. Gestisci tutto da un'interfaccia visiva.