Nichten
Nichten

Reputation: 85

c# - localization - changing language in wpf app

I am making some wpf application and I need option for changing languages. I have folder named Resorces in my solution, where I store all my resx file - actually language.resx and language.en-EN.resx. My XAML looks like:

<Window x:Class="CoinCatalogue.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:CoinCatalogue"
    xmlns:resx="clr-namespace:CoinCatalogue.Resources"
    mc:Ignorable="d"
    Title="{x:Static resx:language.WindowName}" Height="480" Width="640">
<Grid>
    <Menu x:Name="menu" HorizontalAlignment="Left" Height="auto" VerticalAlignment="Top" Width="517">
        <MenuItem x:Name="File" Header="{x:Static resx:language.File}">
            <MenuItem x:Name="Open" Header="{x:Static resx:language.Open}"/>
        </MenuItem>
    </Menu>

</Grid>

Everything is ok - I have access to string in app. And in my main class I am trying to change culture with line:

language.Culture = new System.Globalization.CultureInfo("en-EN");

But nothings happen to my app. This line is before InitializeComponent();.

What am I doing wrong? I am using Visual Studio 2015. /is it a good idea to change app language in this way?

Upvotes: 3

Views: 16168

Answers (1)

Gary Wright
Gary Wright

Reputation: 2469

I think how you are changing the culture is the issue.

I just put a quick sample together where I had the following resx files:

Resources.en-US.resx

Resources.fr-FR.resx

Resources.resx

If I run the app without attempting to change the culture, I get the strings from the Resources.en-US.resx file as that is my default. If I add this code before InitializeComponent my strings come from the Resources.fr-FR.resx file:

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("fr-FR");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("fr-FR");

Upvotes: 2

Related Questions