New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CultureInfo didn't set appropriately #5543
Comments
Are you setting the culture manually? Or expect it to be set by the browser? That last option isn't supported (yet) I believe. |
@floreseken I didn't set it. I am writing a demo to invoke a webapi, output start time and end time, and then see it incorrect (not beijing time), finally I found this problem is caused by CultureInfo. I tried set the CultureInfo in Program.cs, but the DateTime.Now still incorrect.
|
Probably it is only partially related, but time zone doesn't work: #983 |
I too bumped into this, but in my case it was low priority, and I had not looked to isolate and check the issue before raising a ticket. |
My answer was wrong. It appears to be a limitation in Mono-Wasm. |
FYI I tried to setup the current culture like this
The console logs are ok but if I try to display a date on the html, the result is not ok. |
@RemiBou yes, this is because globalization data is missing (it is expected to be provided by the system but emscripten does not so mono has no data) |
Not sure about that because my console.log shows the data localized and
shows it localized as well. I think it's more a Thread context problem, I'll try setting current culture in various place (Program / OnInit/ OnParameterSet are not working so far) to see if I can fix this. |
@RemiBou oh hm right, did you try setting |
Nope I'll try that
Le jeu. 16 août 2018 20:12, Robin Sue <notifications@github.com> a écrit :
… @RemiBou <https://github.com/RemiBou> oh hm right, did you try setting
CurrentUiCulture?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://github.com/aspnet/Blazor/issues/1056#issuecomment-413636512>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABS8etGBtzkfqYOIOKkiMrr72hUn6ru2ks5uRbYGgaJpZM4U8eEl>
.
|
This works for me in index.cshtml file <div>@date</div> @functions
{
DateTime date;
protected override void OnInit()
{
var plCulture = System.Globalization.CultureInfo.GetCultureInfo("pl-PL");
System.Globalization.CultureInfo.CurrentCulture = plCulture;
// This is not necessary
//System.Globalization.CultureInfo.CurrentUICulture = plCulture;
date = DateTime.Now;
Console.WriteLine(date.ToString("yyyy MMM ddd"));
} In console I see I have to set |
It looks that problem is related to async method. I changed protected override async Task OnInitAsync()
{
string[] languages = await Js.NavigatorLanguages();
string firstLanguage = languages.FirstOrDefault() ?? "pl-PL";
Console.WriteLine(firstLanguage);
CultureInfo browserCulture = System.Globalization.CultureInfo.GetCultureInfo(firstLanguage);
System.Globalization.CultureInfo.CurrentCulture = browserCulture;
System.Globalization.CultureInfo.CurrentUICulture = browserCulture;
date = DateTime.Now;
Console.WriteLine(date.ToString("yyyy MMM ddd"));
} and now I see correct values (culture) in console and wrong culture format in HTML page. |
I have just changed |
code after await can be executed in an other thread, so we have to find the place where the parent thread is so all the child thread will get this when created (I think that's how it works in ASPNET Core). This kind of settings should be done on a centralized place instead of per page. Maybe setting DefaultThreadCurrentCulture in Program.Main might solve it. I tried to find the thread management code in monowasmsbut the code base is too hard to read for my level. |
Yay I found it !! In Program.Main if you set DefaultThreadCurrentCulture it's used by the rendering engine :) :) The problem now is lang switching, I think the best strategy would be store it in local storage and reload the full app. Blog Post :https://remibou.github.io/Localizing-DateTime-in-Blazor/ |
|
Sorry, my fault. I pasted Main method body from https://github.com/aspnet/Blazor/issues/1287#issuecomment-412037803 into Blazor hosted project. Steve's example is from standalone version and it uses direct file download: httpClient.GetStringAsync("sample-data/weather.json") which of course doesn't work in hosted project where WebAPI call is needed. |
@Andrzej-W it's weirf because I'm using Blazor 0.5.1, I'll try to migrate it to a more recent model |
Andrzej-W's solution worked for me, but instead of index.cshtml I put it in MainLayout.cshtml. That way even if user doesn't browse the main page it still works. |
Thanks for contacting us. We believe that the question you've raised have been answered. If you still feel a need to continue the discussion, feel free to reopen it and add your comments. |
In a Blazor cshtml, add "@CultureInfo.CurrentCulture.DisplayName", and then get "Invariant Language (Invariant Country)" while I expect "中文(简体,中国)".
DateTime.Now is not correct also.
The text was updated successfully, but these errors were encountered: