本文介绍了如何在Blazor(服务器)中修改当前的文化日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET Core Blazor全球化和本地化状态:

该说法是正确的,但问题在于,必须在使用文化之前(或可能在每次DOM刷新时)设置区域性.

The statement is true, but the problem is that, the culture has to be set just before it is used (or maybe each time the DOM is refreshed).

为演示起见,我将使用标准的blazor计数器应用程序.让我们修改Counter.razor

For demonstration I will use standard blazor counter application. Let's modify Counter.razor

@page "/counter"
@using System.Globalization;

<h1>Counter</h1>
<input type="text" @bind="currentDate" />

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private DateTime currentDate = DateTime.Now;
    private int currentCount = 0;

    private void IncrementCount() {
        if (currentCount < 2) Utils.SetCCDateFormat();
        currentCount++;
    }

    public class Utils {
        public static void SetCCDateFormat() {
            var cc = CultureInfo.CurrentCulture.Clone() as CultureInfo;
            cc.DateTimeFormat.ShortDatePattern = "dd-yyyy-m";
            CultureInfo.CurrentCulture = cc;
            CultureInfo.CurrentUICulture = cc;
        }
    }

} 

结果是:

  • 首次呈现页面时,文本框包含格式化的日期根据服务器的默认区域性.
  • 第一次和第二次按下按钮时,日期格式为dd-yyyy-m
  • when the page is first rendered the text box contain date formattedby server default culture.
  • when the button is pressed first and second time the date format is dd-yyyy-m

我试图修改OnAfterRenderOnInitialized中的日期,但没有成功.我发现,只有可用的解决方案才能根据剃须刀标记设置格式.

I attempted to modify the date in OnAfterRender, OnInitialized without success. Only usable solution, I have found, is setting the format on the begging of razor markup.

@{Utils.SetCCDateFormat();}

有没有办法修改CurrentCulture以使其在blazor电路中持久存在?

观察到的行为是正确的还是错误?

Is the observed behavior correct or is it a bug?

修改

到目前为止我发现的东西

可以在创建blankor端点之前在中间件中设置区域性属性(CultureInfo.CurrentCulture),并且在整个电路生命周期中这些更改都是持久的.当我们在组件生命周期方法中修改CurrentCulture时,更改只是暂时的(直到方法结束).

It is possible to set culture properties (CultureInfo.CurrentCulture) in a middleware before the blazor endpoint is created and the changes are persistent for the circuit lifetime. When we modify CurrentCulture in component lifecycle methods the change is only temporary (till the end of the method).

我对这个问题的理解是

  • 创建电路时,它将当前区域性存储在某个地方
  • 服务器的线程数有限
  • 在需要时将线程分配给电路,并且当前区域性由开始时存储的内容设置
  • 可以修改CurrentCulture,但这不会影响设置存储,因此,当调用另一个事件方法(另一个线程)时,将使用原始区域性.
  • When a circuit is created it stores the current culture somewhere
  • A server has a limited number of threads
  • A thread is assigned to a circuit when required and the current culture is set by what was stored at the beginning
  • It is possible to modify the CurrentCulture, but this doesn't influence the setting storage and so when another event method is called (other thread) the original culture is used.

所以看来问题是:如何在创建电路文化设置后对其进行修改?

也许不可能,并且有必要进行完全刷新(通过导航再次启动请求),并使用中间件设置修改后的区域性.文化存储的存在只是我的推测,我没有任何支持它的参考.

Maybe it is not possible and it is necessary do full refresh (start a request again with navigation) and use a middleware to set a modified culture. A culture storage existence is only my conjecture and I don't have any reference to support it.

非常感谢Tyeth和Ashiquzzaman的帮助,但我并没有将他们的尝试作为答案.

Many thanks to Tyeth and Ashiquzzaman for help but I am not taking their attempts as the answer.

推荐答案

Ashiquzzaman的答案的第二部分(在OR之后)提示采取正确的路径.

The second part of Ashiquzzaman's answer (after OR) hints at the correct path to take.

内置的ASP.Net Core本地化中间件将是您最好的新朋友: https://docs. microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1#localization-middleware

The built in ASP.Net Core localisation middleware will be your new best friend:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1#localization-middleware

您可以依靠默认包含的CookieRequestCultureProvider,并通过为每个用户设置登录用户的cookie(我建议登录后,因为您将可以访问用户存储的"偏好设置),因此您可以覆盖默认浏览器要求的语言(或系统默认设置).

You can rely on the default included CookieRequestCultureProvider, and by setting a cookie for your logged in users on a per user basis (I suggest upon login as you will have access to the Users stored preferences then) you can override the default browser requested language (or system default as a last resort).

RequestLocalizationProviders的列出顺序在发布的链接上,建议查询字符串可以覆盖cookie,而cookie可以覆盖浏览器首选项:

The listed order of RequestLocalizationProviders is on that link posted and suggests the querystring can override the cookie which can override the browser preference:

https://docs .microsoft.com/en-us/aspnet/core/fundamentals/localization?view = aspnetcore-3.1#cookierequestcultureprovider

  1. QueryStringRequestCultureProvider
  2. CookieRequestCultureProvider **
  3. AcceptLanguageHeaderRequestCultureProvider

cookie格式为c =%LANGCODE%| uic =%LANGCODE%,其中c为文化,而uic为UICulture,例如:

The cookie format is c=%LANGCODE%|uic=%LANGCODE%, where c is Culture and uic is UICulture, for example:

c=en-UK|uic=en-US

您一定应该阅读Ashiquzzaman建议的有关全球化和本地化的热门部分: https://docs.microsoft. com/en-us/aspnet/core/blazor/globalization-localization?view = aspnetcore-3.1

You should definitely read the blazor section on globalisation and localisation as suggested by Ashiquzzaman:https://docs.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-3.1

这篇关于如何在Blazor(服务器)中修改当前的文化日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 17:03