我正在编写一个月历样式的控件,并需要显示一个字符串,指示今天的日期。因此,在英语文化的机器上,它会显示 Today: 11/02/2009
。
如果使用不同的文化,比如说法国文化,那么我会使用法语的“今天”(Aujourd'hui)。
.NET平台是否将此单词公开作为文化信息的一部分,以便我可以自动检索它?我找不到任何公开的东西,但可能我没有找到正确的地方。
我正在编写一个月历样式的控件,并需要显示一个字符串,指示今天的日期。因此,在英语文化的机器上,它会显示 Today: 11/02/2009
。
如果使用不同的文化,比如说法国文化,那么我会使用法语的“今天”(Aujourd'hui)。
.NET平台是否将此单词公开作为文化信息的一部分,以便我可以自动检索它?我找不到任何公开的东西,但可能我没有找到正确的地方。
旧但依然有用(多老?VB6 老)。
基本上,Windows会在Comctl32.dll中保留“今天”的本地化版本。您可以使用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
这是一个相当全面的 .Net 本地化概述。
简而言之,DateTime结构的方法将根据系统区域设置格式化日期。您可以通过指定自己的区域设置来覆盖默认区域设置。
Edit: Sorry, I misread your question. No, there is no such thing. You could use a translation site to get the translations of Today that you need to support, and keep them in a dictionary in your code. Upon closer examination, though, I would not recommend this at all, since the resulting string "Today: xx/xx/xxx" may feel awkward in other languages. While the German version: "Heute: 11.2.2009", or the French "Aujourd hui: 11.2.2009" seem to work ok in a calendar, I can t say for Chinese or Japanese. This illustrates the issues that you can run into when you think localization is just translation.