Page 1 of 1

Multilanguage Site Using Cookie and Session?

Posted: Tue Oct 12, 2004 6:27 am
by poeta_eletrico
Hi!

I am developing a website in three languages today and right now I have a DIR for every language so, I have 3 pages at the same issue to every language...very hard to deal with all them together cos I have to worry with different pages of the same subject...

I think this was reasonable in the past but not now, I think. I was wondering to use $_COOKIE and $SESSION to store the preference of the user, so, the user could SELECT (form/checkbox) its preference in a form and then CHECK the box appropriated to it and then CONFIRM. So the page could reload and then change all the text layout to it s choosen language...

I´ve being reading too much documents related to this subject, but I am still uncertain to build the best method. If is good to store the $_COOKIE session for that into $SESSION just in the case browser´s user has disabled $_COOKIE ... you know... what I really want is THE NEXT TIME THE SAME USER COMES TO MY SITE, my site recognize its preference and then automatically loads his/her preference.

I would really appreciate any comments about this subject so important and I think a lot of other people must collect these informations for another usage. So I hope this NEW TOPIC becomes very useful for everyone.

Thank you very much.
Poeta_Eletrico

Posted: Wed Oct 13, 2004 6:04 am
by kettle_drum
Either way is pretty simple to implement, you just have to look at the pros and cons.

-Yes some people may not accept cookies - but im sure not many.
-Does your server allow sessions.
-If the user doesnt accept cookies and you use sessions - then you get a very unfriendly URL with the session id in.

If the pages are the same for each language, then you may want to just have one page and replace the text in the page with variables which can be set to the required language - saves you having so many pages to change to edit something.

You could also try a bit of guessing if the user hasnt got a cookie by checking the header stuff that the user sends to see if ay language details are sent.

Posted: Wed Oct 13, 2004 6:34 am
by twigletmac
You would need a cookie to save preferences for the next time the user comes to your site. You could give people the option to be remembered and tell them that this would require a cookie then those who wanted could accept it. Then use sessions to manage everybody's preferences for that session (so those who don't/won't accept cookies don't have the language changed each time they change language).

Remember that if you want to give search engine spiders the ability to crawl all your pages you would need to provide a plain text link to the different language pages as otherwise they'll be hidden 'cause spiders can't do the cookie thing.

Mac

Posted: Wed Oct 13, 2004 1:13 pm
by poeta_eletrico
-Yes some people may not accept cookies - but im sure not many.
-Does your server allow sessions.
-If the user doesnt accept cookies and you use sessions - then you get a very unfriendly URL with the session id in.
Yes, my host enables SESSIONS.
If the pages are the same for each language, then you may want to just have one page and replace the text in the page with variables which can be set to the required language - saves you having so many pages to change to edit something.
Yes. I have 8 links at all. Right now I have 24 pages (3x8) cos I need to have 1 page for every link (and have 3 languages to deal with)...following the way I described above I will have only 8 pages with all the code inside being generated by users preference.

If you have more time and patience could you parse a brief code for a better idea of mine? So I paste here the way I am developing. I think would be great for me and for the others or if you have a place where I could take a look on a tutorial ...

Thank You very much,
Poeta_Eletrico

Posted: Wed Oct 13, 2004 4:43 pm
by kettle_drum
Ok, heres some sudo code to begin with:

Code: Select all

if(user has cookie with language in){
   $language = cookie['language'];
}else{
   $language = default language;
}

//get the language vars for the users language - can be from database or file
$lang = mysql_query("SELECT * FROM lang WHERE language = '$language'");
$lang = mysql_fetch_assoc($lang);

//start your page here
echo "<html>
..........
$lang['welcome_message']
<br><hr>

<div>......$lang['click_me']
":
I first of all check to see if the user has selected a language previously if not they have to use the default language. Then i get all the possible words/phrases in an array from a database - you could get them from a file if you want. The phrases are stored in an array where the key identifies the variable content.

Then you simply replace all text in your page with the variables.