How to convert the standard Noda Timezone Id's from English to Localized Language?

╄→гoц情女王★ 提交于 2019-12-06 02:33:11

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/27593931/how-to-convert-the-standard-noda-timezone-ids-from-english-to-localized-languag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!