I'm having some problems with CultureInfo values for a specific culture, "pt-PT". To narrow it down I created an MVC project in VS 2017 using .net framework and added the following line to the view About.cshtml:
<p>#(new System.Globalization.CultureInfo("pt-PT").DateTimeFormat.FirstDayOfWeek)</p>
This was the only change I made to the project created by visual studio.
When I run this project in VS using the IIS Express, the value returned by FirstDayOfWeek is 'Monday'.
However if I run this project in IIS (on the same machine), the value returned by FirstDayOfWeek is 'Sunday'.
I was expecting the value to be the same and to be 'Monday'.
I'm puzzled about the diference in values and would like some help in understanding it.
Note: After further tests I concluded that if I change the first day of the week in my computer settings, that change is reflected when running the project in IIS Express. I'm more confused... I guess this property is useless...
By default, IIS uses DateTimeInfo.InvariantInfo which returns Sunday as its FirstDayOfWeek value (also using United States date format too). If you're unsure why IIS (not Express) uses different culture than you're using, set the culture information in Application_BeginRequest handler inside Global.asax code:
protected void Application_BeginRequest(object sender, EventArgs e)
{
string culture = "pt";
if (HttpContext.Current.Request.Path.Contains("pt"))
{
culture = "pt"; // Portuguese, use 'pt-PT' if not sure
}
else
{
// set to other cultures, including invariant (default) one
}
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
}
Or use globalization element in web.config file to set Thread.Culture & Thread.UICulture:
<globalization culture="pt-PT" uiCulture="pt" />
If all of the above settings doesn't work, use these steps below:
Open IIS Manager, select your site and open ".NET Globalization".
Open "Culture" tab, set both Culture & UI Culture section to Portuguese.
Restart the application pool or use iisreset command to apply all changes.
NB: As a general convention, the site shouldn't rely on server's application pool setup to work as expected with specified locale/culture. See Set the Culture and UI Culture for ASP.NET Web Page Globalization for further details about culture settings.
Also check if the regional settings of your application pool set to follow NetworkService's user account, change it to LocalSystem if necessary.
Similar issues:
ASP.NET Application is displaying american date formats
How do you set the IIS Application Pool Identity User Locale when it's set to ApplicationPoolIdentity
Related
I have a web application in which I set the CultureInfo on Thread.CurrentThread to ar-IL (Arabic-Israel):
Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-IL")
The problem is that on my local machine (Windows 10 with IIS 10) it works just fine and no exception is being thrown. However, on Azure (Windows Server 2012 R2 with IIS 8.5) it throws CultureNotFoundException:
Culture is not supported. Parameter name: name 'ar-IL' is an invalid
culture identifier
I checked the source of CultureInfo and realized that the native call to nativeInitCultureData() is the culprit. On my Windows 10 machine it returns true but on Windows Server 2012 it returns false.
Also, checking the SSCLI for nlsinfo.cpp file reveals this comment:
// Call GetLocaleInfoEx and see if the OS knows about it.
// Note that GetLocaleInfoEx has variations:
// * Pre-Vista it fails and has to go downlevel
// * Vista succeeds, but not for neutrals
// * Win7 succeeds for all locales.
// * Mac does ???
So, how can I handle custom combinations of languages and regions (ar-IL, he-US etc) that are not recognized by Windows?
P.S I'm aware of the possibility to create and register a custom locale (using CultureAndRegionInfoBuilder) but it will take too much effort to cater for all the combinations I'm planning to support.
Yes, Windows 10 is able to provide somewhat sensible information for any culture name.
Constructing culture info yourself is an option - as long as calendar information is compatible you can merge strings from neutral ("HE") culture with data for any specific one (like "en-US"). CultureAndRegionInfoBuilder can be used to safely combine information as shown in the MSDN sample:
// Create a custom culture for ru-US.
CultureAndRegionInfoBuilder car1 = new CultureAndRegionInfoBuilder("ru-US",
CultureAndRegionModifiers.None);
car1.LoadDataFromCultureInfo(CultureInfo.CreateSpecificCulture("ru-RU"));
car1.LoadDataFromRegionInfo(new RegionInfo("en-US"));
Aleternatively you can build XML files defining cultures you need to support and either load them at run-time or even install on the servers using CultureAndRegionInfoBuilder.Register - Create custom culture in ASP.NET.
From Control Panel, I set my Region and Language setting to French (France)
When I am running my application as console application,
Thread.CurrentThread.CurrentCulture returns French
But when I'm running it as windows service, it returns invariant culture or English (US)
Is there a way to fix that?
The service is probably running as a user that has it's own culture.
Why not set the culture when you start your service
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");
Also from Default Culture in a Windows Service
If your Windows Service is running under the SYSTEM account or other account without a
profile it will use the settings defined under the
"HKEY_USERS/.DEFAULT/Control Panel/International" registry key.
You can change these values from "Control Panel / Regional and Language Options / Advanced"
by checking the checkbox "Apply all settings to the current user account and to the default
user profile".
I normally use the former technique as I only need to change it for a specific service rather than for the whole OS.
.NET 4.5 added CultureInfo.DefaultThreadCurrentCulture and CultureInfo.DefaultThreadCurrentUICulture i suggest setting it as early as possible and it should solve your issue.
This will change the current threads culture and all threads that are created.
When I'm using DateTime in dd-MM-yyyy format.
When I debug my code at localhost its works fine.
But After deploying my ASP.NET web project on IIS server DateTime changes to mm-dd-yyyy format automatically.I'm facing many issues because of this problem.
I'm not able to find any solution, please let me know how can I solve this.
How can I get rid of this issue.?
1) Change the datetime format of your server from:
Control Panel -> Regional and Language Options -> Advanced
2) Open IIS and follow below steps: (For IIS7)
Click on you Website
Select .NET GLOBALIZATION option
From Culture tab, select required Culture and UI Culture.
Finally iisreset.
Your IIS probably has another Localization selected, than on your development machine.
Printing should be pretty simple if you specify the format: yourDate.ToString("dd.MM.yyyy");
Parsing a date has been a problem for me in the past. You can change the server settings or specify a CultureInfo directly in the code, like this:
DateTime.ParseExact(myDateString, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);
I can't figure out how to get the CultureInfo of the Installed System on the Client Machine.
There is the CultureInfo.InstalledUICulture Property, but it seems to be unavailable in Silverlight.
Regards
Jonny
I believe that Culture.CurrentCulture will in fact provide you with the user's culture. It can however change or be programmatically set via current thread's Thread.CurrentCulture property. I'm not sure if Silverlight can access the user's machine/operating system culture/language settings beyond this mechanism.
As you mention in a comment, you cannot trust it as it will definitely change through the lifetime of the application. Perhaps then you should record the current culture when the application first starts up before it's programmatically changed, and store it indefinitely (statically or otherwise) to be referenced by your code.
EDIT: Another possibility is to leverage the hosting browser and its JavaScript. Googling around I see that you can access window.navigator.language which will report the language of the browser. Internet Explorer likes to do its own thing and reports the browserLanguage, userLanguage, and systemLanguage.
You can write up a small JavaScript method on the page (you will want to do more cross-browser tests, version tests, and operating system tests):
function GetUserLanguage()
{
if (window.navigator.language)
return window.navigator.language;
else //yay IE
return clientInformation.browserLanguage;
}
Then in Silverlight you might have something like:
string userLanguage = (string)HtmlPage.Window.Invoke("GetUserLanguage");
CultureInfo userCulture = new CultureInfo(userLanguage);
I'm not sure if all cultures reported by the browser (across all browsers/versions/operating systems) will match the culture listing in Silverlight.
Greeting people...i develop a web..everything working fine till deployment...my question is - why is it this error appear? because if i run the web on Visual Studio Server
everything fine...but when i deploy and run it on IIS server suddenly this error
appear..why is people? really need some help here..
string tarikh = Convert.ToDateTime(txtTarikh.Text).ToString("yyyy-MM-dd");
the line is where the cause of error..thanks in advance
For this problem you have to go to the IIS
Go to IIS -> Select your Configured Website -> Click on .NET Globalization
From .NET Globalization, select Culture and UI- Culture as English (United State) (en – US)
Restart IIS by running command as iisreset through windows command prompt
Check application is giving same problem or not
Chances are the server-side default culture is different to the one you've been using in development.
What format are you expecting the date to be in, anyway? You should either use DateTime.TryParseExact or at least use DateTime.TryParse specifying the appropriate CultureInfo. (For example, the culture of the user.)
Likewise I would suggest that you supply an appropriate CultureInfo to ToString - possibly CultureInfo.InvariantCulture.
You have different "regional setting" on your development machine and the web server.
Instead of calling Convert.ToDateTime(string), you could try to use the overloaded version Convert.ToDateTime(string, IFormatProvider) and specify in what format you expect the date to be in.
Some of us thinks that today's date is "2012-04-22" while other claims it is "4/22/2012" etc...
EDIT: Just do something like:
var ci = new CultureInfo("xx-XX");
var dateTime = Convert.ToDateTime(txtTarikh.Text, ci);
Where xx-XX is the code of the culture you want to work with. Look it up here:
http://sharpertutorials.com/list-of-culture-codes/
Because the date you get from txtTarikh.Text is not parsed as date.
Probably on your local machine, your regional settings are different from your server.
Add a log and print txtTarikh.Text to see what returns at your server and also on your local machine.
Issue is resolved by setting up .net globlization settings in IIS
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" fileEncoding="utf-8" culture="en-US" uiCulture="en-US" />
</system.web>
change these lines in web.config and publish it again.. It worked for me.