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!

Answers


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);
        }
    }    
}

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"));

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);
}

Need Your Help

Xcode 9 - Localization Issue Warning Storyboard

ios xcode storyboard interface-builder xcode9-beta

I am currently testing out Xcode 9 beta 3 at the time of this writing.

Parsing a Vimeo ID using JavaScript?

javascript vimeo

How do I parse an ID from a Vimeo URL in JavaScript?