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

[Blazor-Server] Ability to change renderer's Culture on each component #35965

Closed
javiercampos opened this issue Aug 31, 2021 · 3 comments
Closed
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-server ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved

Comments

@javiercampos
Copy link

javiercampos commented Aug 31, 2021

Is your feature request related to a problem? Please describe.

I want to be able to set the renderer's Culture on each component. Currently, by default, when rendering, the component gets rendered with the CurrentUICulture that was on the thread that created the actual renderer.

This is fine if you have (as the documentation shows for localizing) one culture per http-request (or circuit) and don't change it during the lifetime of the circuit, but as soon as you want to dynamically change a component's culture, you just cannot.

An example of why this came to be is here: https://stackoverflow.com/questions/68987835/guaranteeing-currentuiculture-across-invokeasync-invocations

And an example of how changing it doesn't affect the component's rendering at all if done on an InvokeAsync (using the synchronization context) is here: https://gist.github.com/javiercampos/1152da404c3004d7fbaac8e503425e4a

This forces us to have a different CultureInfo object, which would not be a problem, but definitely forces you to use .ToString(Culture) on the template or @bind-X:culture="@Culture" on all two-way bindings. Which is easy to forget.

Add to that, that most third-party components don't really care about doing so, so having the renderer do it would be great.

Describe the solution you'd like

Be able to specify the component's culture for the renderer in some manner.

My proposed implementations of this solution could be one of either:

  • Have the CurrentCulture flow (and allow changing) within the RendererSynchronizationContext, so if you do InvokeAsync(() => CultureInfo.CurrentUICulture = something), it'll flow it to the rest of the invocations on the context.
  • Have the renderer set the culture before rendering (either make IComponent implement a CultureInfo getter, or, if you don't want to break existing code, make an optional ILocalizedComponent which has it, and the renderer understands, so if it finds it when rendering, it'll change the culture for the rendering phase), possibly changing it on ComponentState by checking if the component is an ILocalizedComponent

But those are just ideas, since I'm not 100% sure how the rendering is actually implemented in Blazor

@pranavkm
Copy link
Contributor

@javiercampos it's likely that addressing #28521 (which should ensure persisting the culture across InvokeAsync calls) should help here?

@javiercampos
Copy link
Author

javiercampos commented Aug 31, 2021

@javiercampos it's likely that addressing #28521 (which should ensure persisting the culture across InvokeAsync calls) should help here?

Actually, pretty much. That issue (the issue itself) looks like a much simpler explanation, but the underlying issue is the same and more than likely solving that would solve my issue.

Incidentally, I'd prefer the issue to be solved with the "other" suggestion (having a ILocalizedComponent or similar), but the way that linked issue would be solved would also work, definitely

@TanayParikh
Copy link
Contributor

Thanks @javiercampos closing this as dup of #28521.

@TanayParikh TanayParikh added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Sep 1, 2021
@ghost ghost added the Status: Resolved label Sep 1, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Oct 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-server ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved
Projects
None yet
Development

No branches or pull requests

4 participants