I have an old API with switches the current culture in that way:
private void ChangeCulture(int lcid)
{
Debug.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] ChangeCulture Culture Start: {Thread.CurrentThread.CurrentCulture}");
var newCulture = CultureInfo.GetCultureInfo(lcid);
Debug.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] Changing culture of currentThread to: {newCulture}");
Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = newCulture;
// Old framework compatibility (not important for this example)
CultureInfo.DefaultThreadCurrentCulture = newCulture;
CultureInfo.DefaultThreadCurrentUICulture = newCulture;
Debug.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] ChangeCulture Culture End: {Thread.CurrentThread.CurrentCulture}");
}
Previously, the code was called from a synchronous context. However, since it is now required that the code is called from an asynchronous context. Like here:
private async void Button_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}]Culture Start: {Thread.CurrentThread.CurrentCulture}");
if (this.currrentLcid == 1031)
{
await Task.Delay(1000);
this.ChangeCulture(1033);
this.currrentLcid = 1033;
}
else
{
await Task.Delay(1000);
this.ChangeCulture(1031);
this.currrentLcid = 1031;
}
Debug.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}]Culture End: {Thread.CurrentThread.CurrentCulture}");
}
Output:
[1]Culture Start: de-DE
[1] ChangeCulture Culture Start: de-DE
[1] Changing culture of currentThread to: en-US
[1] ChangeCulture Culture End: en-US
[1]Culture End: en-US
The culture flow with the async execution. But it doesn't flow globally back. If i call:
private void CheckCulture_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}]CheckCulture_Click: {Thread.CurrentThread.CurrentCulture}");
}
Output:
[1]CheckCulture_Click: de-DE
Everything is MainThread. (ManagedThreadId=1)
.NET Framework 4.8
How can i switch the current culture globally without knowledge of the calling context?