SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict Bravogolf's Avatar
    Join Date
    Aug 2004
    Location
    All over really :)
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Setting another language on site and remembering

    Hi all, at eye-floaters.com I want to create a French version on the same page. Ideally I'd rather not have two versions of the same article. Instead have one PHP page that has if(language=FR) then show French text else show English text. The text is hardcoded on the PHP page as opposed to being stored on a database.

    As far as I can tell, a session variable would work best, could store a link under the colour styles section (left hand column of page) and then click for French or click for English. The chosen language would then become plaintext as opposed to a link. What do you think, would that work?

    Would like the language to remain the same on each page until the user either exits the page or changes language

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Session variable would be fine, a COOKIE would be better. With a cookie the user can come back to your site and continue in their chosen language rather than have to select it each time they visit.

    Have a look at Cookies for more information
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Addict Bravogolf's Avatar
    Join Date
    Aug 2004
    Location
    All over really :)
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, you're right, spikeZ, a Cookie would definitely make more sense. Alas, my coding ability (or lack thereof) means I'd have to spend a lot of time learning to get that function working. Are there any demo's or existing code functions that I could review, that you know of?

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Cookies are quite easy to use and shouldnt take that long to learn how to manipulate.

    Basically a cookie holds a value which can then be read using the $_COOKIE super global.

    so the cookie syntax is:
    setcookie ($name ,$value, $expire, $path, $domain, $secure, $httponly)

    You dont need to use all the arguments though:
    PHP Code:
    setcookie('myCookieName''myCookieValue'); 
    Would do to set the basic cookie.
    PHP Code:
    $language $_GET['language']; // eg fr or gb

    setcookie('language_cookie'$language);

    #print_r($_COOKIE['language_cookie']);
    if(isset($_COOKIE['language_cookie'])) {
        echo 
    'use language:'$_COOKIE['language_cookie'];
    } else {
        echo 
    'No Cookie Available';

    Now the thing with cookies is that they are only available AFTER the page that has set them has either been refreshed or the user has moved on to a different page. To use the language selected you can:

    PHP Code:
    $language $_GET['language'];
    $_SESSION['language_temp'] = $language;
    setcookie('language_cookie'$language);

    #print_r($_COOKIE['language_cookie']);
    if(isset($_COOKIE['language_cookie'])) {
         unset(
    $_SESSION['language_temp']);
        echo 
    'use language:'$_COOKIE['language_cookie'];
    } else {
         echo 
    'No Cookie Available but I can use the session var, so:'$_SESSION['language_temp'];
         

    Hope that helps
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Addict Bravogolf's Avatar
    Join Date
    Aug 2004
    Location
    All over really :)
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That helps a lot, thank you spikeZ. I'll play around with a demo page later on in the week, should be able to get it working with your code.

    Cheers

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    No problem, if you need more - post back
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A couple of things:
    • no need for a session whatsoever
    • both languages need to be fully accessible without the cookie otherwise one will not be crawled by Google
    • work out the best language to send to the client based on the $_SERVER['HTTP_ACCEPT_LANGUAGE'] variable
    • only set a cookie to the client if they actively changes the language

  8. #8
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And remember this when using cookies:

    setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.
    http://tr2.php.net/manual/en/function.setcookie.php
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  9. #9
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by bokehman View Post
    A couple of things:
    • no need for a session whatsoever
    • both languages need to be fully accessible without the cookie otherwise one will not be crawled by Google
    • work out the best language to send to the client based on the $_SERVER['HTTP_ACCEPT_LANGUAGE'] variable
    • only set a cookie to the client if they actively changes the language
    Quote Originally Posted by glenngould View Post
    And remember this when using cookies:


    http://tr2.php.net/manual/en/function.setcookie.php
    Both excellent points
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  10. #10
    SitePoint Addict Bravogolf's Avatar
    Join Date
    Aug 2004
    Location
    All over really :)
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, so my plan is to have two links, one for setting English and one for setting French. By default page will load in English.

    When set in French, will store var and shall in each page have a if lang=fr then show the french text and if lang isn't set or is Eng then show English.

    So, my helpful anonymous friends, by hiding the page text in an If loop, will it still get indexed by search engines?
    Also, I already have cookies being set by a piece of code to set stylesheets, I assume there's no restriction on having more than one cookie being set?


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
  •