Set uiCulture automatically based on browser accept language

谁都会走 提交于 2019-11-30 14:00:25

问题


How I can set the culture of the ui automatically based on the users'browser? All I found about this is Globalize.culture("pt-BR"); But it sets pt-BR as default and I dont want set this by default! I want only set this if the user is pt-BR! How can I do this? And the validator methods, how can I set them for a specific culture?


回答1:


In a ASP.NET MVC the web.config is the right place. There is a quick summary, the first snippet shows, how could be e.g. pt-BR culture forced

<globalization 
    enableClientBasedCulture="false" 
    uiCulture="pt-BR" 
    culture="pt-BR" />

If application is ready to accept the culture from the client (browser), settings should be

<globalization 
    enableClientBasedCulture="true" 
    uiCulture="auto" 
    culture="auto" />

The above setting will take a Language selected in client browser (e.g. cs-CZ in my case). If none is defined then system settings will be used. Final snippet shows, how to allow client to set and send intended culture, but in case that no Language is pre-selected, override the system setting with some other default value pt-BR

<globalization 
    enableClientBasedCulture="true" 
    uiCulture="auto:pt-BR" 
    culture="auto:pt-BR" />

Extended: culture settings for jQuery validator and numeric input

Note: I am definitely not an expert in jQuery and globalization techniques. This is example how I do adjust validator to correctly process any numeric input

razor View part (X() is a shortcut for new HtmlString()):

var defaultThousandSeprator = "@X(culture.NumberFormat.NumberGroupSeparator)";
var defaultDecimalSeprator = "@X(culture.NumberFormat.NumberDecimalSeparator)";

jQuery part (custom methods for min and max)

$.validator.addMethod("min", function (value, element, param)
{
  var num = value.replace(RegExp(" ", "g"), "") // remove spaces
          .replace(RegExp('\\' + defaultThousandSeprator, "g"), "") // thousand separator
          .replace(RegExp("\\" + defaultDecimalSeprator, "g"), "."); // fix decimals
  return this.optional(element) || num >= param;
});
$.validator.addMethod("max", function (value, element, param)
{
  var num = value.replace(RegExp(" ", "g"), "") // remove spaces
          .replace(RegExp('\\' + defaultThousandSeprator, "g"), "") // thousands
          .replace(RegExp("\\" + defaultDecimalSeprator, "g"), "."); // decimals
  return this.optional(element) || num <= param;
});

And then jQuery.validator evaluates input values for cs-CZ: 10 000,00 correctly as well as en-US: 10,000.00.




回答2:


You need to write out the script from the web page (or master page):

<script type="text/javascript">
    Globalize.culture("<% = CultureInfo.CurrentCulture.ToString() %>");
</script>

That's it. Mind you, that I used CurrentCulture instead of CurrentUICulture, as this is what you should be using for formatting. If you need the translations (which I wouldn't do this way as it would hurt localizability), you'll need your original CurrentUICulture.



来源:https://stackoverflow.com/questions/13550703/set-uiculture-automatically-based-on-browser-accept-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!