Forum Moderators: coopster
I would like to write a function so that the value for href in the language links is created from the URL of the page and the respective language 'directory':
URL = [mysite.com...]
(rewritten from /template.php?page=somepage)
Language Link on Page =
<a href="en/somepage.htm">English</a>
The idea being that the php function 'writes' the 'en/somepage.htm' based on the url of the page.
For the link to the other language pages I could use:
function url_en ()
function url_de ()
function url_es ()
I've looked at explode/implode and parse_url, but am not sure that I am on the right track - i.e. is there a 'right' way to do this?
if you explode the $PATH_INFO into an array, which in your case is /en/somepage.htm
$array = explode("/",$PATH_INFO);
you get:
$array[1] = 'en'
$array[2] = 'somepage.htm'
($array[0] is always empty - i.e. the content to the left of the first slash)
you could then write a switch clause depending on the content of $array[1] to display the appropriate page
switch ($array[1]) {
case 'en':
load the english page;
break;
case 'de':
load the german page;
break;
case 'es':
load the spanish page
break;
}
then replace the 'load the page sentence' with the instructions you need to load the appropriate page.
that's if i've understood you correctly ;-)
good luck
What you wrote is very cool, I like it, and I've learnt from it; however, I'm not sure it will work for me, let me try and explain, 'cause maybe I've misunderstood something!
What I would like is a function that automatically takes the url of the page and ads in the appopriate extra directory before the somepage.htm. So the code may look like this:
<div id="extras">
<a href="<? php function es()?>" title="Espanol">Espanol</a> ¦
<a href="<? php function de()?>" title="Deutsh">Deutsh</a></div>
That code would reside in my template file, so that with any include it would automatically generate the correct URL when it sends out the page.
I'm wondering, could it be this then...
function de() {
$array = explode("/",$PATH_INFO);
echo "de/$array[2]";
}
The only drawback would be that the main site is spanish and resides at the root level, so there is no /es/. It could therefore be:
function es() {
$array = explode("/",$PATH_INFO);
echo "/$array[1];
} Not sure if I have that all right. Is it okay to use 'echo' there, or is that bad practise?
I really need to get out and study PHP some more.