Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End [php] html "lang" attribute if statement

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #29042

    So I am using wordpress with a multilingual plugin. In the english and spanish it gives the html tag an attribute of lang.

    So if its an english page it would look like this:
    <html lang="en">

    And if its a spanish page it would look like this:
    <html lang="es">

    So in my header I want to add a php conditional statement that looks something like this:
    <?php if(lang="es") {
    echo ‘[english html code]’;
    } else {
    echo ‘[spanish html code]’;
    }

    Of course that is wrong but how would I so this correctly? Thanks!

    #111727
    ryan_whitlie
    Member

    I tried searching for this too but there doesn’t seem to be a way to detect the actual website lang attribute, only that of the browser which is no help.

    If you’re using the qTranslate plugin, or another one, look into the code and see how they detect the selected language. Might be a cookie or session. Then you will have a starting point

    #253196
    rubaka
    Participant

    I found another solution using JavaScript:

    var lang = document.documentElement.lang
    if (lang == ‘en-US’) {
    document.getElementById(“footertext3”).innerHTML = “Here you can find us! We are expecting you!”;
    }

    Something like this! Will replace text inside specified div. This is for using inside WP Page, if you try to modify header.php you can use this:

    —do something—

    —do something else–

    #253197
    rubaka
    Participant
    <?php if(ICL_LANGUAGE_CODE=='en'): ?>
    ---do something---
    <?php elseif(ICL_LANGUAGE_CODE=='it'): ?>
    ---do something else--
    <?php endif; ?>
    
    #273443
    Atelierbram
    Participant

    The WPGlobus plugin changes the url of the second language. So in the OP’s case with the default language being English and the second being Spanish, in the Spanish version of a page or post there would be es somewhere in the url, place depending on the permalink structure. Now in PHP one can take advantage of this like this:

    // set variables:
     $reqUrl = $_SERVER["REQUEST_URI"];
     $isSpanish = strpos($reqUrl, 'es'); 
    

    echo it out in header.php on the html language attribute:

    html lang="php if ($isSpanish!==false) echo 'es'; else echo 'en'"

    #273547
    tivnet
    Participant

    @Atelierbram

    “es” can appear in any part of the URL.

    WPGLobus provides a method to get the current language.

    https://stackoverflow.com/questions/39961013/how-to-get-language-using-wp-globus

    #273559
    Atelierbram
    Participant

    Cool, I knew about this but thought this method sturdier, for it will still work even if the WPGlobus plugin makes changes in their API. But thanks anyway.

    Also PHP strpos looks at all the parts of the url for a match: very usefull.

    #273560
    Atelierbram
    Participant

    @tivnet On second thought, echoing out the variable with the WPGLobus Config method is a lot cleaner, so I may opt for that.

      $lang = WPGlobus::Config()->language;
      html lang="<?= $lang; ?>"
    
Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘Back End’ is closed to new topics and replies.