1

I am following this thread :-

multiligual wpf application

I want to support multiple languages as originally asked.

I've done everything suggested by Aghilas. However, only the last language that I have added to my App.xaml file actually gets applied. It makes no difference which ResourceDictionary I add at runtime.

Using this extract of the language section in my App.xaml file, only the last language referred to gets used, in this case it's French.

            <ResourceDictionary Source="languages/lang-english-uk.xaml" />
            <ResourceDictionary Source="languages/lang-english-us.xaml" />
            <ResourceDictionary Source="languages/lang-spanish.xaml" />
            <ResourceDictionary Source="languages/lang-french.xaml" />

This is the code I call when initialising the application.

    private void LoadLanguageResource()
    {
        ResourceDictionary dict = new ResourceDictionary();
        CultureInfo cultInfo = Thread.CurrentThread.CurrentCulture;
        switch (cultInfo.TwoLetterISOLanguageName)
        {
            case "fr":
                dict.Source = new Uri("..\\languages\\lang-french.xaml", UriKind.Relative);
                break;
            case "es":
                dict.Source = new Uri("..\\languages\\lang-spanish.xaml", UriKind.Relative);
                break;
            default:
                if (cultInfo.Name.Contains("US"))
                {
                    dict.Source = new Uri("..\\languages\\lang-english-us.xaml", UriKind.Relative);
                    break;
                }
                else
                {
                    dict.Source = new Uri("..\\languages\\lang-english-uk.xaml", UriKind.Relative);
                    break;
                }        
        }
        this.Resources.MergedDictionaries.Add(dict);
    }
maffoo
  • 107
  • 1
  • 8
  • Did you ever get an answer. I am researching having multiple languages displayed at the same time in the UI. I know how I do this in C++ but I cannot see a comparative path in WPF.I was hoping to set the culture in each control and display multiples controls in something like a wrappanel or such but so far I got nothing that works. – Paul Wichtendahl Aug 21 '23 at 02:46

2 Answers2

0

What I did to get my multiple languages to work was to only loaded one language file at a time, in my case I loaded English as a default one, in my App.xaml.

Then in my startup of the application I would check for what language the user wanted to display and I would remove the current language from the ResourceDictionary and then only add the newly selected language that the user wants.

Use:

this.Resources.MergedDictionaries.Remove(...);
this.Resources.MergedDictionaries.Add(...);

Example load code that I use:

 internal void LoadLanguageFile()
 {
      var languageCode = ApplicationSetting.LanguageCode;
      if (string.IsNullOrEmpty(languageCode) == false)
      {
            var dictionariesToRemove = new List<ResourceDictionary>();

            foreach (var dictionary in Application.Current.Resources.MergedDictionaries)
            {
                if (dictionary.Source.ToString().Contains(@"/Strings.") == true)
                    dictionariesToRemove.Add(dictionary);
            }

            foreach (var item in dictionariesToRemove)
                Application.Current.Resources.MergedDictionaries.Remove(item);

            var languageDictionary = new ResourceDictionary()
            {
                Source = new Uri($"/SomeApp;component/Assets/Languages/Strings.{languageCode}.xaml", UriKind.Relative)
            };

    Application.Current.Resources.MergedDictionaries.Add(languageDictionary);

            }
        }

Hope this helps.

Michael Woolsey
  • 803
  • 8
  • 15
  • Great idea. I've just had a go at this. However, I think I'm struggling to refer to the existing merged dictionary. How do I get a reference to the English dictionary I added in App.xaml? .Remove( ??? ); – maffoo Apr 18 '18 at 21:30
  • Ok, so I've used your example above (tweaked for my app). I'm now getting resource errors saying that none of the resources defined in the resource file that is loaded can be found. I am certain that the merged dictionary exists at the end of the method as I have called your foreach loop again and my english.xaml is there. However, any reference to the string resource names either within the code - FindResource("t-hellowelcome") - or within the XAML - {DynamicResource t-close} - cannot find the resource. Am I missing something really obvious? – maffoo Apr 19 '18 at 21:30
  • Fixed it. I was using this.Current.Resources..... instead of Application.Current.Resources.... Thanks! – maffoo Apr 19 '18 at 21:47
0

This is pretty much the method I use for localisation, except the apps I've worked on delivered one local language dictionary and you can't pick between say English and Spanish. When you merge another resource dictionary in which has the same keys as an existing one, the new ones replace the old. Some of my apps are mostly English with parts that are localised. Some also merge in branding strings or pictures which overlay the original.

You need to bind with dynamicsource to pick up changes.

The problem you're seeing seems more likely to be associated with your switch. If you debug that, I think you'll find there's a problem there and it just ends up picking the default dictionary no matter what.

If you really wanted to remove a dictionary you can do:

Application.Current.Resources.MergedDictionaries[0].Clear();

I think maybe you can also do removeat. But I don't think you should need to do either.

Andy
  • 11,864
  • 2
  • 17
  • 20