如何将标准的Noda时区Id从英语转换为本地化语言? [英] How to convert the standard Noda Timezone Id's from English to Localized Language?

查看:342
本文介绍了如何将标准的Noda时区Id从英语转换为本地化语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在试图本地化我的WindowsPhone应用程序的时间为少数几个国家。我使用的野田佳彦时间,因为它是一个新手非常容易。这个问题我面对所有的时区ID是标准的英语,我正在寻找一种方式来获得这些标识的转换为本地语言的字符串。



的一种方法是也使本地化的字符串为每一种语言每个ID。但它似乎是非常低效的,因为有500的时区。请建议对我来说,直接将时区的ID转换成当地语言以节省时间的方式的方式



我的代码:

  VAR现在= Instant.FromDateTimeUtc(DateTime.UtcNow); 
VAR TZDB = DateTimeZoneProviders.Tzdb;

无功名单=从tzdb.Ids
id其中id.Contains(/)及和放大器; !id.StartsWith(等等,StringComparison.OrdinalIgnoreCase)
让TZ = TZDB [ID]
让利偏移量= tz.GetUtcOffset(现在)
排序依据抵消,ID
选择新的
{
DisplayValue =的String.Format((UTC {0}){1} {2},offset.ToString(+ HH:MM,NULL),now.WithOffset(偏移).TimeOfDay.ToString(HH:MM TT,NULL),ID)
};


解决方案

这是不是野田时的电流特性, 。所以你需要在其他地方获得数据



本地化时区(以及其他项目)最好的的Unicode CLDR 项目。您可以编写代码来解析包含在CLDR版本不同的XML文件。您还需要了解数据是如何表示的,和一些边缘情况。



或者,你可以使用的我已经完成实施



安装的 TimeZoneNames 的NuGet包:



<预类=郎-PS prettyprint-覆盖> PM>安装封装TimeZoneNames



然后就可以轻松解决的时区名称本地化的显示值:



<预类=郎CSHARP prettyprint-覆盖> //例如输入值
变种名称= TZNames.GetNamesForTimeZone(美国/洛杉矶 EN-US);

//例子输出值
Assert.Equal(太平洋时间,names.Generic);
Assert.Equal(太平洋标准时间,names.Standard);
Assert.Equal(太平洋夏令时,names.Daylight);

这适用于非英语语言环境也是如此。会产生任何语言有效的文本,其前提是在CLDR存在数据



的相同的库也可以被用于获得一个时间带id的列表特定国家。这可以被用来实现两下拉选择列表,在这里首先你选择的国家,然后你选择的国家内的时区。



<预类=朗CSHARP prettyprint-覆盖> VAR区= TZNames.GetTimeZoneIdsForCountry(US);

您可以看看的项目的单元测试的进一步的例子。


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

解决方案

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.

这篇关于如何将标准的Noda时区Id从英语转换为本地化语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