3

In my wpf project, I have added two resource files:

Resources\English.resx and Resources\German.resx

in MainWindow.xml, I try to find the value from the resource file:

<Window x:Uid="Window_1" x:Class="LocalizationInvestigate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Resources="clr-namespace:LocalizationInvestigate.Resources"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Uid="Grid_1">
        <Label x:Uid="Label_1" Content="{x:Static Resources:English.LabelHello}"></Label>
    </Grid>
</Window>

For English, it works perfectly this way. However, based on the local language, how I can make it automatically switch to German by using: Resource:German.LabelHello?

halfer
  • 19,824
  • 17
  • 99
  • 186
olidev
  • 20,058
  • 51
  • 133
  • 197

1 Answers1

6

Well usually, you would create resource files with the standard culture string in its name. E.g.

Strings.en.resx
Strings.en-US.resx
Strings.de-DE.resx
Strings.de-AU.resx
...

The resource manager would switch the culture according to the Thread.CurrentUICulture. I think this is good article about it. The localization has also a fallback behavior, so that unknown cultures would be answered with the en resources.

The usage in the XAML would be.

<Label Content="{x:Static Resources:Strings.LabelHello}" />
DHN
  • 4,807
  • 3
  • 31
  • 45
  • Thanks for the answer. I have created another resource: fr-CA but when I set it like this: Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-CA"); It is not possible to switch to fr-CA language file – olidev Jul 01 '13 at 07:39
  • Well since I don't know, what you've changed etc. It's a bit difficuilt to tell, what's wrong... – DHN Jul 01 '13 at 08:07
  • You shouldn't change the `CurrentUICulture`. You need to set `Strings.Culture` to the culture you want to use. – MOnsDaR Mar 21 '18 at 12:30