Home > Mobile >  WPF localization with dynamic resource doesn't work
WPF localization with dynamic resource doesn't work

Time:09-10

I made a custom UI wix bootstrapper as a WPF .net class library. I wanted to change the setup language in realtime according to the user choice by using a MergedDictionaries and DynamicRessource.

But the issue I encounter is that the string doesn't show on the UI and it's not an uri path error because I can access it with the code behind.

<Window.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <ResourceDictionary x:Key="Application">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/LanguageSelector;component/Langues/StringResources.fr-FR.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
</Window.Resources>

The code used to access the string ressource in the view

 <TextBlock Text="{DynamicResource Setup}" Margin="10" FontSize="16" Visibility="{Binding Path=LanguageSelectorUIEnabled, Converter={StaticResource BooleanToVisibilityConverter}}"  />
      

The StringResources.fr-FR.xaml file

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=mscorlib">


    <system:String x:Key="Setup">Choissisez la langue du gestionnaire d'installation</system:String>
  

CodePudding user response:

Modify your window markup to include the merged resource dictionary in the Resources dictionary directly:

<Window.Resources>
    <ResourceDictionary>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/LanguageSelector;component/Langues/StringResources.fr-FR.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

Or merge the ResourceDictionary at application level in App.xaml:

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/LanguageSelector;component/Langues/StringResources.fr-FR.xaml"/>
            </ResourceDictionary.MergedDictionaries>
       </ResourceDictionary>
    </Application.Resources>
</Application>
  • Related