Php alternate several body attribute from a link [solved]

In another thread I asked for a js solution to my problem with a multilingual website. I guess, adding something like ?set=it or ?set=en at the end of the link.
I have not many pages with three languages (Italia, Spanish, English) in the the same webpage, with this code

html

<span lang='it'>Pubblicazioni</span><span lang='es'>Publicaciones</span><span lang='en'>Publications</span>

css

body[lang=it] *[lang]:not([lang="it"]){display:none;}
body[lang=en] *[lang]:not([lang="en"]){display:none;}
body[lang=es] *[lang]:not([lang="es"]){display:none;}

js

I can add that with js I can change (temporarily) the body webpage language.
This is the js code:


//multilingual
function setEn() {
  document.body.setAttribute("lang", "en"); 
}
function setIt() {
  document.body.setAttribute("lang", "it"); 
}
function setEs() {
  document.body.setAttribute("lang", "es"); 
}
//-->

Function possibly called in every webpage with a link (<span onclick="setIt()">ita</span><span onclick="setEs()">esp</span><span onclick="setEn()">eng</span>).
And this was why I tried before with js (setting from a link in an external website page), as I said. But unsuccessfully (at least, so far).

php

Every page has a php variable as body lang:

<body lang="<?php if(empty($lang)) {echo "it";} else {echo"$lang";} ?>" >

and so far my links (from a webpage to another of my website) go to a small php (such as members-es.php), that contains only the lang variable and the include the matching complete webpage (with <span> multilingual as above, in this case members.php).

my question

My question is: it would be possible to avoid this two-files system, and use something like this (in Example 3) to open the (only, at this point) target webpage setting its body lang from the link (<a href>) in the starting webpage of my website?

Maybe using match function?

1 Like

The php $_SERVER[‘HTTP_ACCEPT_LANGUAGE’] variable will give you the browser’s language setting. You can then use this when building the html document.

The js solutions you are attempting are getting the lang attribute from the html document, which is coming from what your server is outputting. This is too late in the process.

2 Likes

I think that I have found a solution (with this help): with GET:

1st page

<p><a href="aaa.2.php?varname=en">eng</a><br />
<a href="aaa.2.php?varname=it">ita</a><br />
<a href="aaa.2.php?varname=es">esp</a></p>

2nd page

<?php
$lang = $_GET['varname'];
?>
<html>
 <body lang="<?php if(empty($lang)) {echo "it";} else {echo"$lang";} ?>">
<p>some words</p>
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.