Getting system Timezones in different languages

2019-01-09 15:19发布

I'm currently getting the list of all timezones like this:

var TheListOfAllTimezones = TimeZoneInfo.GetSystemTimeZones();

So for instance, the timezone in Paris has a DisplayName property of W. Europe Standard Time. Now how do I get this list in another language? For instance, for users in France, I'd like to display Heure Europe de l'Ouest.

Thanks.

enter image description here

4条回答
欢心
2楼-- · 2019-01-09 15:28

How about to create some class like "TimeZoneInfoExtension" which will have static method named like 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();
        }
    }
}

Where i18n is a class with resources. And yes, you have to fill translations manualy. But I just used something like this in different system languages to generate translations:

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

And then you can get use it depending on your CurrentCulture like so:

foreach (var timeZoneInfo in TimeZoneInfo.GetSystemTimeZones())
{
    Console.WriteLine(timeZoneInfo.ToLocalizedString());
}
查看更多
干净又极端
3楼-- · 2019-01-09 15:37

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.

查看更多
Viruses.
4楼-- · 2019-01-09 15:46

From the documentation:

The collection returned by this method is sorted by display name using the current culture.

Have you tried changing the current culture to the user's culture before getting the list?

That might not work. It's possible that the only display names available are those for the Windows version that's installed. That is, if you have a US-English version of Windows, the French display names might not be there. Looking at my Registry (HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Time Zones), I see only the English names.

查看更多
混吃等死
5楼-- · 2019-01-09 15:52

If the CurrentCulture of your thread is French ("fr-FR"), and if that language is "native" with your Windows version, then the properties StandardName and DaylightName will be in French, it seems.

Edit:

It doesn't look like changing the CurrentCulture of the thread will help. The time zones all come from the Registry (see Jim Mischel's answer for the path), and it looks like the language of the Windows installation determines the values. The IDs (which are keys in the Registry path) are always in English, while the other properties depend on the Windows language.

What is your output of the following code:

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
查看更多
登录 后发表回答