NET 6, WPF App, CultureInfo.CurrentUICulture is reseted to "en-US" after programmatically set to "RU"

Valery Pogorelov 1 Reputation point
2021-12-12T22:06:55.247+00:00

Hi everyone!
I have such type of app:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
In App.xaml.cs:
private async void OnStartup(object sender, StartupEventArgs e)
{
Host = CreateHostBuilder(e.Args).Build();

            IConfiguration configuration = Host.Services.GetRequiredService<IConfiguration>();
            string uiCultureName = configuration["UICulture"]; // "RU"
            CultureInfo.CurrentUICulture = new CultureInfo(uiCultureInfo);

            var task = Host.RunAsync();
            ...
            var mainWindow = new MainWindow();
            mainWindow.Show();

Main window shows correct resources from Resources.RU.resx, but when I click button, child window is shown using Resources.resx (English). CurrentThread.ManagedThreadI is 1 for both window constructors. But for child window CultureInfo.CurrentUICulture is “en-US”. Help please, how can I track, where is CultureInfo.CurrentUICulture reseted back to “en-US”? Thank you in advance for any help. Windows itself has English UI.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,626 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 25,431 Reputation points Microsoft Vendor
    2021-12-14T07:08:44.807+00:00

    If you want to set the child window to display RU.resx resources. For setting up and using resources, you could try to refer to the following methods.
    Project structure:
    157268-image.png
    En.resx:
    157329-image.png
    RU.resx:
    157369-image.png
    App.xaml.cs:

    public partial class App : Application  
      {  
        public App()  
        {  
          System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("RU");  
          System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("En");  
        }  
      }  
    

    Add xmlns:res="clr-namespace:CurrentUICultureDemo.Properties" to both MainWindow.xaml and SubWindow.xaml.
    MainWindow.xaml:

    <Window x:Class="CurrentUICultureDemo.MainWindow"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:CurrentUICultureDemo"  
            xmlns:res="clr-namespace:CurrentUICultureDemo.Properties"  
            mc:Ignorable="d"  
            Title="MainWindow" Height="450" Width="800">  
        <StackPanel>  
            <Button x:Name = "button1" Content="{x:Static res:RU.Submit}"   Width="75"/>  
            <Button x:Name="Button2" Content="{x:Static res:RU.Password}"  Width="75" Click="Button2_Click"/>  
        </StackPanel>  
    </Window>  
    

    MainWindow.xaml.cs:

    public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
        }  
    
        private void Button2_Click(object sender, RoutedEventArgs e)  
        {  
          SubWindow w= new SubWindow();  
          w.Show();  
        }  
      }  
    

    SubWindow.xaml:

    <Window x:Class="CurrentUICultureDemo.SubWindow"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:CurrentUICultureDemo"  
            mc:Ignorable="d"  
             xmlns:res="clr-namespace:CurrentUICultureDemo.Properties"  
            Title="SubWindow" Height="450" Width="800">  
        <StackPanel>  
            <Button x:Name = "btn1" Content="{x:Static res:RU.Submit}"  Width="75"/>  
            <Button x:Name = "btn2" Content="{x:Static res:En.Submit}"  Width="75"/>  
            <Button x:Name="btn3" Content="{x:Static res:RU.Password}"    Height="30" Width="75"/>  
            <Button x:Name="btn4" Content="{x:Static res:En.Password}"    Height="30" Width="75"/>  
        </StackPanel>  
    </Window>  
    

    The result:
    157269-1.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html