1

I am having multiple languages in my website and i would like to know whcih is the best approach to save the user selected language when browsing my website.

I thought of adding cookies and checking if the language exists and if so, show it, otherwise show the default one.

My other though was using sessions, but this wont stay long since sessions expire too soon.

Do you have any better approach? Thanks.

1
  • sessions can be made to expire after any interval you want, or not expire at all. Commented Aug 17, 2011 at 14:37

4 Answers 4

1

Cookies are a good way in you don't have any authentication on your application.

Sign up to request clarification or add additional context in comments.

6 Comments

i want all the users browsing the website(registered/unregistered) to be able to select their language and view the content with their desired language.So i guess cookies is a solution.
You can add an option to your preferences with the language of the user. So, if the user is logged in, you use his preferences, otherwise you use the cookie if set. If there is no cookie, you can use the default language.
This can be added as a feature for the user.What i need know is how to do it in a general set of people.
What do you mean by general set ? Aren't cookies enough ?
I meant that i want the cookies for all users browsing my site to be able to choose their own language and not just logged in users
|
1

i would suggest putting a suffix or prefix on your page strings that the php can pull out and set the language as neccessary (such as www.somewhere.com/en/pagename) so that you can allow bookmarking/posting etc. it should also help seo.

-Edit to add code:

here is an example code for the .htaccess:

# rewrite all requests for language-specific files
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^en/(.*)$ $1?lang=en [NC,L]
RewriteRule ^de/(.*)$ $1?lang=de [NC,L]
RewriteRule ^fr/(.*)$ $1?lang=fr [NC,L]

3 Comments

I'd recommend this in conjunction with cookies.
how is this link being generated?via htaccess?what if en/ was a folder before languages was integreted in the website?
in the htaccess you would pull the first rule then add it as a get parameter, therefore to get to the /en folder you would have to type the path /en/en. Ive added some more code to my answer to show the theory.
0

I'd definitely go with cookies - sessions expire just as soon as you close the browser, and are thus unsuitable.

Using the IP-address can yield very bad results because of dynamic addresses and whatnot.

The best thing would be to tie the language to a user account in a database, but this, of course, won't work if your website doesn't have any sort of accounts for users.

2 Comments

The problem here is that i want even unregistered users to be able to change language
Yes, and therefore Cookies is probably the best solution for making the preference persistent.
0

If you want to register your site to the search engines, you should make your links attainable. When the robots visited your page, they are not able to save cookies or session variables so they can't reach your other different language pages.

But if you dont, you can use cookies to save their choises.

http://tr.php.net/manual/en/function.setcookie.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.