2

I am trying to change the current culture in web api throuch c#.net. I've tried in web forms and asp.net mvc also, but didn't work

After I go through some answers from Stack overflow, I've found the following answer to change the current culture.

  CultureInfo.CurrentCulture.ClearCachedData();
     System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(Code);
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(Code);

But after I update, the culture has been changed for the current request only. After sending another request from the application, the culture info has been changed to default one.

I don't know what's happening. I want to maintain the current culture for the application till the user log out.

How to implement this one?

Jeeva J
  • 3,173
  • 10
  • 38
  • 85
  • 1
    You need to do this in the global.asax file, if you are in a asp.net mvc/web forms application. If you are in a web-api, I think it does not make sense, since in a web-api project you have no application, rather just independent http requests – Veverke Jun 16 '15 at 11:49
  • Thank you. If we are using asp.net mvc/web forms application, How to do in global.asax? – Jeeva J Jun 16 '15 at 11:51
  • I've tried in asp.net MVC/web forms application too, but didn't work.. How to implement in global.asax? – Jeeva J Jun 16 '15 at 11:56
  • It's working exactly as it should: what happens when 2 users with different culture log in?? You want culture to depend on a each user and be parametric for each request; I think. – frenchie Jun 16 '15 at 12:09
  • 1
    related: [Localize text on one page in MVC](http://stackoverflow.com/q/25643017/1207195). You may skip stuff about localization with MVC and jump directly to paragraph about how to set culture for thread of served request. – Adriano Repetti Jun 16 '15 at 12:19

1 Answers1

1

You can override Application_BeginRequest in your Global.asax file to change the culture on each request.

Edit: Technically not an override. More here: how do you wire up Application_BeginRequest() in asp.net-mvc

Community
  • 1
  • 1
SaphuA
  • 3,092
  • 3
  • 39
  • 58
  • See edit. There's some sample code and explanations there. – SaphuA Jun 16 '15 at 11:59
  • I had confused. You are saying to change the culture on each request. Right? I did translate on button click. Shouldn't this retain on all the request? – Jeeva J Jun 16 '15 at 12:04
  • 2
    No, you have to [change it for each request](http://stackoverflow.com/q/25643017/1207195). Same thread may be reused for multiple requests (made by different users). Your next request may be in another thread or even in another process. – Adriano Repetti Jun 16 '15 at 12:23