Results 1 to 9 of 9

Thread: Switching language pages in PHP - help, please

  1. #1
    Join Date
    Feb 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switching language pages in PHP - help, please

    Hi, everyone,

    I've been searching for this for a while now but am coming up dry. I'm looking for a language switch script that allows the following:

    If a user is on http://www.mysite.com/en/page.php and clicks the 'French' link in the nav menu, it switches them automatically to the French version of that specific page: http://www.mysite.com/fr/page.php (and vice versa).

    Any assistance would be very much appreciated!

    Cheers.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    This is a complex question.

    There are a few parts:

    1. You need to translate all the pages.

    2. You need to have a way to transfer people (and save the language setting, if you want).

    3. You need to have copies of all the pages in each language.



    1. Translation:
    This is ALWAYS best done by a person, not a machine. The easiest way to deal with this is to recode your pages so they work with sections/phrases and translate each section individually, like "hello" is stored somewhere as "bonjour" so you can replace it later.
    If you can translate it yourself, great. If not, see if you can find some help. If you have a big enough/popular site, then you can get visitors who speak a language to help translate it. That's how the big sites do it (like facebook, for example).
    The alternate method here is to use a machine translator and then save the content (like google translate), which is always a bad idea because there will ALWAYS be errors, and might actually be harder to read than just making the visitor view it in English-- after all they can always cut/paste the page into a translator themselves... that doesn't help much.
    You could also look for a setup for your server that will automatically translate (like running a copy of google translate on your server, if that code was available), but it will be hard to find a reasonable translator, at least for free. And again, it won't turn out that well.

    2. Language choice/links:
    There are many ways to go about this. The easiest way is to store a variable somewhere (cookie, database, session data), and use that to load the pages. You don't even need /fr/ or /en/, because you could just use the general page, but program it to present the content in the language matching the choice. So you need links somewhere on the page(s) that allow a language change, then just store that variable once the links are clicked, and finally display content based on that variable.

    3. Site structure:
    You will need to recode your entire site to make this work.
    There are a couple approaches.
    a) You can just create a whole copy of your site (like /en/ and /fr/) manually and update it manually. This is easiest to set up, but hardest to maintain. It's not a very good idea unless your site never changes.
    b) You can use a single page setup and load phrases like I mentioned before. You can use files on your server, or better a database. You would then make a function (for example) that works like this language('Hello') which would of course take that input and "translate" it based on a list you have somewhere. This is how slightly more complex systems work. Remember you can leave the default (if the phrase is not defined) as just keeping the English, so you can always have SOME content, even if it isn't actually translated. This means you can keep the website running in French, even if it takes a long time to actually get all the translations, or if the content changes too quickly to keep up.
    c) Finally, if you want to make the effort to do it the best (but most complex) way, then you can use a full CMS system to generate ALL of the content on your site, including switching for language. This means you would use something like joomla to control all of the content and use a template/content two-part setup for your whole site.


    I hope this gives you an idea where to start. You need to now decide what is the best for your site. Generally pick (a) for small sites, (b) for bigger projects, and (c) for complex large sites with a lot of content that is often changing.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Feb 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the thorough reply! That was more than I expected.

    All of the pages are translated and there won't be a whole lot of maintenance so I decided to have two directories: en/ and fr/ each with the same structure just with the translated copy so I'm not concerned about scope or maintenance challenges.

    I suppose I could hard code the URLs to switch from, say en/about/index.asp to fr/about/index.asp but I'd rather use a dynamic solution in the URL in the event that the site does become a little less simplified in the future.

    I stumbled across this which I named language-switch.php and saved to my root dir:

    Code:
    <?php
    	$completeNewURL = "";
    	
    	$referer = "$HTTP_REFERER ";
    	$brokenDownReferer = "";
    	
    	if (stristr($referer, "/en/")){
    		$brokenDownReferer = str_replace("/en/", "/fr/", $referer);
    	}else{
    		$brokenDownReferer = str_replace("/fr/", "/en/", $referer);
    	}
    	
    	//These two lines of code will redirect the user to the corresponding 
    	//french/english version of the page they were just on.
    	
    	//print("$completeNewURL");
    	$URL="$brokenDownReferer";
    	header ("Location: $brokenDownReferer");
    ?>
    and then in my en/about/index.php file I hyperlinked this:

    Code:
    <a href="../includes/language-switch.php">Francais</a>
    After confirming that the path to the language-switch.php file was correct the script didn't work. Unfortunately, I'm running this test on localhost so I can't post an example.

    Cheers

  4. #4
    Join Date
    Oct 2009
    Location
    Sialkot
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    we can change the language through the google api ? is that possible ? or any other website provide online translate our web ?

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Sialkoti View Post
    we can change the language through the google api ? is that possible ? or any other website provide online translate our web ?
    Probably, but those translations aren't very precise in many cases.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Feb 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Probably, but those translations aren't very precise in many cases.
    Exactly. When it comes to the actual translation of web content, you're better off hiring someone to do the translating. That's why I'm just looking for a script to swap the directory since my content is already translated.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The approach in the script above looks workable enough.

    If ALL you need is the link, then it's not very complex at all.

    Simply have an a tag like this:
    <a href=".....">Switch Language</a>

    (If you want "Language" to say the language, then you need to store a variable for the current setting and use the opposite one, OR you can extract it from the URL-- ie, "if (fr), echo 'en'), etc.)

    Anyway, for the href:
    PHP Code:
    <?php
    $currenturl 
    $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    echo 
    strpos($currenturl,'/en/')?str_replace('/en/','/fr/',$currenturl):str_replace('/fr/','/en/',$currenturl);
    ?>
    ( condition?true:false; )

    Something like that should work.

    If you end up with more than two languages, it's better to store a variable and have a menu for choosing the new language.

    I think that string (HTTP_HOST.REQUEST_URI) should work, but I'm not positive that will always be the full URL. If that doesn't work, google for a better "current url in php" or just ask again. From a quick google search that looks right, but it might not be entirely reliable, so just a warning there.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Feb 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ah, success! It turns out that the REQUEST_URI string was all that was needed else it returns the URL twice. Here's the modified code:

    Code:
    <?php
    $currenturl = $_SERVER["REQUEST_URI"];
    echo strpos($currenturl,'/en/')?str_replace('/en/','/fr/',$currenturl):str_replace('/fr/','/en/',$currenturl);
    ?>
    I tested it in a couple of different pages within each en/ and fr/ dir to make sure it was switching properly and it works like a charm.

    I can't thank you enough for the quick response and assistance. You are one class act, sir.

    Cheers

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The issue with REQUEST_URI is that I'm not sure exactly what the format is.

    For any given page, you can have many links to it:
    Assuming you are on http://example.com/dir/1.php
    <a href="http://example.com/dir/2.php">
    <a href="/dir/2.php">
    <a href="2.php">

    Any of those will take you to the same page, so depending on how your links are set up (absolute, relative, or something in between), then you may not get the /fr/ part of the url in it every time. I think that the browser will format any of those with the relative location it's already at-- so you end up with a reasonable link. But I'm not sure about the best way to test this, because it would require setting up many pages and testing for all possibilities.

    However, if it's working that's great, and no reason to mess with it. But if you run into problems it is likely due to that.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •