Skip to content
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

Closed
chenyj796 opened this issue Jun 29, 2018 · 20 comments
Closed

CultureInfo didn't set appropriately #5543

chenyj796 opened this issue Jun 29, 2018 · 20 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@chenyj796
Copy link

In a Blazor cshtml, add "@CultureInfo.CurrentCulture.DisplayName", and then get "Invariant Language (Invariant Country)" while I expect "中文(简体,中国)".
DateTime.Now is not correct also.

@floreseken
Copy link

floreseken commented Jun 29, 2018

Are you setting the culture manually? Or expect it to be set by the browser?

That last option isn't supported (yet) I believe.

@chenyj796
Copy link
Author

chenyj796 commented Jun 29, 2018

@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.

        // set culture in Program.cs
        var culture=CultureInfo.GetCultureInfo("zh-cn");
        CultureInfo.CurrentCulture = culture;
        CultureInfo.CurrentUICulture = culture;
        //////////////////

        var serviceProvider = new BrowserServiceProvider(services =>
        {
            // Add any custom services here
        });
        ......

@Andrzej-W
Copy link

Probably it is only partially related, but time zone doesn't work: #983

@simonziegler
Copy link

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.

@chucker
Copy link

chucker commented Jul 1, 2018

    // set culture in Program.cs
    var culture=CultureInfo.GetCultureInfo("zh-cn");

I don't believe Program.cs executes in the same context (or even the same runtime) as your Blazor page. It's used to initialize the behavior of Blazor, not your site.

My answer was wrong. It appears to be a limitation in Mono-Wasm.

@RemiBou
Copy link
Contributor

RemiBou commented Aug 16, 2018

FYI I tried to setup the current culture like this

 var languages = await JSRuntime.Current.InvokeAsync<string[]>("navigatorLanguages");
         System.Globalization.CultureInfo.CurrentCulture = languages.Select(c => System.Globalization.CultureInfo.GetCultureInfo(c)).FirstOrDefault();
         await Console.Write(DateTime.Now.ToString("yyyy MMM ddd"));

The console logs are ok but if I try to display a date on the html, the result is not ok.

@Suchiman
Copy link
Contributor

@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)

@RemiBou
Copy link
Contributor

RemiBou commented Aug 16, 2018

Not sure about that because my console.log shows the data localized and

@entity.CreatedOn.ToString("MMMM", new System.Globalization.CultureInfo("fr"))

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.

@Suchiman
Copy link
Contributor

@RemiBou oh hm right, did you try setting CurrentUiCulture?

@RemiBou
Copy link
Contributor

RemiBou commented Aug 16, 2018 via email

@Andrzej-W
Copy link

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 2018 sie czw. (correct Polish names). On web page I see 16.08.2018 22:38:18 which is correct default datetime format for Polish language. I can change "pl-PL" to "en-US" and it also works as expected. For "fr" I see 2018 août jeu and 16/08/2018 22:46:21 respectively.

I have to set CurrentCulture in OnInit in index.chtml. It doesn't work in Program.Main.

@Andrzej-W
Copy link

It looks that problem is related to async method. I changed OnInit to OnInitAsync

    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.

@Andrzej-W
Copy link

I have just changed CurrentCulture to System.Globalization.CultureInfo.DefaultThreadCurrentCulture and it doesn't work either.

@RemiBou
Copy link
Contributor

RemiBou commented Aug 17, 2018

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.

@RemiBou
Copy link
Contributor

RemiBou commented Aug 17, 2018

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/

@Andrzej-W
Copy link

Andrzej-W commented Aug 19, 2018

@RemiBou I was fighting with ContinueWith in Program.Main a few days ago but without success. I have read your blog and source code https://github.com/RemiBou/Toss.Blazor. Your Main method looks like Blazor 0.4. In Blazor 0.5 "default" Main looks different. I have found this answer with sample code from @SteveSandersonMS https://github.com/aspnet/Blazor/issues/1287#issuecomment-412037803 and it doesn't work either. Application doesn't start. Everything is loaded, nothing in browser console and I see only "Loading..." in browser window.

@Andrzej-W
Copy link

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.

@RemiBou
Copy link
Contributor

RemiBou commented Aug 21, 2018

@Andrzej-W it's weirf because I'm using Blazor 0.5.1, I'll try to migrate it to a more recent model

@aspnet-hello aspnet-hello transferred this issue from dotnet/blazor Dec 17, 2018
@aspnet-hello aspnet-hello added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates area-blazor Includes: Blazor, Razor Components labels Dec 17, 2018
@alexDevBR
Copy link

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.

@mkArtakMSFT
Copy link
Member

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.

@mkArtakMSFT mkArtakMSFT removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 9, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

10 participants