开发者

What is the best way to store a chosen language in a bilingual web application?

There are a couple of approaches that come to mind:

  1. GET. I.e. something like www.domain.com, www.domain.com/lang/de

  2. Session.

  3. Database.

I am curious what is an industry st开发者_如何学Goandard in this area.


The Cookie method can become really annoying on a public machine (Internet cafe, etc).

For the best SEO results you should set up your URLs for different languages anyways, but I normally also store the selected language in session. Of course that expires on browser close, but when a user comes to the site, I always check the Accept-Language header, and decide the starting language based on that.

If you have something like an auto-login feature, you can save the selected language in the database too.


You can't store information on GET and plus you need to rewrite the URLS to look like this www.domain.com/lang/de But its really good for make your application search engine friendly.

And for your answer, I will use what you call it GET + Cookies.

Database - you will need a table for that or if you have already a table for your user you will need at least one more field.

Session the users should set they are Language every-time its a bit boring!


The GET method would require the user to go to the start page every single time, even if he has a direct link to something inside. Otherwise you cant use get, or you'll have to include it in every single link. The session method will expire and forget about the user. The database is nice, but the user will have to login AND THEN see the website in his own language. I think the best thing to do is use a cookie.


You could change de default application locale in the bootstrap based on the value stored in a session. This way you get more flexibility if you want to add a new language, you just have to create a new translation file if all of you code is built based on the user local. This will also ease the date/time and money display process since those are also based on the locale.

french locale ie : fr_FR or fr_CA (for french France of french Canada) english locale id : en_US or en_UK (for english United States or english United Kindom)

and then if you have to display money, the local will set the good money symbol (but won't do the price conversion) (ie fr_FR -> € and fr_CA -> $)

You could base the default language selection based on the geolocation of the user based on his IP address


Well...

  1. with a custom route setup in ZF - and you can probably omit the 'lang' portion. it's also really easy to configure and good for SEO.
  2. sessions are good way to setup #1 when a user returns to your site
  3. database storage is a good way to setup #2 and #1 when a user logs in with their own preferences set.

What is also a good way to attempt to do the initial setup is use Zend_Locale to detect the users language via their OS/browser settings. But, if you do this you should always let users override your detected value (and save it in a session or db setting)

This is something that is entirely up to you. You'll be hard pressed to find a "standard."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