How to change browser receive language

If the user has his browser for fr-CA, for example, but my site has the ability to view the page in another accessible language (for example, in English). How can I override the accept-language header so that I can reload using the specified language?

I tried to just change the Accept-Language header and then reload the page, but I'm not sure how to do this. Any thoughts?

UPDATE: Okay, so I get people to explain to me what localization is, so I shouldnโ€™t have to ask about it correctly.

In my site, globalization is currently set to global.config, which automatically sets the culture of the stream depending on which language was agreed upon when the application was launched. By default, the user browser sends an accept-language header based on the language settings for the browser, which, as mentioned above, usually do not know what they are, or how to change them. In any case, allow this default behavior so that the browser handles the language headers first. However, as a FEATURE, I want to allow the user to change this host language header from the page. For example, in an application, the language settings are usually determined by cookies or user preferences (through the profile settings), but on the landing / login page (especially if this is your first login to a specific computer), I have no idea who you are, so I only have your browser settings. But let me say that you are traveling in business and accessing this site from an American computer, the page is loading in English, and you cannot read it, and you do not know how to change the language of the browser. Would it be nice to be able to choose your language from the drop-down menu or icon or something else? I think it will be so.

So, to do this, I need to be able to change the host language header and reload the page. This is where I am not sure how to proceed.

I tried navigator.language = <selected language> and xhr.setRequestHeader , but they don't seem to work.

+7
javascript jquery asp.net-mvc-4 localization
source share
2 answers

Maybe I'm wrong, but the locale settings are not something that you change in your code. This is determined by the browser user configuration. If you use HttpClient, you definitely send the correct value for the Accept-Language header to the server, but do not expect it to change this from your code and make it effective in ALL requests that appear in the browser.

A common practice is to set up a custom cookie to your language preference. By default, you should fill this cookie with the value of the Accept-Language header. There is a parameter in ASP.NET MVC, so that Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture are set according to the user's browser configuration:

 <system.web> <globalization enableClientBasedCulture="true" uiCulture="auto" culture="auto" /> </system.web> 

Then you can create an ActionFilter that will check the user cookie and set Thread.CurrentThread.CurrentUICulture accordingly .

Thread.CurrentThread.CurrentUICulture is used to determine the resources that the code should use. Although Thread.CurrentThread.CurrentCulture is used for all operations with numbers and dates.

Here's what this action filter looks like:

 public class LanguageActionFilter : IActionFilter { public void OnActionExecuted(ActionExecutedContext filterContext) { } public void OnActionExecuting(ActionExecutingContext filterContext) { var langCookie = GetOrSetLanguageCookie(filterContext.HttpContext); var culture = new CultureInfo(langCookie.Value); Thread.CurrentThread.CurrentUICulture = culture; } private HttpCookie GetOrSetLanguageCookie(HttpContextBase context) { var cookie = context.Request.Cookies[Constants.LanguageCookieName]; if (cookie == null) { //set the cookie for first time cookie = new HttpCookie(Constants.LanguageCookieName, Thread.CurrentThread.CurrentUICulture.Name) { Expires = DateTime.Now.AddYears(1), Path = "/" }; context.Response.Cookies.Add(cookie); } return cookie; } } 

Note:

  • Even when your user selects, say, โ€œes-Arโ€ in your interface, and you have configured your UICulture for this user accordingly, if the user browser is configured with the US dates specified for that user, it will be analyzed using the English Agreement, therefore floating point number will be.

IE: if a user in the USA with a browser set to en_US prefers to use the Spanish interface of your application when this user sends information to the server, for example:

1/5/2015 will be analyzed on January 5 on the server, and not on May 1.

A number, such as 1900, will be parsed as 1900 on the server, not 1.900.

I learned this hard way. I have a question here that has never been answered.

Please let me know if you have any questions.

+4
source share

The Accept-Language header is the request header, so it is sent by the browser (or other client software). On your server, you can ignore it without doing anything. That is, it does not act as such; it all depends on whether your server code recognizes it and takes some action. Therefore, you cannot and should not change the header sent by the browser.

The user can change the Accept-Language header sent by the browser using the browser preference settings. For example, in Chrome, go to "Settings", click "Show advanced settings ...", scroll down to "Languages", click "Language and input settings ...", and then add languages โ€‹โ€‹and drag them to order them.

This is not something that people often do, and most people have no idea about the existence of such settings. So the browser sends, as a rule, some factory default values. They can be rather strange, for example. Accept-Language: fr-CA will mean "if you, dear server, have different language versions of the Im resource requesting, please give me Canadian French, if you donโ€™t have one, I donโ€™t need the version that you give." This means that if the resource exists in the generic French (French-French) or in English, this will have nothing to do with, say, the version of Swahili or Volapuk. Thus, a more sensible title will usually be Accept-Language: fr-CA, fr, en , and this is in practice how you should deal with Accept-Language: fr-CA .

+3
source share

All Articles