- This topic is empty.
-
AuthorPosts
-
May 16, 2010 at 2:20 am #29042Historical Forums UserParticipant
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!
October 12, 2012 at 6:55 am #111727ryan_whitlieMemberI 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
March 27, 2017 at 1:42 am #253196rubakaParticipantI 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–
March 27, 2017 at 1:43 am #253197rubakaParticipant<?php if(ICL_LANGUAGE_CODE=='en'): ?> ---do something--- <?php elseif(ICL_LANGUAGE_CODE=='it'): ?> ---do something else-- <?php endif; ?>
June 30, 2018 at 2:05 pm #273443AtelierbramParticipantThe 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 inPHP
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'"
July 2, 2018 at 3:58 am #273547tivnetParticipant“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
July 2, 2018 at 11:38 am #273559AtelierbramParticipantCool, 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.July 3, 2018 at 12:02 am #273560AtelierbramParticipant@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; ?>"
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.