0

I made my test project as per

How I dynamically switch between different language resource files?

I am unable to see the language change at runtime. I am sharing my codes and attempts here.

xaml file

    <MenuItem Header="{x:Static Resources:Resources.File}" VerticalAlignment="Center"> <!-- MenuFile-->
            <MenuItem Header="{x:Static Resources:Resources.New}"/>
            <MenuItem Header="{x:Static Resources:Resources.Open}"/>
            <MenuItem Header="{x:Static Resources:Resources.Close}"/>
        </MenuItem>


   private void English_Click(object sender, RoutedEventArgs e)
    {
        ChangeCulture("en-US");
    }

    private void German_Click(object sender, RoutedEventArgs e)
    {
        ChangeCulture("de-DE");
    }

    private void French_Click(object sender, RoutedEventArgs e)
    {
        ChangeCulture("fr-FR");
    }

    private void Chineese_Click(object sender, RoutedEventArgs e)
    {
        ChangeCulture("zh-CN");
    }

    public void ChangeCulture(string text)
    {
       Thread.CurrentThread.CurrentUICulture = new CultureInfo(text);
       Thread.CurrentThread.CurrentCulture = new CultureInfo(text);
       Properties.Resources.Culture = new CultureInfo(text);
    }

The resource files that I created are of names Resources.de-DE.resx, Resources.fr-FR.Resx, Resources.zh-CN.resx and Resources.resx (Which contains default english localization)

Please guide, as to how to edit the xaml UI controls to switch text when respective language selection changes

NewLearner
  • 25
  • 1
  • 5
  • If it's dynamic updates you need then you'll probably have much better luck using a [custom markup extension](https://www.wpftutorial.net/LocalizeMarkupExtension.html). That's what I use, it works really well. – Mark Feldman Nov 09 '19 at 22:09
  • What is the problem to use `Strings.en-US.resx`? You resources will be loaded according the current thread culture or use fallback – Pavel Anikhouski Nov 10 '19 at 15:28
  • @PavelAnikhouski Whenever I try to create Strings.en-US.resx resource file name, it creates an empty Strings.en-US.Designer.cs file. There by I will not be able to use the statements like MenuItem Header="{x:Static Resources:Strings.New}". Please Correct me if somethings else that I am missing to get this work – NewLearner Nov 11 '19 at 15:15
  • @NewLearner They should be empty, only main `Resources.resx` with default localization (English basically) contains autogenerated `Resources.Designer.cs` code, by declaring custom tool in file properties. You don't need to generate this code for every language. You can use en-US as neutral language in `Resources.resx` and define a separate files e.g. fr-FR or de-De in a separate files – Pavel Anikhouski Nov 11 '19 at 15:21
  • @PavelAnikhouski I have updated the code per your suggestion. Please refer my latest edits in the intial question posted. I'm still unable to change the UI to desired language changes. – NewLearner Nov 11 '19 at 15:50
  • @PavelAnikhouski, Also when I try to implement the following line of code, MenuFile.Header = DemoLangSupport.Properties.Resources.File; where MenuItem "File" Name = "MenuFile", then it works. Does this mean that I need to have names of every UI Item (like maybe text block, radiobutton, MenuItem, Window) populated like the above statement mentioned? – NewLearner Nov 11 '19 at 17:05
  • @NewLearner yes, you have to specify in resources all localizable strings from your application – Pavel Anikhouski Nov 11 '19 at 18:30
  • @PavelAnikhouski I understand, I need to mention all the localize strings in resource file. But my question was only when I mention in code .cs file like so then they work. MenuFile.Header = DemoLangSupport.Properties.Resources.File Does this mean all the localized string in resource file needs specific mapping in .cs file as well? I thought reference of each of them in .xaml should take care once we change the current UI thread to desired language – NewLearner Nov 11 '19 at 18:38
  • @PavelAnikhouski Sorry about asking again. Will you be able to help guide my last comment? – NewLearner Nov 14 '19 at 16:51
  • @NewLearner You should refer to localized keys only once, the exact language will be determined automatically according thread culture – Pavel Anikhouski Nov 14 '19 at 21:01
  • @PavelAnikhouski, Please refer my updated code in my initial edit. It still does not work as expected. Is there anything else that I am missing. – NewLearner Nov 15 '19 at 00:11

0 Answers0