Sha Le
Sha Le

Reputation: 1301

TimeZoneInfo -- translated standard name based on culture

TimeZoneInfo.StandardName returns value only in English, is there a way I can get the translated name based on set culture?

Thx.

Upvotes: 3

Views: 4226

Answers (4)

Vladyslav  Kurkotov
Vladyslav Kurkotov

Reputation: 505

How about to create some class like "TimeZoneInfoExtension" which will have static method named like ToLocalizedString:

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

Upvotes: 1

Craig
Craig

Reputation: 7076

TimeZoneInfo are pulled from the registry...

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Time Zones

So its OS Language Dependent.

Changing the culture will have no effect in the Time Zone DisplayName. Of course, you can leverage resource files to achieve the desired affect.

Upvotes: 5

Thomas Li
Thomas Li

Reputation: 3338

I don't think it is possible because it is getting the information from the registry: @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones"

Upvotes: 0

Peter
Peter

Reputation: 3956

You can get the localized name by using TimeZoneInfo.DisplayName

http://msdn.microsoft.com/en-us/library/system.timezoneinfo.displayname.aspx

Upvotes: -1

Related Questions