IIS use wrong pt-PT - c#

In .net 4.0, web forms and IIS 8
I have in web.config this:
<globalization culture="pt-PT" uiCulture="pt-PT" />
When I do in C# this:
ltNumber.Text = (12345.12).ToString("N");
I get this: 12 345,12
But the output must be 12.345,12
This began to look bad on windows 10. windows 7 everything was fine. What can be wrong?

The numeric ("N") format specifier uses your CurrentCulture settings.
That means your CurrentCulture has white space as a NumberGroupSeparator and , as a NumberDecimalSeparator.
As a solution, you can Clone your CurrentCulture, set these properties what ever you want, and use that culture as a second parameter in your ToString method. Like;
var clone = (CultureInfo) CultureInfo.CurrentCulture.Clone();
clone.NumberFormat.NumberDecimalSeparator = ",";
clone.NumberFormat.NumberGroupSeparator = ".";
ltNumber.Text = (12345.12).ToString("N", clone); // 12.345,12
I don't think these properties are changed based on operating system versions. It all about which culture settings you use.

Related

Azure culture-specific month/day formatting different than localhost

I'm running into a very strange issue where the "month/day" standard date format as specified on https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx is rendering differently on my local machine than it is on my azure cloud services and websites.
The culture in this case that is rendering differently is "en-AU". For the date of 2017-05-04 it should render as 4 May and on my local machine it does exactly that. On our website (azure cloud service) and our API (azure website) it renders as May 4. The strange part is that if I use the "short date pattern" it renders as 04/05/2017 on both azure/local. So this seems to be specific only to the "month/day" pattern.
I've tried setting
var culture = new CultureInfo("en-AU");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
and the formatting code is
string.Format(new CultureInfo("en-AU"), "Until {0:M} {0:yyyy}", endDate);
I'm wondering if its possible that the version of some culture definition is different in Azure than it is on my local machine? To my knowledge they are both running .net 4.5. I've added log statements in the code so I can confirm that the culture is set correctly on the line that the code runs, but for some reason, it is just outputting a different value in Azure than it does locally.
I have used both "en-AU" and "en-ZA" culture in both local and Azure environments.Unfortunately,I did not face the issue that you have mentioned in your question in both environments.
It seems the date format that you are getting is US format which might be due to the fact that azure data center that you are using to host your application is based in USA and your date is formatted to that culture.Anyway,give a try to format the date like :
var currentCulture = new CultureInfo("en-AU");
var formattedDate = DateTime.Now.ToString("G",currentCulture);
For the South African culture,try the following:
var currentCulture = new CultureInfo("en-ZA");
var formattedCurrency = currency.ToString("C", currentCulture);
//currency = 100000 then formattedCurrency => R 100 000,00
Good luck ..!!!

Use the Culture of the OS, but not the language

I've encountered several times the same problem in applications we develop:
We want to allow the user to edit/display it's data in his format (date, currency, ...), but we want to display the application in English only (for several reasons, it's a pro, international application, in a domain in which we communicate mostly in English).
There is no problem when we manage the whole application, but most of third-party pro frameworks that I used (Telerik, DevExpress) are using the CurrentCulture to display my data in the correct format AND in the corresponding language.
So, even if I have my computer in English, I have my regional settings set to fr-CH, I will have all third party user controls in French.
I cannot set the CurrentCulture to a specific culture and set the format of my user controls to something else (I would loose my default format) and I can't let the CurrentCulture to be the default one because I would have my third party components in another language.
I tried to build my own culture (CultureAndRegionInfoBuilder), with no success. When I change the language, I still have my application in the user-specific language.
Concrete problem
I'm using a date editor(basic, it has one text input and can popup a calendar). I want to have the date displayed in my OS locale(ch-FR, so 15 january 2013 would be "15.01.2013"), but I don't want that when I display the calendar month/day name appears in french.
What is the correct approach with this?
Store the original CultureInfo for your purposes and try editing CurrentCulture and CurrentUICulture properties of the CurrentThread property in System.Threading.Thread, maybe this will solve your problem.
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
I resolved my problem by having a custom culture info:
private static void UpdateCultureInfoWithoutLangage()
{
//We initialize a en-US cultureInfo and change all formats + number infor related
CultureInfo cultureInfoEn = new CultureInfo("en-US");
CultureInfo cultureInfoEnClone = (CultureInfo)cultureInfoEn.Clone();
//Setting DateTimeFormat(Without changing translations)
cultureInfoEnClone.DateTimeFormat.FirstDayOfWeek = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
cultureInfoEnClone.DateTimeFormat.FullDateTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern;
cultureInfoEnClone.DateTimeFormat.LongDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern;
cultureInfoEnClone.DateTimeFormat.LongTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern;
cultureInfoEnClone.DateTimeFormat.MonthDayPattern = CultureInfo.CurrentCulture.DateTimeFormat.MonthDayPattern;
cultureInfoEnClone.DateTimeFormat.ShortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
cultureInfoEnClone.DateTimeFormat.ShortTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
cultureInfoEnClone.DateTimeFormat.TimeSeparator = CultureInfo.CurrentCulture.DateTimeFormat.TimeSeparator;
cultureInfoEnClone.DateTimeFormat.YearMonthPattern = CultureInfo.CurrentCulture.DateTimeFormat.YearMonthPattern;
cultureInfoEnClone.NumberFormat = CultureInfo.CurrentCulture.NumberFormat;
Thread.CurrentThread.CurrentCulture = cultureInfoEnClone;
Thread.CurrentThread.CurrentUICulture = cultureInfoEnClone;
Application.CurrentCulture = cultureInfoEnClone;
}

