View Full Version : Choose your language links
the black mambo
04-08-2008, 08:52 PM
hi everyone,
i'm working on an international website & am looking for a way to have a "choose your language" link on every page which would load the same page that you're currently on in the chosen language. i've worked out language selection on the home page w/ a cookie set based on this post -
http://www.dynamicdrive.com/forums/showthread.php?t=16544&highlight=%22language+selection%22&page=4
the english site is in the root & the translated sites are in subdirectories.
examples
mysite.com/index.php (english home page)
mysite.com/fr/index.php (french home page)
is there a script that would look at the page you're currently on & jump to the subdirectory & load that current page?
so if you're at, say,
mysite.com/contact.php
& click the link to show Spanish, it would jump you to
mysite.com/sp/contact.php
I'm looking for a way to do this dynamically & not have to manually add the links to every page. please let me know if you know of any options. i'm more of a designer & not much of a developer, but do know some basic js & php.
THANKS!
-bm
hmsnacker123
04-09-2008, 03:01 PM
hey, bm i had to change the same task once and what i done is:
i would put a link to the directory first of all:
<a href="fr/index.htm">Francais</a>
Next i would look for a free tool on the internet that if you put text into it it translates it. this helped me reply.
also to add the link to every page you can use a .dwt (dynamic web template)
the black mambo
04-09-2008, 04:05 PM
hi hmsnacker123,
thanks for getting back! the translation of the site is already complete. I've been using php includes for the header & footer.
I'm looking for a way to add an include to every page that would redirect the user to the appropriate translated page - w/out adding the links manually to every page.
djr33
04-09-2008, 09:34 PM
Hmm. Interesting question.
There are certainly a few way this can be done.
One is writing the current url into the link, through PHP. Just use $_REQUEST['REQUEST_URI'] and print that, using url_encode() as a variable on the change language link-- then just use changelang.php?p=THATURI, and then just redirect to that URI.
You could also store their last page through a session, cookie, or directly on the server, or you could rely upon the $_REQUEST['REFERER'] (page from which the request was sent), but that isn't always reliable, and the first few methods might be a little difficult (like using a database, etc.).
I'm pondering using <base href=""> for something, and setting that as "fr", etc.
However, I think the best approach would be to use PHP. In fact, don't setup different directories. Use PHP to generate each page. Store the user's language (Sessions), then just click the link and it will update the language ("?lang=fr") and redirect to the current page, just also add french as the language; the same page, then, once that session variable is set will be in french from the PHP.
In other words, check the language and include that portion of the page in that language. It will make your work easier too. Consider using a database, or maybe just some orderly directories for all the languages.
This also is helpful because you can have an incomplete language-- it can default to English.
the black mambo
04-10-2008, 02:16 PM
hi djr33,
thanks so much for your insight. the site has been setup w/ english in the root & fr and sp in subdirectories. I've also setup subdirectory domains for those folders - fr.mysite.com, es.mysite.com. I think it would be too much of a mess to move all of the files out of the subdirectories at this point.
would you be so kind as to give me some code examples of how to integrate your solutions?
One is writing the current url into the link, through PHP. Just use $_REQUEST['REQUEST_URI'] and print that, using url_encode() as a variable on the change language link-- then just use changelang.php?p=THATURI, and then just redirect to that URI.
However, I think the best approach would be to use PHP. In fact, don't setup different directories. Use PHP to generate each page. Store the user's language (Sessions), then just click the link and it will update the language ("?lang=fr") and redirect to the current page, just also add french as the language; the same page, then, once that session variable is set will be in french from the PHP.
THANKS!!!!!!!
the black mambo
04-10-2008, 04:34 PM
i'm wondering if there's a way to just rewrite the URL using php - like if the user is on an English page (www.mysite.com/info.php) & clicks the SPANISH link, the URL is appended, removing www & replacing that w/ es - so www.mysite.com/info.php would change & refresh to es.mysite.com/info.php. does that make sense?
thanks again!!!!:)
djr33
04-11-2008, 10:17 PM
It's possible to setup URL rewriting on your system, but that's not something done with PHP. Part of the server configuration.
Basic information here:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
I haven't used it, so I can't suggest more.
The first idea I wrote about was to pass the current URL as a parameter in the link. So each link on the page would have the current address attached and the changelanguage page would redirect to the URL it was sent. Look up using $_GET variables in PHP.
The other option is a bit more complex but would save you time in the end. Basically, setup a templating system.
Create a session for the user (look up PHP sessions for this-- a bit complex, but useful), and start with the default language (English?).
If they click any of the other languages, change that $_SESSION['lang'] variable.
Now, whenever you serve any page, it can check. For example:
<?php include($_SESSION['lang'].'/index.php'); ?>
That will include "en/index.php" if you are set to "en" for English.
Etc.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.