2016-03-25 39 views
4

以下代碼託管在OS語言爲日語的系統中,我正在以日語顯示時區的顯示。我將web.config中的全球化設置設置爲英文,但沒有運氣。有什麼方法可以用英文文本顯示?系統時區顯示英文文本

<select id="timezone" name="timezone"> 
    @foreach (var timeZone in TimeZoneInfo.GetSystemTimeZones()) { 
     <option value="@timeZone.Id">@timeZone.DisplayName</option> 
    } 
</select> 

截圖的用戶界面

enter image description here

+3

可能是一個連接到這個問題:http://stackoverflow.com/questions/13163241/getting-system-timezones-in-different-languages – greenshade

+0

我有權在託管環境中承載我的網站,並沒有任何安裝任何語言包的權限。 – Hoque

+0

是的,它依賴於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/)的解決方案。 –

回答

2

您可以使用字典,但你不得不如果主機的操作系統時區已更新,則維護它(在時區改變的情況下......並且他們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 IANAwikipedia,如果你是高達datamine它。

還有從Unicode,它在顯示時區的用戶(不考慮大量的冗餘時區)一般更適合,並提供其(便於解析)的XML文件中的許多語言

當然和Common Locale Data Repository,有也可以通過Jon Skeet(et al。)得到,你可能想檢查一下,這也是Noda Time。我想認爲它維護自己的奧爾森時區數據庫,但我並不是所有這一點確定。

0

一個解決方案是使用一個字符串表用英語與你的價值觀。你可以通過服務器端訪問字符串表(如上所述)或客戶端,並使用其中的值填充下拉列表。

樣品JSON文件:

{ 
"timezones":{ 
    "en":{ 
     "standard":[ 
      {"zone":"(UTC-5:00)", 
      "title":"Eastern Standard Time"}, 
      {"zone":"(UTC-6:00)", 
      "title":"Central Standard Time"}, 
      {"zone":"(UTC-7:00)", 
      "title":"Mountain Standard Time"}, 
      {"zone":"(UTC-7:00)", 
      "title":"Phoenix Standard Time"}, 
      {"zone":"(UTC-8:00)", 
      "title":"Pacific Standard Time"} 
     ], 
     "daylightSavings":[ 
      {"zone":"(UTC-4:00)", 
      "title":"Eastern Standard Time"}, 
      {"zone":"(UTC-5:00)", 
      "title":"Central Standard Time"}, 
      {"zone":"(UTC-6:00)", 
      "title":"Mountain Standard Time"}, 
      {"zone":"(UTC-7:00)", 
      "title":"Pacific Standard Time"} 
     ] 
    } 
} 

}

樣品(客戶端)執行

<!DOCTYPE html> 
<html> 
    <head> 
     <script src="jquery-2.0.3.js" ></script> 
     <script type="text/javascript"> 
      var inDaylightSavings = false; 

      $(document).ready(function() { 
       // TODO - set in daylight savings. 

       // Load data from local json file 
       $.getJSON("tz.json", function(obj) { 
        var targetDataset = obj.timezones.en.standard; 
        // Adjust for U.S. daylight savings 
        if(inDaylightSavings){ 
         targetDataset = obj.timezones.en.daylightSavings; 
        } 

        // Update the dropdown 
        $.each(targetDataset, function(key, value){ 
         $("#selectTimezones").append("<option>" + value.zone + " " + value.title + "</option>"); 
        }); 
       }); 
      }); 
     </script> 
    </head> 
    <body> 
     <label>Select Timezone</label> 
     <select name="timezones" id="selectTimezones"> 
      <option value="None" selected="Selected">- not selected -</option> 
     </select> 
    </body> 
</html> 

Result

相關問題