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.
Related
I'm running a .NET Core app which contains DateTime.Now.ToString(). When on my PC it displays European date format (dd/mm/yy) but when running on a Linux VM located in the US it displays American date format (mm/dd/yy) despite the timezone on the VM being GMT.
How can I make it display only European date format?
I know I can format it manually inside the program, but is there a way to do this globally on the Linux system?
To set a culture for all application threads you can use CultureInfo.DefaultThreadCurrentCulture property, like that
if (Thread.CurrentThread.CurrentCulture.Name == "en-US")
{
culture = CultureInfo.CreateSpecificCulture("...");
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
}
You are checking that current culture is en-US and update the default culture to any EU specific culture, like de-DE or fr-FR for example. You can also refer to MSDN for details
You can use below code :
DateTime.Now.ToString("dd/MM/yy")
It should work.
For more info: https://www.c-sharpcorner.com/blogs/date-and-time-format-in-c-sharp-programming1
I am trying to get right string values from culture resource file but it's not working, always returning english resources,
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
but
Resources.Resource1.myResource;
still getting english resources, I have two files Resource1.resx and Resource1.fr-FR.resx
I think that you need
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR")
Thread.CurrentThread.CurrentUICulture
Gets or sets the current culture used by the Resource Manager to look
up culture-specific resources at run time.
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;
}
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;
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);