您可以使用字典,但你不得不如果主機的操作系統時區已更新,則維護它(在時區改變的情況下......並且他們DO改變)。如果你不控制主人,這可能是一個真正的痛苦。
我已爲你(你可以檢查它in this fiddle ......不顧一切的樣板代碼,它在dotnetfiddle爲MVC默認的代碼,我只是改變了看法):
@functions{
string GetTimeZonesInEnglish(string timeZoneId, string defaultDisplayName)
{
var dict = new Dictionary<string, string>()
{
{ "Dateline Standard Time"," (UTC-12:00) International Date Line West" },
{ "UTC-11"," (UTC-11:00) Coordinated Universal Time-11" },
{ "Hawaiian Standard Time"," (UTC-10:00) Hawaii" },
{ "Alaskan Standard Time"," (UTC-09:00) Alaska" },
{ "Pacific Standard Time (Mexico)"," (UTC-08:00) Baja California" },
{ "Pacific Standard Time"," (UTC-08:00) Pacific Time (US & Canada)" },
{ "US Mountain Standard Time"," (UTC-07:00) Arizona" },
{ "Mountain Standard Time (Mexico)"," (UTC-07:00) Chihuahua, La Paz, Mazatlan" },
{ "Mountain Standard Time"," (UTC-07:00) Mountain Time (US & Canada)" },
{ "Central America Standard Time"," (UTC-06:00) Central America" },
{ "Central Standard Time"," (UTC-06:00) Central Time (US & Canada)" },
{ "Central Standard Time (Mexico)"," (UTC-06:00) Guadalajara, Mexico City, Monterrey" },
{ "Canada Central Standard Time"," (UTC-06:00) Saskatchewan" },
{ "SA Pacific Standard Time"," (UTC-05:00) Bogota, Lima, Quito, Rio Branco" },
{ "Eastern Standard Time (Mexico)"," (UTC-05:00) Chetumal" },
{ "Eastern Standard Time"," (UTC-05:00) Eastern Time (US & Canada)" },
{ "US Eastern Standard Time"," (UTC-05:00) Indiana (East)" },
{ "Venezuela Standard Time"," (UTC-04:30) Caracas" },
{ "Paraguay Standard Time"," (UTC-04:00) Asuncion" },
{ "Atlantic Standard Time"," (UTC-04:00) Atlantic Time (Canada)" },
{ "Central Brazilian Standard Time"," (UTC-04:00) Cuiaba" },
{ "SA Western Standard Time"," (UTC-04:00) Georgetown, La Paz, Manaus, San Juan" },
{ "Newfoundland Standard Time"," (UTC-03:30) Newfoundland" },
{ "E. South America Standard Time"," (UTC-03:00) Brasilia" },
{ "SA Eastern Standard Time"," (UTC-03:00) Cayenne, Fortaleza" },
{ "Argentina Standard Time"," (UTC-03:00) City of Buenos Aires" },
{ "Greenland Standard Time"," (UTC-03:00) Greenland" },
{ "Montevideo Standard Time"," (UTC-03:00) Montevideo" },
{ "Bahia Standard Time"," (UTC-03:00) Salvador" },
{ "Pacific SA Standard Time"," (UTC-03:00) Santiago" },
{ "UTC-02"," (UTC-02:00) Coordinated Universal Time-02" },
{ "Mid-Atlantic Standard Time"," (UTC-02:00) Mid-Atlantic - Old" },
{ "Azores Standard Time"," (UTC-01:00) Azores" },
{ "Cape Verde Standard Time"," (UTC-01:00) Cabo Verde Is." },
{ "Morocco Standard Time"," (UTC) Casablanca" },
{ "UTC"," (UTC) Coordinated Universal Time" },
{ "GMT Standard Time"," (UTC) Dublin, Edinburgh, Lisbon, London" },
{ "Greenwich Standard Time"," (UTC) Monrovia, Reykjavik" },
{ "W. Europe Standard Time"," (UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna" },
{ "Central Europe Standard Time"," (UTC+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague" },
{ "Romance Standard Time"," (UTC+01:00) Brussels, Copenhagen, Madrid, Paris" },
{ "Central European Standard Time"," (UTC+01:00) Sarajevo, Skopje, Warsaw, Zagreb" },
{ "W. Central Africa Standard Time"," (UTC+01:00) West Central Africa" },
{ "Namibia Standard Time"," (UTC+01:00) Windhoek" },
{ "Jordan Standard Time"," (UTC+02:00) Amman" },
{ "GTB Standard Time"," (UTC+02:00) Athens, Bucharest" },
{ "Middle East Standard Time"," (UTC+02:00) Beirut" },
{ "Egypt Standard Time"," (UTC+02:00) Cairo" },
{ "Syria Standard Time"," (UTC+02:00) Damascus" },
{ "E. Europe Standard Time"," (UTC+02:00) E. Europe" },
{ "South Africa Standard Time"," (UTC+02:00) Harare, Pretoria" },
{ "FLE Standard Time"," (UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius" },
{ "Turkey Standard Time"," (UTC+02:00) Istanbul" },
{ "Israel Standard Time"," (UTC+02:00) Jerusalem" },
{ "Kaliningrad Standard Time"," (UTC+02:00) Kaliningrad (RTZ 1)" },
{ "Libya Standard Time"," (UTC+02:00) Tripoli" },
{ "Arabic Standard Time"," (UTC+03:00) Baghdad" },
{ "Arab Standard Time"," (UTC+03:00) Kuwait, Riyadh" },
{ "Belarus Standard Time"," (UTC+03:00) Minsk" },
{ "Russian Standard Time"," (UTC+03:00) Moscow, St. Petersburg, Volgograd (RTZ 2)" },
{ "E. Africa Standard Time"," (UTC+03:00) Nairobi" },
{ "Iran Standard Time"," (UTC+03:30) Tehran" },
{ "Arabian Standard Time"," (UTC+04:00) Abu Dhabi, Muscat" },
{ "Azerbaijan Standard Time"," (UTC+04:00) Baku" },
{ "Russia Time Zone 3"," (UTC+04:00) Izhevsk, Samara (RTZ 3)" },
{ "Mauritius Standard Time"," (UTC+04:00) Port Louis" },
{ "Georgian Standard Time"," (UTC+04:00) Tbilisi" },
{ "Caucasus Standard Time"," (UTC+04:00) Yerevan" },
{ "Afghanistan Standard Time"," (UTC+04:30) Kabul" },
{ "West Asia Standard Time"," (UTC+05:00) Ashgabat, Tashkent" },
{ "Ekaterinburg Standard Time"," (UTC+05:00) Ekaterinburg (RTZ 4)" },
{ "Pakistan Standard Time"," (UTC+05:00) Islamabad, Karachi" },
{ "India Standard Time"," (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi" },
{ "Sri Lanka Standard Time"," (UTC+05:30) Sri Jayawardenepura" },
{ "Nepal Standard Time"," (UTC+05:45) Kathmandu" },
{ "Central Asia Standard Time"," (UTC+06:00) Astana" },
{ "Bangladesh Standard Time"," (UTC+06:00) Dhaka" },
{ "N. Central Asia Standard Time"," (UTC+06:00) Novosibirsk (RTZ 5)" },
{ "Myanmar Standard Time"," (UTC+06:30) Yangon (Rangoon)" },
{ "SE Asia Standard Time"," (UTC+07:00) Bangkok, Hanoi, Jakarta" },
{ "North Asia Standard Time"," (UTC+07:00) Krasnoyarsk (RTZ 6)" },
{ "China Standard Time"," (UTC+08:00) Beijing, Chongqing, Hong Kong, Urumqi" },
{ "North Asia East Standard Time"," (UTC+08:00) Irkutsk (RTZ 7)" },
{ "Singapore Standard Time"," (UTC+08:00) Kuala Lumpur, Singapore" },
{ "W. Australia Standard Time"," (UTC+08:00) Perth" },
{ "Taipei Standard Time"," (UTC+08:00) Taipei" },
{ "Ulaanbaatar Standard Time"," (UTC+08:00) Ulaanbaatar" },
{ "North Korea Standard Time"," (UTC+08:30) Pyongyang" },
{ "Tokyo Standard Time"," (UTC+09:00) Osaka, Sapporo, Tokyo" },
{ "Korea Standard Time"," (UTC+09:00) Seoul" },
{ "Yakutsk Standard Time"," (UTC+09:00) Yakutsk (RTZ 8)" },
{ "Cen. Australia Standard Time"," (UTC+09:30) Adelaide" },
{ "AUS Central Standard Time"," (UTC+09:30) Darwin" },
{ "E. Australia Standard Time"," (UTC+10:00) Brisbane" },
{ "AUS Eastern Standard Time"," (UTC+10:00) Canberra, Melbourne, Sydney" },
{ "West Pacific Standard Time"," (UTC+10:00) Guam, Port Moresby" },
{ "Tasmania Standard Time"," (UTC+10:00) Hobart" },
{ "Magadan Standard Time"," (UTC+10:00) Magadan" },
{ "Vladivostok Standard Time"," (UTC+10:00) Vladivostok, Magadan (RTZ 9)" },
{ "Russia Time Zone 10"," (UTC+11:00) Chokurdakh (RTZ 10)" },
{ "Central Pacific Standard Time"," (UTC+11:00) Solomon Is., New Caledonia" },
{ "Russia Time Zone 11"," (UTC+12:00) Anadyr, Petropavlovsk-Kamchatsky (RTZ 11)" },
{ "New Zealand Standard Time"," (UTC+12:00) Auckland, Wellington" },
{ "UTC+12"," (UTC+12:00) Coordinated Universal Time+12" },
{ "Fiji Standard Time"," (UTC+12:00) Fiji" },
{ "Kamchatka Standard Time"," (UTC+12:00) Petropavlovsk-Kamchatsky - Old" },
{ "Tonga Standard Time"," (UTC+13:00) Nuku'alofa" },
{ "Samoa Standard Time"," (UTC+13:00) Samoa" },
{ "Line Islands Standard Time"," (UTC+14:00) Kiritimati Island" },
};
string retVal;
if(dict.TryGetValue(timeZoneId, out retVal))
return retVal;
return defaultDisplayName;
}
}
而且然後:
<select id="timezone" name="timezone">
@foreach (var timeZone in TimeZoneInfo.GetSystemTimeZones()) {
<option value="@timeZone.Id">@GetTimeZonesInEnglish(timeZone.Id, timeZone.DisplayName)</option>
}
</select>
我從一個英語機做了一些簡單的控制檯工具來輸出「字典初始化格式」的字符串,以防萬一要更新他們:
using System;
public class Program
{
public static void Main()
{
foreach (var timeZone in TimeZoneInfo.GetSystemTimeZones())
Console.WriteLine("{ \"" + timeZone.Id + "\",\" " + timeZone.DisplayName + "\" },");
}
}
檢查出來in this fiddle
這一切都表示,由於時區顯示使用主機的操作系統的註冊表中找到名字,且依賴於語言的......而我上面說「作品」,如果他們從來沒有更新,我會說這不是處理它的最好方法。
我會親自處理我自己的時區,如果我是不是在我的控制主機,在我的數據庫或任何...你可以找到一個更新的時區數據庫in the IANA或wikipedia,如果你是高達datamine它。
還有從Unicode,它在顯示時區的用戶(不考慮大量的冗餘時區)一般更適合,並提供其(便於解析)的XML文件中的許多語言
當然和Common Locale Data Repository,有也可以通過Jon Skeet(et al。)得到,你可能想檢查一下,這也是Noda Time。我想認爲它維護自己的奧爾森時區數據庫,但我並不是所有這一點確定。
可能是一個連接到這個問題:http://stackoverflow.com/questions/13163241/getting-system-timezones-in-different-languages – greenshade
我有權在託管環境中承載我的網站,並沒有任何安裝任何語言包的權限。 – Hoque
是的,它依賴於OS本地化,而不是.NET的全球化功能。這已在[時區標記wiki](http://stackoverflow.com/tags/timezone/info)中記錄。此外,請參閱[我的博客關於此主題](http://codeofmatt.com/2014/12/26/localized-time-zone-names-in-net/)並嘗試[此庫](http:// mj1856.github.io/TimeZoneNames/)的解決方案。 –