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?

- 15,195
- 17
- 54
- 82
2 Answers
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
.

- 122,561
- 47
- 239
- 335
-
But changing this doest'n influence in jquery validators(the validations keeps the same), so I need to change the culture with Globalize.js. – MuriloKunze Nov 25 '12 at 12:31
-
Ok, I do not know Globalize.js. To handle JS stuff, I use dynamically generated JS variables e.g.: `var defaultCulture = "@( CultureInfo.CurrentCulture.Name)"`; and then extending JS components like `$.datepicker.regional[defaultCulture || ''];`. But my answer was explaining how to allow client culture to be used, as you asked... There could be default, but overriden with client if provided – Radim Köhler Nov 25 '12 at 12:42
-
Yeah, thank. But I need to know how to work with globalize and the validations too. – MuriloKunze Nov 25 '12 at 12:51
-
I extended my answer, with some basic how to. This is the way, how C# part can render strings representing numbers based on CurrentCulture, while jQuery.validator still evaluate them... just a clue – Radim Köhler Nov 25 '12 at 13:07
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
.

- 18,366
- 7
- 57
- 79