本地化DayOfWeek的方式不那么难看?

时间:2011-05-02 23:38:44

标签: c# localization culture

using System;

namespace Server.Custom.Extensions
{
    public static class FriendlyExtensions
    {
        public static string Friendly(this DayOfWeek day)
        {
            if (day == DateTime.Now.DayOfWeek)
                return "Hoy";

            int dayOfWeek = (int)DateTime.Now.DayOfWeek;
            int dayOfEvent = (int)day;

            if (dayOfWeek + 1 == dayOfEvent || (dayOfWeek == 6 && dayOfEvent == 0))
                return "Mañana";

            switch (day)
            {
                default:
                case DayOfWeek.Monday: return "Lunes";
                case DayOfWeek.Tuesday: return "Martes";
                case DayOfWeek.Wednesday: return "Miercoles";
                case DayOfWeek.Thursday: return "Jueves";
                case DayOfWeek.Friday: return "Viernes";
                case DayOfWeek.Saturday: return "Sabado";
                case DayOfWeek.Sunday: return "Domingo";
            }
        }
    }
}

有没有办法用Cultures本地化这个?怎么样? :( 顺便说一句,我希望它也能说“今天”或“明天”,而不仅仅是转换日子

2 个答案:

答案 0 :(得分:7)

DateTime.Now.ToString("ddd", new CultureInfo("es-ES"));

DateTime.Now.ToString("dddd", new CultureInfo("es-ES"));

参考文献:

答案 1 :(得分:4)

来自here的代码(见下)可能会让您走上正轨。

 CultureInfo german = new CultureInfo("de-DE");
 DateTimeFormatInfo dtfi = german.DateTimeFormat;

 Console.WriteLine("Days of the week for the {0} culture:",
                    german.Name);
 for (int ctr = 0; ctr < dtfi.DayNames.Length; ctr++)
    Console.WriteLine("   {0,-12}{1}", dtfi.DayNames[ctr],
                      dtfi.DayNames[ctr] == dtfi.DayNames[(int)dtfi.FirstDayOfWeek] ? 
                            "(First Day of Week)" : "");