Toolbelt.Blazor.I18nText icon indicating copy to clipboard operation
Toolbelt.Blazor.I18nText copied to clipboard

Demo?

Open Letram opened this issue 5 years ago • 3 comments

Is there any available demo of the project to see how to change the current culture of the app?

Letram avatar Apr 09 '20 17:04 Letram

@jsakamoto

Would be really nice to have / know

would it be possible to post is, or get the demo code?

https://jsakamoto.github.io/Toolbelt.Blazor.I18nText/

peterhym21 avatar Sep 28 '22 07:09 peterhym21

Thank you for asking me.

The source code of the demo is in the following link:

  • https://github.com/jsakamoto/Toolbelt.Blazor.I18nText/tree/master/Tests

And you can see the code that changes the current language in the MainLayout.razor.

  • https://github.com/jsakamoto/Toolbelt.Blazor.I18nText/blob/master/Tests/Components/Shared/MainLayout.razor

You can also see another example in the following link that is a part of another standalone Blazor WebAssembly app:

  • https://github.com/jsakamoto/tap-to-tweet-reserved/blob/master/TapToTweetReserved/Client/Pages/Languages.razor

The point is just calling the I18nText.SetCurrentLanguageAsync("{language code}") method.

Please remember the "Blazor I18n Text" library doesn't provide any UI components and doesn't care about the CurrentCulture and CurrentUICulture of the current thread. For example, even if you change the language from "en" to "fr" by calling I18nText.SetCurrentLanguageAsync("fr"), CultureInfo.CurrentCulture and CultureInfo.CurrentUICulture would never be changed. If you want to make the CultureInfo.CurrentCulture and CultureInfo.CurrentUICulture follow to the language settings on the I18nText service, you have to subscribe the ChanegLanguage event of the I18nText service and do it by yourself (I don't have examples of doing that for now).

And also, on Blazor Server apps, you should know that changing the CultureInfo.CurrentCulture and CultureInfo.CurrentUICulture in the application thread doesn't work expectedly. The change will be dropped next event handling. That is by the design of Blazor Server apps. To change the culture of Blazor Server apps, you have to set the cookie and restart the entire session.

Appendix: If you don't mind dirty hacking, there is a NuGet package that allows you to change the culture of the current session of Blazor Server apps on the fly without restarting. (if you have an interest in it, follow the link below.)

  • https://www.nuget.org/packages/Toolbelt.Blazor.Server.ScopedCulture/1.0.0-preview.1#readme-body-tab

jsakamoto avatar Sep 28 '22 23:09 jsakamoto

yeah I have tried to implement the example into my solution, after digging around in the solution, I found your examples

but cant seam to get it to work?

as seen her:

https://github.com/jsakamoto/Toolbelt.Blazor.I18nText/issues/56

peterhym21 avatar Sep 29 '22 04:09 peterhym21