darson1991
darson1991

Reputation: 406

Multilanguage wpf application with resources in Visual Studio Designer

This is my issue: I have multilanguage WPF application with resources in two different files. Now I choose the proper one in app.xaml.cs like this:

var dict = new ResourceDictionary();
switch (Thread.CurrentThread.CurrentCulture.ToString())
{
    case "de-DE":
        dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.de-DE.xaml", UriKind.Absolute);
        break;
    default:
        dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.xaml", UriKind.Absolute);
        break;
}
Resources.MergedDictionaries.Add(dict);

and everything works fine, but I can't see that resources in VisualStudio Designer.

On the other hand when I define ResourceDictionary in App.xaml file like this:

<Application x:Class="Ampe.UI.Views.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Exit="App_OnExit" ShutdownMode="OnMainWindowClose">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Resources;component/StringResources.de-DE.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

then I have this resources in designer but I can't set multilanguage.

Is there any possibility to visible resources in designer with multilanguage application? Maybe some kind of change app.xaml file while application is turn on?

Upvotes: 1

Views: 2604

Answers (1)

keymusicman
keymusicman

Reputation: 1291

You are on the right way.

  1. I'd recommend you to clear application merged dictionaries before adding new ones.

        Resources.MergedDictionaries.Clear();
        var dict = new ResourceDictionary();
        switch (Thread.CurrentThread.CurrentCulture.ToString())
        {
            case "de-DE":
                dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.de-DE.xaml", UriKind.Absolute);
                break;
            default:
                dict.Source = new Uri("pack://application:,,,/Resources;component/StringResources.xaml", UriKind.Absolute);
                break;
        }
        Resources.MergedDictionaries.Add(dict);
    
  2. Your app.xaml should look like you said:

    <Application x:Class="Ampe.UI.Views.App"
    
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Exit="App_OnExit" ShutdownMode="OnMainWindowClose">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/Resources;component/StringResources.de-DE.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    
  3. When you get localized values from resources, you have to use DynamicResources instead of StaticResources:

    <TextBlock Text="{DynamicResource MyString}" />
    

It works for me. Hope it helps.

Upvotes: 2

Related Questions