即使我在美國,我也暫時將瀏覽器語言和區域設定為英語 - 英國。我已洗掉“en-US”,現在將“en-GB”作為我唯一的語言偏好。
在我的 Blazor WebAssembly 站點的一個組件上,我有一個回傳字串的屬性:myDate.ToString("d") System.Globalization.CultureInfo.CurrentCulture.DisplayName;在我的頁面上呈現為6/27/2020en (GB),其中 myDate 是設定為 2020 年 6 月 27 日 00:00:00.000 的 DateTime。我的 Blazor 站點app.UseRequestLocalization(...)設定了中間件。
不應該以英國格式顯示日期,即27/06/2020?我只能猜測 ShortDatePattern 沒有從 CultureInfo.CurrentCulture 正確設定。會是什么呢?
更新:所有 WebAssembly 組件輸出都向我表明,DateTimeFormatInfo.CurrentInfo.ShortDatePattern == "M/d/yyyy"即使CultureInfo.CurrentCulture.Name == "en-GB". 為什么會這樣?是什么從文化中設定了 ShortDatePattern,我可以“重新初始化”它嗎?
顯式呼叫myDate.ToString("d", System.Globalization.CultureInfo.GetCultureInfo("en-GB"));仍然奇怪地輸出M/d/yyyy格式(美國格式)。為什么會這樣?
更新 2:我創建了一個最小示例:File-New Project、Blazor Web Assembly、.NET 5 ASP.NET Core 托管。我用以下內容替換了 App.Razor:
<div> current culture @(System.Globalization.CultureInfo.CurrentCulture.Name) </div>
<div> current date format @(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern) </div>
<div> today @DateTime.Now.ToString("d") </div>
這導致(出乎意料地):

Firefox 瀏覽器設定(Edge 和 Chrome 類似并顯示相同的問題):

這導致發送 -- 這Accept-Language: en-GB,en-US;q=0.7,en;q=0.3似乎是“首選 en-GB”的正確方法,并導致 CultureInfo.CurrentCulture 具有正確的值。
I've tried it with Microsoft.AspNetCore.Components.WebAssembly.* nuget packages version 5.0.9, 5.0.11 - both show the same incorrect result.
UPDATE 3: The same minimal project in 6.0.0-rc.1 works and gives the right date format! Is this really a Blazor 5 bug that they never fixed?
uj5u.com熱心網友回復:
沒有重現。
我在 .net5 和 .net6 預覽版中都使用了以下代碼,并且在兩個版本中,輸出都由瀏覽器中設定的頂級語言正確確定(我使用了 Chrome、Firefox 和 Edge)。
<p>@theData</p>
@code
{
string theData => $"{DateTime.Now} {System.Globalization.CultureInfo.CurrentCulture.DisplayName}";
}
所以,如果你仍然有問題:創建一個 mre。其他事情可能正在發生。
uj5u.com熱心網友回復:
對于 .NET 6 ,這似乎是固定的。
對于 .NET 5,我找不到根本原因,因此我需要撰寫此解決方法,需要時需要顯式呼叫。顯然,如果我們能找到一個更好的解決方案來允許按預期使用 DateTime.ToString,這是不可取的。
/// <summary>
/// Returns a short date-only string from a date/time value, based on the user's current culture.
/// </summary>
public static string ToLocalShortDate(this DateTime value)
{
// this is needed because I can't get localization to work -- see https://stackoverflow.com/q/69542125/7453
// (if we can fix, better to use DateTime.ToString("d"))
string format;
// countries taken from https://en.wikipedia.org/wiki/Date_format_by_country
if (CurrentCulture.Name.EndsWithAny("US", "CA", "ZA", "KE", "GH", "en"))
format = "MM/dd/yyyy";
else if (CurrentCulture.Name.EndsWithAny("CN", "JP", "KR", "KP", "TW", "HU", "MN", "LT", "BT"))
format = "yyyy-MM-dd";
else format = "dd/MM/yyyy";
return value.ToString(format);
}
/// <summary>
/// Returns true if and only if a string ends with any of some strings.
/// The value will not match a null reference.
/// </summary>
public static bool EndsWithAny(this string value, params string[] allowedValues) =>
allowedValues != null && value != null && allowedValues.Any(s => CurrentCulture.Name.EndsWith(s));
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/313661.html
