一种不太难看的本地化 DayOfWeek 的方法?

发布于 2024-11-04 07:49:28 字数 1059 浏览 1 评论 0原文

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

有什么方法可以用文化来本地化它吗?如何? :( 顺便说一句,我也希望它能说“今天”或“明天”,而不仅仅是转换日期

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

Is there some way to localize this with Cultures? how? :(
By the way I want it to say "Today" or "Tomomorrow" too, not just convert the days

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

像你 2024-11-11 07:49:29
DateTime.Now.ToString("ddd", new CultureInfo("es-ES"));

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

参考文献:

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

or

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

References:

绅刃 2024-11-11 07:49:29

此处(见底部)的这段代码可能会让您进入正确的轨道。

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

This code from here (see bottom) might put you on the right track.

 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)" : "");  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文