[Blazor-Server] Ability to change renderer's Culture on each component #35965
Labels
area-blazor
Includes: Blazor, Razor Components
feature-blazor-server
✔️ Resolution: Duplicate
Resolved as a duplicate of another issue
Status: Resolved
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/1152da404c3004d7fbaac8e503425e4aThis 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:
RendererSynchronizationContext
, so if you doInvokeAsync(() => CultureInfo.CurrentUICulture = something)
, it'll flow it to the rest of the invocations on the context.IComponent
implement aCultureInfo
getter, or, if you don't want to break existing code, make an optionalILocalizedComponent
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 anILocalizedComponent
But those are just ideas, since I'm not 100% sure how the rendering is actually implemented in Blazor
The text was updated successfully, but these errors were encountered: