Background:
I have an async await function that gets a language and locale parameters from an external source and this will be used to set to the CurrentUICulture.
My web application is built in .NET Core 3.1 using C#
Problem:
The CurrentUICulture is set only in the context of Child method and once it goes out of this scope, CurrentUICulture doesn't reflect the changed value.
My Predicament:
I know it is always recommended to set the value of CurrentUICulture from main thread (and not from a child thread), but I am working on an existing code where there is too much of dependency on Thread.CurrentThread.CurrentUICulture and it is practically difficult for me to change at various places.
Snippet:
public class Example
{
public static async Task Main()
{
Console.WriteLine("Main Thread: Current UI culture is {0}",
Thread.CurrentThread.CurrentUICulture.Name);
await Child();
Console.WriteLine("Main Thread: UI culture after change is {0}",
Thread.CurrentThread.CurrentUICulture.Name);
}
public static async Task Child()
{
//get the culture text from an external service (assume this will return "pt-BR")
var cultureText = await externalService.GetCultureAsync();
//set the culture
Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureText);
Console.WriteLine("Child Thread: UI culture changed to {0}",
Thread.CurrentThread.CurrentUICulture.Name);
}
}
Actual Output:
(Assume the value returned by external service is "pt-BR" [Portugese Brazil])
Main Thread: Current UI culture is en-US
Child Thread: UI culture changed to pt-BR
Main Thread: UI culture after change is en-US
Desired Output:
Main Thread: Current UI culture is en-US
Child Thread: UI culture changed to pt-BR
Main Thread: UI culture after change is pt-BR
Any leads to solve the issue would be highly appreciable.