Hi all, I've been looking on the net for something like this and found a few things but because I'm a beginner still, I can't put it together. I need to change the site's language based on the folder /en/ or /fr/ and the file names are always the same. The site is not big and this would work perfectly.
I have this code in asp that does it but not sure how to convert it into php: A sample site using it is http://www.contacservices.com/fr/contactus.html but on home page, it's using index.html and index_fr.html because they're not in the /en/ and /fr/ folders
<% Dim PATH_INFO PATH_INFO = Request.ServerVariables(\"PATH_INFO\") If (Instr(PATH_INFO,\"/fr/\")>0) Then 'Response.Write(\"FRENCH\") PATH_INFO =Replace( PATH_INFO , \"/fr/\", \"/en/\") ElseIf (Instr(PATH_INFO,\"/en/\")>0) Then 'Response.Write(\"ENGLSIH\") PATH_INFO =Replace( PATH_INFO , \"/en/\", \"/fr/\") End If %> <%'Response.Write(Request.ServerVariables(\"PATH_INFO\"))%> <div id=\"topslimbar\"><a href=\"<%=PATH_INFO %>\" target=\"_self\" title=\"Passer au français\">Français</a></div>
I also found this http://www.phpbuilder.com/tips/item.php?id=343 but I can't figure out why it's not working. Maybe I'm just not using it right. Can you guys please help me?
Unfortunately, this is not what I had in mind. I've seen that before but I've posted my request on another forum and will post back a link once I get the right answer. Thanks Chris!
Ooh, I've just remembered I did something like this in PHP. I will briefly try and explain it, the site is located here http://www.elephantandcastle.towntalk.co.uk There is not much text that changes, just little bits here and there as they can all speak basic English in the area.
Essentially, clicking the language changer calls the same page again but appends /spanish or /english to the address. Using .htaccess file this is passed as a get variable ?lang=spanish or english. The site then checks to see if the $_GET['lang'] has been set and if so creates a session and stores a session variable setting that as the default language. So for every other page, while it does not say the language in the address, the language is remembered and the content varied accordingly. You can still append the language if you want, it should not make any difference to the overall effect.
The url looks messy when you click on the flags and navigation back and forth. I think this confuses the user. I found another sample but also not sure how to use this one either http://www.phpbuilder.com/tips/item.php ... int_mode=1 :? Please, I hope someone answer my question the way I wrote it. I think I'm getting closer but I would love to see code samples because I'm such a newbie with PHP. I need it to do the same thing shown here http://www.contacservices.com/en/aboutus.html (when you click on the Français in the top right corner). It just changes the folder from en to fr and reloads/redirects the page. This works from any page except the home page which is because it's not within the en or fr folders. Thanks to anyone who can help!
Ok I think I understand what you mean. You have this setup: http://grab.by/2mmk and you want the php code to create a link that will switch between the en and fr directories.
EXACTLY! Thanks for clearing this up Dave. Only thing is, the include folder would be in the top root level as well as the en and fr folders. Do you have a solution for this?
Thanks for the reply again dave. But I found a solution after playing around with my original code!!! :lol: :lol: Basically converting the code I already had from the start from ASP to PHP did the trick:
I put the following code in the include folder and I named it hdr-ar.php
<?php $path_info = $_SERVER['REQUEST_URI']; if (strpos($path_info, \"/ar/\") > 0){ echo \"YOU ARE IN THE ARABIC SITE\"; $path_info = str_replace(\"/ar/\", \"/en/\", $path_info); }else if (strpos($path_info, \"/en/\") > 0){ echo \"YOU ARE IN THE ENGLISH SITE\"; $path_info = str_replace(\"/en/\", \"/ar/\", $path_info); } echo '<br />'; echo $path_info; ?> <!-- this is where the link will show --> <div id=\"langbar\"><a href=\"<?php echo $path_info; ?>\" target=\"_self\" title=\"Switch to English\">Switch to English</a></div>
You can comment the echo statements. It's just there for testing.
I put the following code in the include folder and I named it hdr-en.php
<?php $path_info = $_SERVER['REQUEST_URI']; if (strpos($path_info, \"/ar/\") > 0){ echo \"YOU ARE IN THE ARABIC SITE\"; $path_info = str_replace(\"/ar/\", \"/en/\", $path_info); }else if (strpos($path_info, \"/en/\") > 0){ echo \"YOU ARE IN THE ENGLISH SITE\"; $path_info = str_replace(\"/en/\", \"/ar/\", $path_info); } echo '<br />'; echo $path_info; ?>
*The arabic writing might show weird on this post but you get the point.
I had a problem running this using PATH_INFO which didn't really do anything. Then I used REQUEST_URI and that fixed it right away. Just include the file(s) using
<?php include(\"../include/hdr-en.php\"); ?>
and you're good to go.
It's too bad not many people answered my question properly (maybe didn't fully understand it) and I'm more amazed/sad :shock: at how little responses there were but anyways, I hope this helps someone in the future. :D By the way, this works in php5 and should be fine in php4 too.
I've been looking on the net for something like this and found a few things but because I'm a beginner still, I can't put it together.
I need to change the site's language based on the folder /en/ or /fr/ and the file names are always the same. The site is not big and this would work perfectly.
I have this code in asp that does it but not sure how to convert it into php:
A sample site using it is http://www.contacservices.com/fr/contactus.html but on home page, it's using index.html and index_fr.html because they're not in the /en/ and /fr/ folders
I also found this http://www.phpbuilder.com/tips/item.php?id=343 but I can't figure out why it's not working. Maybe I'm just not using it right.
Can you guys please help me?
http://sundaymorning.jaysalvat.com/
Essentially, clicking the language changer calls the same page again but appends /spanish or /english to the address. Using .htaccess file this is passed as a get variable ?lang=spanish or english. The site then checks to see if the $_GET['lang'] has been set and if so creates a session and stores a session variable setting that as the default language. So for every other page, while it does not say the language in the address, the language is remembered and the content varied accordingly. You can still append the language if you want, it should not make any difference to the overall effect.
Hope that makes some sense!
I found another sample but also not sure how to use this one either http://www.phpbuilder.com/tips/item.php ... int_mode=1 :?
Please, I hope someone answer my question the way I wrote it. I think I'm getting closer but I would love to see code samples because I'm such a newbie with PHP.
I need it to do the same thing shown here http://www.contacservices.com/en/aboutus.html (when you click on the Français in the top right corner). It just changes the folder from en to fr and reloads/redirects the page. This works from any page except the home page which is because it's not within the en or fr folders.
Thanks to anyone who can help!
http://grab.by/2mmk
and you want the php code to create a link that will switch between the en and fr directories.
if (preg_match('/^\/(en|fr)\/(.*)/', $_SERVER['REQUEST_URI'], $matches)) {
$cur_lang = $matches[1];
if ($cur_lang == 'en')
$alt_lang = 'fr';
else
$alt_lang = 'en';
$alt_lang_url = \"/{$alt_lang}/{$matches[2]}\";
echo \"<a href=\\"$alt_lang_url\\">$alt_lang</a>\";
}
But I found a solution after playing around with my original code!!! :lol: :lol: Basically converting the code I already had from the start from ASP to PHP did the trick:
I put the following code in the include folder and I named it hdr-ar.php
You can comment the echo statements. It's just there for testing.
I put the following code in the include folder and I named it hdr-en.php
*The arabic writing might show weird on this post but you get the point.
I had a problem running this using PATH_INFO which didn't really do anything. Then I used REQUEST_URI and that fixed it right away.
Just include the file(s) using
It's too bad not many people answered my question properly (maybe didn't fully understand it) and I'm more amazed/sad :shock: at how little responses there were but anyways, I hope this helps someone in the future. :D
By the way, this works in php5 and should be fine in php4 too.