Forum Moderators: coopster

Message Too Old, No Replies

Best/simplest method for language link?

Site in two languages

         

encyclo

3:18 pm on Apr 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was wondering - I have a site which has identical content and filenames, with an English-language version in a directory called "/en/" and a French version in "/fr/". What I want to do is have a link on the menu which switches to the equivalent page in the other language. The en and fr directories are not necessarily at root level. For example, if I have the page:

example.com/subdir/en/widgets.php

I would like to generate a link on that page to:

example.com/subdir/fr/widgets.php

What would be the simplest way of going about this?

Many thanks for your help!

jatar_k

4:14 pm on Apr 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about echo'ing the link something like this

$whereami = $_SERVER['PHP_SELF']; 
if (strpos($whereami,'/en/') === false) {
$reppath = "/en/";
$oldpath = "/fr/";
} else {
$reppath = "/fr/";
$oldpath = "/en/";
}
str_replace($reppath,$oldpath,$whereami);
echo "<a href=\"$whereami\">Switch Language</a>";

I think that's right, it's early here and I am un caffeinated. ;)

DrDoc

4:40 pm on Apr 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may want to include the slashes...

$whereami = $_SERVER['PHP_SELF'];
if (strpos($whereami,'/en/') === false) {
$reppath = "/\/en\//";
$oldpath = "/\/fr\//";
} else {
$reppath = "/\/fr\//";
$oldpath = "/\/en\//";
}

str_replace($reppath,$oldpath,$whereami);

echo "<a href=\"$whereami\">" . ($reppath=="/\/fr\//"?"Changez La Langue":"Switch Language") . "</a>";

encyclo

11:22 pm on Apr 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks jatar_k and DrDoc for the replies! I spotted (eventually) your deliberate mistake where:

str_replace($reppath,$oldpath,$whereami);

should read:

str_replace($oldpath,$reppath,$whereami);

Escaping the slashes didn't seem to work when I tried it, unless they were escaped in the original string...

Of course, I see this as your way of forcing me to understand what I'm doing rather than copying and pasting (you succeeded)!

I'm still waiting for the day whan I can answer one of the questions in this forum, but I'm getting better ;)

jatar_k

11:30 pm on Apr 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sry about that but I did warn you I was under caffeinated. ;)

I also don't test my snippets as they aren't my projects, I have to leave some fun in it. Sometimes they work as is or sometimes they don't.

Glad you figured it out. :)

encyclo

11:37 pm on Apr 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey, like I said, it's better that way! If I cut and paste and it works, I don't learn much! As for the caffeine, I gave that up years ago - I now can only work with a clear head!

encyclo