Development Language Selector - PHP

Discussion in 'Software' started by :: Phat ::, 3 Jul 2004.

  1. :: Phat ::

    :: Phat :: Oooh shakalaka!

    Joined:
    7 Jun 2002
    Posts:
    4,886
    Likes Received:
    3
    Right, I'm creating a website for our business, I want it bilingual, the users must be able to select a language either via a link or by a drop down etc.
    Need 2 languages. English & Welsh *groan*

    I was thinking if I could hold all the text for the site in flat files or something then link to them? Eg

    <? PHP Include ....etc content\$lang$\page1.txt> where $lang$ would be ENG or WSH or something?

    Anyway, I've tried looking for scripts etc on the web to no avail, most of the things that come anywhere near are intertwined with bloggers etc which I won't be needing.

    any help is muchos gracias :D
     
  2. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    User loads website (index.php)
    If cookie is set, go to (say) main.php and use cookie
    if no cookie:
    User selects language (lang.php)
    Cookie is set holding preference
    User sent to main page
    Site then uses cookie value to include necessary files
    Preference is stored for next time user visits assuming visitor doesnt clear cache/cookies

    index.php :
    PHP:
    if(!$_COOKIE['language']) {
      
    //click here for english or here for welsh ->lang.php
    } else {
      
    header (Location"http://www.yoursite.com/main.php");
    }
    lang.php:
    PHP:
    if($_GET['lang'] == 'eng' || $_GET['lang'] == 'wsh') {
      
    setcookie("language"$_GET['lang'], time()+31536000"/"".yoursite.com"0);
    } else {
      
    setcookie("language"'eng'time()+31536000"/"".yoursite.com"0);
    }
    header(Location"http://www.yourwebsite.com/main.php");

    main.php:
    PHP:
    if(!$_COOKIE['language']) {
      
    header (Location"http://www.yoursite.com/index.php");
    }
      include 
    '/path/to/templates/index.'.$_COOKIE['language'].'.php';
    Something like that anyway - there are many ways to do this. Code is completely untried and untested, it's also nearly 2AM, i'm tired and expecting someone to pick holes in my method :D
     
  3. Hwulex

    Hwulex What's a Dremel?

    Joined:
    1 Feb 2002
    Posts:
    4,007
    Likes Received:
    1
    Yeah, if you and your users are alright using cookies, then just do it like that. :thumb:

    As for the actual language text, if you want them in a file rather than DB-d, just define them as constants.

    lang_en.php:
    define('TEXT_GOOD_MORNING', 'Good morning');

    lang_wa.php:
    define('TEXT_GOOD_MORNING', 'Bore da');*


    include whichever lang you want as per RTTs (probably broken (;)) code) and then wherever you display the text, just do

    echo "<div style=\"width: 100px\">".TEXT_GOOD_MORNING."</div>";

    etc etc.


    (Don't know if you knew all this already, but if you did, it might help someone else).


    *I have no idea if this is correct; blame Signor Google
     
  4. Hepath

    Hepath Minimodder

    Joined:
    20 Oct 2003
    Posts:
    730
    Likes Received:
    0
    Just to annoy all those people and spam here ... the .NET technology is amazingly simple for this kind of stuff but then again its expensive (i.e. not free!)

    I have to admit I hate cookies but saving a user's preference is probably one of the few cases its acceptable. In light of the fact I know nothing much about PHP I think Hwulex has come up with the best suggestion. Its quite extensible and incredibly similar to something I was gonig to suggest.

    Only one real point to make is that sometimes dual language sites can really be a pain to get working as some translations don't overlay the same as another. Thus if your pages have static information only, I would consider making them static HTML. This has the advantage that you cut the code out quicker and will be delivered to the browser faster too, but you'll also have the maintenance nightmare of duplicated pages (deliver the site then have an upgarde path to render the site as per Hwulex suggestion). Its all swings and roundabouts.

    Sorry pretty spammy but its early at work and I'm having to use the internal internet cafe as there is no-one able to unlock my Win2K account. So I'm struggling with the Americano from our starbucks!

    Stu
     

Share This Page