Manually set Culture Info Year Time Pattern - asp.net

I would like to ask if it is a good coding practice to manually set the YearMonthPattern of the CurrentCulture? My problem is I set the current culture to Invariant, and the YearMonthPattern changed.
What I did is just added the code to set the YearMonthPattern just after setting the Cultures.
//Set Cultures
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(127);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(127);
//Set Year Month Pattern
System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.YearMonthPattern = "MMMM, yyyy";

I can't see anything wrong with it. It's just that it might not be conform to the actual culture pattern but if that's how you want years to be displayed it is OK.

Related

date times from server to client etc

I'm using .net Core 2.1,
For serializing dates i'm using newtonsoft,
Client side i'm using moment and knockout
So how do you cleanly go about setting up your site so dates can be sent back and forth and also always displayed in a friendly format?
I want the display to always be a variant of dd/MM/yyyy HH:mm:ss. Sometimes I will not be displaying the time.
Ultimately whenever i get or send a date back i get it in UK and the other end sees it as US (MM/dd/yyyy) etc.
I'm happy to work with dates on the server as ISO8601, i'm just stumped having to create new moments everywhere, specify incoming and out going formats all over the place. I'm clearly not doing it right.
These are my current serializer settings.
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Culture = new System.Globalization.CultureInfo("en-GB");
settings.DateFormatString = "dd/MM/yyyy HH:mm:ss";
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
JsonConvert.DefaultSettings = () => settings;
I'm just not succeeding in keeping the format consistent.

Asp.Net Culture name without region/country name and default settings

I am working a multi-lang site. I want to get and set culture and uiculture with culturename without countryname. If browser is english or lang is English choosen, it will return en not en-US or en-GB. Because I use one localresources file per language and I have to send language code to sql procedure as a parameter.
Thread.CurrentThread.CurrentCulture
Thread.CurrentThread.CurrentCulture.Name
Thread.CurrentThread.CurrentUICulture
All of them returns en-GB,de-DE,de-AT etc... I just want first part and use this ones.
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28VS.71%29.aspx
There are the name in the link but browser does note return it, I just want and use the simple part.
How can I do that?
It is solved (:
edit:
Now, How can I read browser's culture and if I don't have it, how can I set the culture that I have.
eg: I have en,de,ru and the visitor's browser sen me fr, I want it is shown en ?
Have a look at the TwoLetterISOLanguageName-property. It should return the identifier you are looking for. For example:
var cult = new System.Globalization.CultureInfo("en-US").TwoLetterISOLanguageName;
Returns "en" so that you can send it to the stored procedures.

asp.net multiligual website culture settings

In asp.net multilingual website in english Uk and swedish, i have three rsources file
1. en-GB.resx
2. sv-SE.resx
3. Culture neutral file.
I have create one base class and all pages is inherited from that class. There i write following lines to set UICULTURE and culture
1. Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture.Name;
2. Thread.CurrentThread.CurrentCulture = CultureInfo.CurrentCulture.Name;
Question: Suppose my browser language is Swedish(sv-SE) then this code will run because it find CurrentUICulture and CurrentCulture values as sv-SE.
Now if suppose browser language is Swedish(sv) only, in that case values will be set as
CurrentUICulture = sv; and CurrentCulture = sv-SE
Now the problem is that user can able to view all text in Culture neutral resource file that i kept as english while all decimal saperators, currency and other will be appear in swedish.
It looks confusing to usr.
What would be right approach. I am thinking following solution. Please correct me?
1. i can create extra resource file for sv also.
2. I check value of CurrentUICulture in base class and if it is sv then replace it with sv-SE
Please correct me which one is right approach or Is there any other good way of doing?
You can easily replace the value in the base class like you mentioned. I would stay away from creating an additional resource file that duplicates data since it will be harder to maintain.
if (Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToLower() != "sv")
...replace with sv-SE
EDIT See this other question]1 for additional info. There's a good article referenced in the answer of that question

