14

我目前正在获取所有时区的列表,如下所示:

var TheListOfAllTimezones = TimeZoneInfo.GetSystemTimeZones();

因此,例如,巴黎的时区DisplayName具有W. Europe Standard Time. 现在我如何用另一种语言获得这个列表?例如,对于法国的用户,我想显示Heure Europe de l'Ouest.

谢谢。

在此处输入图像描述

4

4 回答 4

6

Changing the CurrentCulture doesn't work as the information comes from the registry (XP) or from the Multilingual User Interface (MUI) DLL (Vista, Windows 7).

On Vista or Windows 7, you may install other languages and change the display language (Region and Language -> Keyboards and languages -> Display language). A reboot is required. This, and only this, will actually change the language used in TimeZoneInfo.

On Windows 7, only Ultimate and Enterprise allow the installation of other languages - by means of installing Multilingual User Interface Packs.

Once you installed other languages, you should be able to find the DLLs in the system32 folder (look for tzres) and maybe export the resources with Visual Studio.

As to credible/official sources - how about an article on msdn of the BCL Team:

...the display strings are loaded either from the Multilingual User Interface (MUI) DLL, tzres.dll, or straight from the registry, when MUI support is unavailable. MUI-enabled operating systems such as Windows Vista contain MUI_Display, MUI_Std, and MUI_Dlt keys, which are indirectly controlled by the operating systems regional settings. On down-level platforms such as Windows XP and Windows Server 2003, only the Display, Std, and Dlt keys exist. The Display, Std, and Dlt key values are localized only in the default language of the operating system.

So what did they write about CurrentUICulture ?

Because of the Windows time zone registry architecture, CurrentUICulture settings do not impact the values of these TimeZoneInfo properties.

于 2012-11-02T20:43:27.103 回答
1

如果您的线程是法语(“fr-FR”),并且如果该语言是您的 Windows 版本的“本地”语言,CurrentCulture那么属性似乎将是法语。StandardNameDaylightName

编辑:

看起来改变CurrentCulture线程不会有帮助。时区都来自注册表(请参阅 Jim Mischel 的路径答案),看起来 Windows 安装的语言决定了这些值。ID(注册表路径中的键)始终为英文,而其他属性取决于 Windows 语言。

以下代码的输出是什么:

Console.WriteLine(CultureInfo.InstalledUICulture.DisplayName);

var tzi = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
Console.WriteLine(tzi.Id);              // always English
Console.WriteLine(tzi.DisplayName);     // localized
Console.WriteLine(tzi.StandardName);    // localized
Console.WriteLine(tzi.DaylightName);    // localized
于 2012-10-31T17:25:14.193 回答
0

文档中

此方法返回的集合使用当前区域性按显示名称排序。

在获取列表之前,您是否尝试过将当前文化更改为用户的文化?

那可能行不通。唯一可用的显示名称可能是已安装的 Windows 版本的显示名称。也就是说,如果您有美国英语版本的 Windows,则可能不存在法语显示名称。查看我的注册表 ( HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Time Zones),我只看到英文名称。

于 2012-10-31T17:22:36.290 回答
0

如何创建像“TimeZoneInfoExtension”这样的类,该类将具有名为 ToLocolizedString 的静态方法:

public static class TimeZoneInfoExtensions
{
    public static string ToLocalizedString(this TimeZoneInfo timeZone)
    {
        switch (timeZone.Id)
        {
            case "Dateline Standard Time":
                return i18n.DatelineStandardTime;

            case "UTC-11":
                return i18n.UTC11;

            case "Hawaiian Standard Time":
                return i18n.HawaiianStandardTime;

            case "Alaskan Standard Time":
                return i18n.AlaskanStandardTime;

            ....

            default:
                throw new NotImplementedException();
        }
    }
}

其中 i18n 是一个有资源的类。是的,您必须手动填写翻译。但我只是在不同的系统语言中使用了类似的东西来生成翻译:

Regex rgx = new Regex("[ +-]");
foreach (var timeZone in TimeZoneInfo.GetSystemTimeZones())
{
    Console.WriteLine("  <data name=\"{0}\" xml:space=\"preserve\">", rgx.Replace(timeZone.Id, string.Empty));
    Console.WriteLine("    <value>{0}</value>", timeZone.DisplayName);
    Console.WriteLine("  </data>");
}

然后你可以根据你的 CurrentCulture 来使用它,如下所示:

foreach (var timeZoneInfo in TimeZoneInfo.GetSystemTimeZones())
{
    Console.WriteLine(timeZoneInfo.ToLocalizedString());
}
于 2014-07-01T06:37:26.710 回答