Language depending on cookie or browser

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Language depending on cookie or browser

Post by jmansa »

I have a script which generates a langaugefile like this:

Code: Select all

        $sql = $db->sql_query("SELECT * FROM ".$prefix."_lang_code WHERE lang_id=2");
        while ($row = $db->sql_fetchrow($sql)) {
        
        define("_".$row['tagname']."","".$row['string']."");
        echo 'Tagname: _'.$row['tagname'].' | Text: '.$row['string'].'<br>';
        
        }
As you can see it generates the language depending on "lang_id".

I want it to choose which lang_id it should use depending on first, if a cookie is active with the preferred language like this:

Code: Select all

$cookie_read = explode("|", base64_decode($user));
          //define variables to hold cookie values.
          $userid = $cookie_read[0];
          $language = $cookie_read[6];
If the cookie is not set it should look at what language browser is like this:

Code: Select all

$browser_lang = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
And if the browsers lang is unsupported it should go to the default language which is lang_id=1.

I have a db table (".$prefix."_lang_languages) with the installed language with these fields(id, lang_long, lang_short) where the lang_short field is the field that matches the browser lang id (en, de, da, se, etc...)

Can this be done and if yes... how? Hope somebody can help or guide me in the right direction :-)
Post Reply