How can I convert a string with a month name like "agosto" to the english translation "August" without knowing if "agosto" is spanish or italian?
I know that I get the month name for the month number 8 in Spanish with
Dim SpanishMonthName as String = Globalization.CultureInfo.GetCultureInfo("ES").DateTimeFormat.GetMonthName(8)
But how can I get the string "August" (8th month name in english) as translation for the spanish or italian month name "agosto"?
Inside the various
CultureInfothere are the month names (seesomeCulture.DateTimeFormat.MonthNames), so you could:You could even try to delegate a little to the .NET
DateTime.ParseExact:In general there is at least one month that has a different meaning in two languages: listopad (October or November, see here). The full list is Hlakola, listopad, Mopitlo, Nhlangula, Nyakanga, Phupu
A first version that uses a
Dictionary<>to collect month names:use it like:
(note that you should cache
mnf... it is probably quite expensive to build)Mmmh... I don't like it... I'm a little OC... and the simple fact that I know that there are some collisions in the name of the months is bothering me.
Here is a second version, using a
ILookup<>and saving even theCultureName, so that it is possible to discover the language(s) of the month name. TheGetMonthNumbers(monthName)now returns a(int MonthNumber, string CultureName)[], an array of anonymous valuet types. You can clearly take the first one and live happily, or you can check it to see if there are multiple differentMonthNumbers.Use it like:
(even here you should cache
mnf... it is probably quite expensive to build)Note that there are many similar cultures, so
numberswill be quite big (for example just for Italian there are 5 cultures, and a search for agosto returned 52 different cultures with the month agosto.