ASP.NET MVC: When to set Thread.CurrentThread.CurrentUICulture?

前端 未结 2 1385
慢半拍i
慢半拍i 2021-01-31 09:31

I am just beginning to localize an ASP.NET MVC application. Most of the strings will be defined in resource files and retrieved via Matt\'s Localization Helpers. Other strings m

2条回答
  •  闹比i
    闹比i (楼主)
    2021-01-31 09:49

    Request.UserLanguages[0] can only be a hint what language the users wishes to see. Most users dont know where to change the browser language.

    Another point: Dont be sure that Request.UserLanguages[0] is a valid language. It can even be null. (Not sure what bots have there)

    You usually have a Language chooser on the page. Once a user has selected a language there, it is stored in a cookie, session or url. I like to use url because I think it looks pretty.

    If a user sees your page without having set a language on your page, you should check if Request.UserLanguages[0] is a language you support and set Thread.CurrentThread.CurrentUICulture.

    I use a filter to set Thread.CurrentThread.CurrentUICulture. Thats ok as long as no other filter is using Thread.CurrentThread.CurrentUICulture. Otherwise you would need to set the right execution order for filters.

    I also use Matts helper and it worked very well so far.

提交回复
热议问题