Different DateTimeFormat for dev and test environment - c#

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;

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 ..!!!

How do I force a culture in asp.net?

I want to be able to set the culture during runtime. For example:
protected void Page_Load(object sender, EventArgs e)
{
Page.Culture = "fr-FR";
Page.UICulture = "fr";
}
But that's not having any effect. I'm using resource files for translation. If I change the language of my browser it works fine, but I want the user to also be able to choose the language as well. So in this case the user wants French as the language.
Any ideas? I'm lost.
If you're creating a site where you are allowing the user to change language for example, then you need to perform this in the Global.asax file in the Application_BeginRequest method.
Each request will then have the culture set.
You simply set the following 2 lines:
Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR");
Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR");
The first line will set the number/date/etc formatting.
The second line specifies which resource localization to load - which will contain your translated content.
You can try with this:
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("en-US");
Refer this article from MSDN for further details.
If you want to set it for whole application, you can set it in your Global.asax as
Thread.Current.Culture = New System.Globalization.CultureInfo("fr-FR");

How to update/change system date time format in regional and language options in control panel using code?

I am looking for code in .NET (C#)
to change the system date/time format in Regional Options of the Control Panel
for Windows XP.
To change date-time format take a look at this http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/263d73b2-8611-4398-9f09-9aa76bbf325e/
You basically need to use native Win API method SetLocaleInfo.
If I understand right, you want to programatically change regional settings permanently, not just for your current process.
The information you need is stored in the registry. For the current user under the key:
HKCU\Control Panel\International
And the default for new users or users without a profile:
HKEY_USERS\.Default\Control Panel\International
You could change the registry values programatically, then broadcast a WM_SETTINGCHANGE message as described in a response to this question.
If you want to change temporarily the regional settings you can use following code:
System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
...
YOUR CODE
...
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
TO find out which regional setting is best, check:
CultureInfo
More reading on System.Globalisation
public void changedatetimeformat()
{
RegistryKey regkey = Registry.CurrentUser.OpenSubKey(#"Control Panel\International", true);
regkey.SetValue("sShortDate", "dd/MM/yyyy");
regkey.SetValue("sLongDate", "dd/MM/yyyy");
}
Then
Restart Your System

How do I tell my webpage to use a specific UI Culture?

I can tell my page to use a certain CultureInfo like
System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
The code above only set's the CultureInfo, not the UICulture, how can I tell the Page to bypass what the browser says and use a specific one, so all GlobalResource's could be applied to the correct culture?
in the code above and having Swedish as my first browser language I get:
System.Globalization.CultureInfo.CurrentUICulture.Name --> sv-SE
System.Globalization.CultureInfo.CurrentCulture.Name --> en-US
I need to set the CurrentUICulture so all localization is made, in this case, in English and not Swedish, like browser is set to:
(source: balexandre.com)
Try
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
I tried it in OnInit of my page and it loaded the resources properly.
EDIT: or you could try setting it in the web.config as shown here:
http://msdn.microsoft.com/en-us/library/bz9tc508.aspx
There's an example on the MSDN website that explains how to do this: How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization
Essentially you can set the CurrentCulture and CurrentUICulture properties of the currently executing thread (see the article for the full code example, this is an extract):
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);

Culture of thread for method render of LocalReport

I'm using a localreport object into an asp.net application.
This report is fed by an array of object. So on the render of the report, some properties of the classe are called.
Class ClassForReport
{
string Date
{
get{return _aDate.ToshortDateString();}
}
}
Now the code for rendering and the problem:
//first of all, I change de culture for taking in account the choice of the user
CultureInfo ci = CultureInfo.CreateSpecificCulture(isoLanguageName_);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
//Here, my culture is now: FR-be
MyLocalReport.render(...) // in this method, the property Date above is called. And when debugging I see that the culture is EN !!!
...
//and here, my culture is still Fr-be
So it seems that when the method render is called, it launch a thread and take the culture of the server and not the culture of the process.
The only workarround I see is changing my report to contains a date and then giving a parameter of culture and formating all my date in all my reports to the given culture...
So I realy hope there is a way to tell the report to take the curent culture of the asp thread and not taking some other culture comming from nowhere.
thx in advance
In ".rdlc" Designer on your ReportFile, set on the Report in Language property "=User!Language".
<Report>
...
<Language>=User!Language</Language>
...
</Report>
then your System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");
will work on the values in report.Render(...); like dates,etc.
Cheers.

Categories