【问题标题】:Is there a .NET Culture-dependent 'Today' string?是否有 .NET 文化相关的“今天”字符串?
【发布时间】:2009-02-11 03:57:11
【问题描述】:

我正在编写一个月历样式的控件,并且需要显示一个指示今天日期的字符串。所以在英语文化机器上它会显示'Today : 11/02/2009'

如果碰巧使用了不同的文化,例如法语,那么我想使用法语单词来表示“今天”。

.NET 平台是否将此词作为文化信息的一部分公开,以便我可以自动检索它?我找不到任何暴露的东西,但也许我没有找对地方。

【问题讨论】:

    标签: c# .net datetime monthcalendar


    【解决方案1】:

    旧.. 但仍然有用(几岁?VB6 旧)。

    基本上,Windows 在 Comctl32.dll 中保留了“Today”的本地化版本。你可以通过 loadstringex 调用来找出它:

    Private Const IDM_TODAY As Long = 4163
    Private Const IDM_GOTODAY As Long = 4164
    
    Public Function GetTodayLocalized(ByVal LocaleId As Long) As String
        Static hComCtl32 As Long
        Static hComCtl32Initialized As Boolean
        Static hComCtl32MustBeFreed As Boolean
    
        Dim s As String
    
        If Not hComCtl32Initialized Then
            hComCtl32 = GetModuleHandle("Comctl32.dll")
            If hComCtl32 <> 0 Then
                hComCtl32MustBeFreed = False
                hComCtl32Initialized = True
            Else
                hComCtl32 = LoadLibrary("Comctl32.Dll")
                If Not hComCtl32 = 0 Then
                    hComCtl32MustBeFreed = True
                    hComCtl32Initialized = True
                End If
            End If
        End If
    
        If hComCtl32Initialized = False Then
            s = "Today"
        Else
            s = LoadStringEx(hComCtl32, IDM_TODAY, LocaleId)
            If s = "" Then
                s = "Today"
            End If
        End If
    
        If hComCtl32MustBeFreed Then
            FreeLibrary hComCtl32
            hComCtl32MustBeFreed = False
            hComCtl32Initialized = False
            hComCtl32 = 0
        End If
    
        s = Replace(s, "&", "")
        If Right(s, 1) = ":" Then
            s = Left(s, Len(s) - 1)
        End If
    
        GetTodayLocalized = s
    End Function
    

    【讨论】:

      【解决方案2】:

      This 是一个非常全面的 .Net 本地化概述。

      简而言之,DateTime 结构的方法将根据系统区域设置格式化日期。您可以通过指定自己的语言环境来覆盖默认语言环境。

      编辑:对不起,我误读了你的问题。不,没有这样的事情。您可以使用翻译网站来获取您需要支持的“今天”的翻译,并将它们保存在代码中的字典中。 但是,经过仔细检查,我根本不建议这样做,因为生成的字符串“Today: xx/xx/xxx”在其他语言中可能会让人感到尴尬。虽然德语版本:“Heute: 11.2.2009”或法语“Aujourd'hui: 11.2.2009”在日历中似乎可以正常工作,但我不能说中文或日文。这说明了当您认为本地化只是翻译时可能会遇到的问题。

      【讨论】:

      • 谢谢。遗憾的是,解释没有提到检索“今天”字符串的格式值或文化值。
      • 要意识到的是:在某些语言中,使用“今天”这个词然后提供日期可能是错误的。他们只是可能不遵守那个约定。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多