5

I am currently trying to localize my windowsphone Time App for a few countries. I am using Noda Time as it was extremely easy for a newbie. The problem I am facing that all the Timezone Id's are in standard English and I am searching for a way to get those Id's converted to Local Language strings.

One way would be too make Localized strings for each ID in every language. But it seems to be Highly inefficient as there are 500 timezones. Please suggest a way for me to get directly the TimeZone ID's converted into Local Language in a less time consuming way.

My code:

var now = Instant.FromDateTimeUtc(DateTime.UtcNow);
var tzdb = DateTimeZoneProviders.Tzdb;

var list = from id in tzdb.Ids
           where id.Contains("/") && !id.StartsWith("etc", StringComparison.OrdinalIgnoreCase)
           let tz = tzdb[id]
           let offset = tz.GetUtcOffset(now)
           orderby offset, id
           select new
           {
               DisplayValue = string.Format("(UTC{0}) {1}  {2}  ", offset.ToString("+HH:mm", null), now.WithOffset(offset).TimeOfDay.ToString("hh:mm tt",null),id)
           };
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
aman shivhare
  • 334
  • 2
  • 13
  • Just a quick note that I do hope to include CLDR data in Noda Time at some point. It would be useful for more than just time zone names. It's not clear to me yet whether we should adopt just the time zone bits for now, and then come back to the rest later, or wait to get everything in one go... – Jon Skeet Dec 23 '14 at 08:03
  • thank you @JonSkeet for looking into this matter.As per my view all in one go seems to be a better option than bits since each programmer has its own needs.So helping one and keeping the other waiting seems a bit unfair and we already have the TimeZoneNames Library to help us currently in getting the job done. – aman shivhare Dec 23 '14 at 09:36

1 Answers1

5

This isn't a current feature of Noda Time, so you will need to get the data elsewhere.

Localizations for time zones (and other items) are best found within the Unicode CLDR project. You can write code to parse the various XML files included with the CLDR releases. You'll also need to understand how the data is represented, and several edge cases.

Or, you can use the implementation that I've already completed.

Install the TimeZoneNames Nuget package:

PM>  Install-Package TimeZoneNames

Then you can easily resolve the time zone names to a localized display value:

// example input values
var names = TZNames.GetNamesForTimeZone("America/Los_Angeles", "en-US");

// example output values
Assert.Equal("Pacific Time", names.Generic);
Assert.Equal("Pacific Standard Time", names.Standard);
Assert.Equal("Pacific Daylight Time", names.Daylight);

This works for non-English locales as well. It will produce valid text in any language, provided that the data exists in the CLDR.

The same library can also be used to get a list of time zone ids for a particular country. This can be used to implement a two-dropdown selection list, where first you pick the country, and then you pick the time zone within the country.

var zones = TZNames.GetTimeZoneIdsForCountry("US");

You can take a look at the project's unit tests for further examples.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • 1
    thank you so much @Matt for introducing me to this wonderful library.. U solved all my Problems. Thank you so much – aman shivhare Dec 22 '14 at 09:15
  • Hey @Matt Johnson now that I am running Certification Kit on my Packages I am getting TimeZoneNames.dll built in debug mode error.I tried my best but could not get it to release mode – aman shivhare Dec 26 '14 at 21:27
  • Thanks. Fixed in the latest version. [See here for details](https://github.com/mj1856/TimeZoneNames/issues/1). Also, please in the future, report issues directly on GitHub. Thanks. – Matt Johnson-Pint Dec 26 '14 at 23:05