Where does ASP.NET get its culture settings from?

In an ASP.NET application I'm setting the culture to lv-LV. All is fine, but the default short date format seems to be "yyyy.mm.dd". The client wants it to be "dd.mm.yyyy" (which is actually the LV standard). Where does ASP.NET get the date/time settings for a specific culture? How can they be changed for a single culture, without affecting other cultures (the application is supposed to be localizable)?
I once had a similar problem and solved it with:
DateTimeFormatInfo dtfi = (DateTimeFormatInfo)Thread.CurrentThread.CurrentCulture.DateTimeFormat.Clone();
dtfi.ShortDatePattern = "dd.MM.yyyy";
dtfi.DateSeparator = ".";
Thread.CurrentThread.CurrentCulture.DateTimeFormat = dtfi ;
Thread.CurrentThread.CurrentUICulture.DateTimeFormat = dtfi;
That way you get to keep every other regional setting except the date format.
This code actually ran on OnLoad in our Page base class. I'm not so sure that is the best way.
.Net gets the date/time settings for a specific culture from the DateTimeFormatInfo class. You can set the DateTimeFormat property of a CultureInfo with a DateTimeFormatInfo that you have modified. If that culture is the current culture, then you can set it on the CurrentCulture. Either way, this only affects the CultureInfo that you have created and won't affect any other culture. (Any call to CultureInfo.GetCultureInfo("lv-LV") will not pick up those changes either since that is a read-only copy.) You don't necessarily have to pass that CultureInfo to any DateTime formatting methods (ToString, ParseExact) - you can pass the explicit pattern or the DateTimeFormatInfo.
CultureInfo lvLV = new CultureInfo("lv-LV");
DateTimeFormatInfo lvLVdtfi = (DateTimeFormatInfo) lvLV.DateTimeFormat.Clone();
lvLVdtfi.ShortDatePattern = "dd.MM.yyyy";
lvLV.DateTimeFormat = lvLVdtfi;
DateTime.Now.ToString("d", lvLV); // short date pattern from modified lv-LV CultureInfo
DateTime.Now.ToString("d", lvLVdtfi); // short date pattern from modified DateTimeFormatInfo
DateTime.Now.ToString("dd.MM.yyyy"); // explicit short date pattern
If lv-LV is the current Windows culture, then you can set a user override (use intl.cpl the Regional and Language Options control panel and customize the format) and the framework will pick this up.

How to render local time given UTC datetime values in ASP.Net without using Javascript?

Is it possible to display local times to users without using Javascript when you store the values as UTC?
You would need serverside to be aware of the clients timezone. There isn't enough information in the typical request to make that determination, the closest you can get is the Accept_Language header which might give you a clue but is hardly useful enough (esp. if the client is in a country that has multiple timezones).
Hence you would need to user to tell you what their timezone is and then use a logon or cookie to store that info.
You could do the conversion on the serverside in vb.net, c# or whatever .net language your using. You are going to have to convert to the local time somewhere.
Your asking a very broad question with no detail so I can't recommend how to do this on the server.
Edit
Based on the comments I see the problem your having is that you want to figure out what the users timezone is without javascript. I always have the user tell me their timezone when they register.
One approach which won't be perfect would be would to be use a geo-ip lookup service that will tell you most likely where your user and give your better granularity then using the language settings.
Think this is the closest you can do:
using System.Globalization;
// get the first language from request (en, fr, ru)
var primaryLanguage = Request.UserLanguages.First().Split(";").First();
// find a culture by this language
var culture = new CultureInfo(primaryLanguage);
// if the culture is neutral, try to find the specific one
if (culture.IsNeutralCulture)
culture = CultureInfo.GetCultures(CultureTypes.SpecificCultures).FirstOrDefault(o => o.TwoLetterISOLanguageName == primaryLanguage);
// get the string from a datetime
var datetimeText = culture ? DateTime.Now.ToString(culture) : DateTime.Now.ToString();

Resources