Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get DayNames from language only in .NET

Assume that I only have a country code (en, de, fr) and I need to display the weekdays in this language. I know about RegionInfo and CultureInfo - but I can't find a solution. If I create a country info from (for an example) "en" I have no DateTime info in it. It would also be OK to just take the first matching Region. For an example en-US for en or de-DE for de.

I don't know if there are differences in DayNames but I know there are some for the months. de-DE Februar - de-AT Feber -- anyhow I don't care. Event if it may be "a bit different" (to see Februar instead of Feber) - it is still German.

And that's what I want to achive - get en an write Monday - get de and write Montag...

Is there a way to create a region just from a language code?

like image 471
ManniAT Avatar asked Mar 23 '10 14:03

ManniAT


2 Answers

This piece may be helpful: to go from "en" to the CultureInfo, a quicker way is

CultureInfo ci = CultureInfo.CreateSpecificCulture("en")

For the second part, I believe you are asking for day names, so you would write

string[] names = ci.DateTimeFormat.DayNames 
like image 131
Jennifer Zouak Avatar answered Sep 30 '22 18:09

Jennifer Zouak


It's usually when you say "I don't care" that you start fighting the APIs.

Anyway, this should work:

var list = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
var ci = list.FirstOrDefault(c => c.IetfLanguageTag.StartsWith("de"));

var ri = new RegionInfo(ci.Name);
Console.WriteLine("Today = {0:dddd MMMM yyyy}", DateTime.Today);
like image 33
Henk Holterman Avatar answered Sep 30 '22 19:09

Henk Holterman