MVC3: Submit string - parse fails on IE and Chrome

I have an issue with the submitted form information. Decimal parsing fails when I try to parse a string returned through Interner Explorer or Chrome but not on Firefox or Safari. The strings looks exactly the same in Visual Studio. I made this debugging bit:
var asd3 = collection["formValue"]; // Get it from the FormCollection
var asd4 = asd3.Replace(",", "."); // Change the punctuation
var asd5 = Decimal.Parse(asd4); // Make the string into a decimal
var asd6 = Math.Round(asd5, 1); // Round it
It fails on asd5 when trying to parse the decimal out of asd4 with the error: Input string was not in a correct format.
Here's an image of the strings. Top is Firefox and below Internet Explorer.
What on earth could be the problem here?
What on earth could be the problem here?
Culture.
In your debugger inspect the value of Thread.CurrentThread.CurrentCulture and you will see differences between your browsers.
If you have a different culture set in your browser this culture will be used by ASP.NET when parsing values especially if you haven't explicitly specified the culture in your web.config:
<globalization culture="en-US" uiCulture="en-US" />
If this is set to auto then the browser culture will be used.
Another possibility is to force invariant culture when parsing to ensure that . (dot) will be the decimal separator.
var asd5 = Decimal.Parse(asd4, CultureInfo.InvariantCulture);

Different DateTimeFormat for dev and test environment

In the Application_BeginRequest() method of global.asax.cs in my ASP.NET MVC project there is code:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(EnCultureKey);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(EnCultureKey);
Thread.CurrentThread.CurrentCulture.DateTimeFormat = new CultureInfo(EnGBCultureKey).DateTimeFormat;
The variables are
private const string EnCultureKey = "en-US";
private const string EnGBCultureKey = "en-GB";
On the dev environment all the dates are in DD/MM/YYYY format, but on the test environment they are in MM/DD/YYYY format.
Could You please advise me on what could be the cause of this difference?
UPDATE:
Please take a look at Setting Culture for ASP.NET MVC application on VS dev server and IIS
If you do want to override these settings for all your pages (instead of giving the User a choice) then the standard way is a setting in web.config :
<globalization uiCulture="en" culture="en-GB" />
The MSDN page also points you to overriding InitializeCulture() if you want to use code.
InitializeCulture() happens early but I suspect that Application_BeginRequest happens even earlier and that its effects are overridden.
Try using that code on the
Application_Start method of the global.asax, that will ensure that every time you start your application, the culture info is set to your specifications.
Make sure you're using the right format time, for example, to show the date:
DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("d"));
Ah I see, sorry.
I think it is because of course in this way you are changing the culture of a single thread, not all thread of the application.
Put your code in a place that is executed by every thread in the application, for example, the page load.
Well, I didn't actually find what IIS setting is responsible, but I've overridden it in Application_PreRequestHandlerExecute() and it finally worked:
var culture = CultureInfo.CreateSpecificCulture(EnCultureKey);
culture.DateTimeFormat = CultureInfo.CreateSpecificCulture(EnGBCultureKey).DateTimeFormat;
Thread.CurrentThread.CurrentCulture = culture;
culture = new CultureInfo(EnCultureKey);
culture.DateTimeFormat = Thread.CurrentThread.CurrentCulture.DateTimeFormat;
Thread.CurrentThread.CurrentUICulture = culture;

Problem with Convert.ToDateTime in asp.net

I have an application that works without any problem in a spanish server.
When i uploaded the application into the online server (an english windows), im getting exceptions (of type "input string is not a valid Datetime/Int32") with Convert.ToDateTime and Convert.ToInt32. Are any web.config line that could help me in this matter? I tried adding a globalization element with the Spanish culture, but didnt worked.
Could you give me a hand?
Thanks in advance.
Josema.
You need:
System.Globalization.CultureInfo culture =
new System.Globalization.CultureInfo("es-ES");
DateTime myDateTime = Convert.ToDateTime(string, culture);
Are you specifying a CultureInfo argument, as an IFormatProvider in your String.Format() calls?
You might have set uiculture instead of culture in the globalization element, see: http://msdn.microsoft.com/en-us/library/bz9tc508.aspx.
...
<globalization culture="es-MX" />
...
You can also try using a more specific culture (like the one above es - mexico).
Ps. I have a site working like that (actually with culture="en" as in my case I needed to force english as my development computer was configured with spanish at the time).

Categories