0

I have a WPF application that supports multiple languages.

I saw this answer to the question Best way to implement multi-language (using .resx files) here on Stackoverflow. Everything works great except that the language doesn't change at runtime.

I have seen this answer to the question How to change the CurrentUICulture at runtime here on Stackoverflow but unfortunately his answer does not help me because the link he posted in his solution does not work.

I saw a lot of articles on google but no success.

A little attempt of mine (.cs):

private void EventSetter_OnHandler(object sender, MouseButtonEventArgs e)
{
    string item = ((ComboBoxItem)sender).Content.ToString();

    switch (item)
    {
        case "English":
            Console.WriteLine("English");
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-GB");
            System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-GB");
            Product.Language.Resources.Culture = new System.Globalization.CultureInfo("en-GB");
            break;

        case "German":
            Product.Language.Resources.Culture = new System.Globalization.CultureInfo("de-DE");
            Console.WriteLine("German");
            break;

        default:
            Console.WriteLine("none");
            break;
    }
}

XAML:

<Grid>
        <StackPanel>
            <Label x:Name="LabelHeader1" Content="{x:Static language1:Resources.LanguageTL}" />
            <Label x:Name="LabelHeader2" Content="{x:Static language1:Resources.HomeTL}" />
            <Label x:Name="LabelHeader3" Content="{x:Static language1:Resources.SkyTL}" />
            <ComboBox x:Name="Languages">
                <ComboBoxItem IsSelected="True">English</ComboBoxItem>
                <ComboBoxItem>German</ComboBoxItem>
                <ComboBox.ItemContainerStyle>
                    <Style>
                        <EventSetter Event="ComboBox.PreviewMouseDown" Handler="EventSetter_OnHandler" />
                    </Style>
                </ComboBox.ItemContainerStyle>
            </ComboBox>
        </StackPanel>

    </Grid>

The keys are saved in .resx files:

.resx file screenshot

And thanks for the help

Ros
  • 27
  • 7
  • I would suggest you to look into official guide on MSDN for localization of a WPF app, using the LocBaml tool from Wpf samples. Look for localization and globalization for WPF. Best way to localize a WPF app is to follow offiical guidelines. https://github.com/microsoft/WPF-Samples/tree/master/Tools/LocBaml – Tore Aurstad Apr 12 '20 at 22:20
  • @ToreAurstad But the method used by Rans with resx files is equally valid and in fact for some scenario much simpler. – Klaus Gütter Apr 13 '20 at 05:09
  • I suppose that you have to force WPF to re-load the values set via "{x:Static ...}". Basically you would have to re-load the whole UI, It will be easier if you repace the "{x:Static ..}" by something allowng for dynamic values like a "{Binding ...}" to a property in the view model or the [LocalizationExtension](https://github.com/XAMLMarkupExtensions/WPFLocalizationExtension). – Klaus Gütter Apr 13 '20 at 05:17

0 Answers0