Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting localized strings for DayOfWeek values

This code isn't localized:

Enum.GetNames(typeof(DayOfWeek))

I want a method that returns a list of localized strings, starting on an arbitrary DayOfWeek, that is localized, and I want to use the built in resources to do so. So far, I've come up with the below code, but I feel like this should be supported in an way that doesn't feel like a hack.

    public List<String> GetLocalizedDayOfWeekValues(DayOfWeek startDay = DayOfWeek.Sunday)
    {
        var days = new List<String>();
        DateTime date = DateTime.Today;

        while (date.DayOfWeek != startDay)
            date.AddDays(1);

        for (int i = 0; i < 7; i++)
            days.Add(date.ToString("dddd"));

         return days; 
    }

Know of a better way of doing this, please share. Thanks!

like image 441
JoeB Avatar asked Jun 26 '12 18:06

JoeB


3 Answers

I'll throw this in. The original post appears to want the day names in the current language. This may come from the Culture set in:

System.Threading.Threads.CurrentThread.CurrentCulture

The DateTimeFormatInfo object for this culture is easily retrieve, and you can use GetDayName:

DateTimeFormatInfo.CurrentInfo.GetDayName(dayOfWeek)

However, if you utilize the CurrentUICulture/CurrentCulture paradigm, for getting just the day name, CurrentUICulture is more appropriate. It is entirely possible that the CurrentCulture is set to en-US for somebody living in the united states, but CurrentUICulture is set to es-MX or es-US for somebody speaking/reading spanish. The dateformat should use the culture setting: mm/dd/yyyy but for day names it should use the UIculture settings: Lunes, Martes, etc.

For this reason, I suggest using this technique:

public String getLocalDayName( DayOfWeek dayOfweek) {
    var culture = System.Threading.Thread.CurrentThread.CurrentUICulture;
    var format = culture.DateTimeFormat;
    return format.GetDayName(dayOfweek);
}
like image 67
Garr Godfrey Avatar answered Sep 23 '22 06:09

Garr Godfrey


I think you're looking for DateTimeFormatInfo.DayNames. Sample code:

using System;
using System.Globalization;

class Program
{
    public static void Main()
    {
        var french = new CultureInfo("FR-fr");
        var info = french.DateTimeFormat;
        foreach (var dayName in info.DayNames)
        {
            // dimanche, lundi etc
            Console.WriteLine(dayName);
        }
    }    
}
like image 34
Jon Skeet Avatar answered Sep 23 '22 06:09

Jon Skeet


These methods will give you a list of day names, defaulting to the specified culture's first day of the week:

public List<String> GetLocalizedDayOfWeekValues(CultureInfo culture)
{
    return GetLocalizedDayOfWeekValues(culture, culture.DateTimeFormat.FirstDayOfWeek);
}

public List<String> GetLocalizedDayOfWeekValues(CultureInfo culture, DayOfWeek startDay)
{
    string[] dayNames = culture.DateTimeFormat.DayNames;
    IEnumerable<string> query = dayNames
        .Skip((int) startDay)
        .Concat(
            dayNames.Take((int) startDay)
        );

    return query.ToList();
}

Compare...

List<string> dayNames = GetLocalizedDayOfWeekValues(new CultureInfo("fr-fr"));

...to...

List<string> dayNames = GetLocalizedDayOfWeekValues(new CultureInfo("fr-ca"));
like image 28
Lance U. Matthews Avatar answered Sep 21 '22 06:09

Lance U. Matthews