2015-03-21 3 views
0

Я пытаюсь создать тестовый многоязычный WPF, используя метод ниже, но когда я запускаю свое приложение, я получаю только перевод на английский язык, пока дата переводится в соответствии с указанным языком, в то время как он игнорирует испанский словарь ресурсов, я думаю проблема заключается в объединенных словарях. Я хочу решить проблему, используя тот же метод.Приложение WPF не переводится из ресурсного словаря?

StringResources.xaml

<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="UserID">User ID</system:String> 
<system:String x:Key="UserPassword">User Password</system:String> 
<system:String x:Key="Login">Login</system:String> 

</ResourceDictionary> 

StringResources.es-ES.xaml

<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="UserID">ID de usuario</system:String> 
<system:String x:Key="UserPassword">usuario Contraseña</system:String> 
<system:String x:Key="Login">Iniciar Sesión</system:String> 

</ResourceDictionary> 

App.xaml

<Application x:Class="WpfStringTables.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="Resources/StringResources.es-ES.xaml" /> 
      <ResourceDictionary Source="Resources/StringResources.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
    </Application.Resources> 
</Application> 

App.xaml.cs

public static void SelectCulture(string culture) 
    { 
     // List all our resources  
     List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>(); 
     foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries) 
     { 
      dictionaryList.Add(dictionary); 
     } 
     // We want our specific culture  
     string requestedCulture = string.Format("StringResources.{0}.xaml", culture); 
     ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString == requestedCulture); 
     if (resourceDictionary == null) 
     { 
      // If not found, we select our default language   
      //   
      requestedCulture = "StringResources.xaml"; 
      resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString == requestedCulture); 
     } 

     // If we have the requested resource, remove it from the list and place at the end.\  
     // Then this language will be our string table to use.  
     if (resourceDictionary != null) 
     { 
      Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary); 
      Application.Current.Resources.MergedDictionaries.Add(resourceDictionary); 
     } 
     // Inform the threads of the new culture  
     Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture); 
     Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); 
    } 

Чтобы проверить это, я использовал следующий MainWindows.xaml.cs

App.SelectCulture("es-ES"); 

форма

<Label Margin="5" Content="{DynamicResource UserID }" HorizontalAlignment="Left" VerticalAlignment="Top" Width="274"/> 
    <TextBox Margin="5" Name="UserIDTextBox" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="274" TabIndex="1"/> 
    <Label Margin="5" Content="{DynamicResource UserPassword}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="274"/> 
    <PasswordBox Margin="5" Name="UserPasswordTextBox" HorizontalAlignment="Left" Height="23" VerticalAlignment="Top" Width="274" TabIndex="2"/> 
    <Button Margin="5" Content="{DynamicResource Login}" Name="loginButton" HorizontalAlignment="Left" VerticalAlignment="Top" Width="107" TabIndex="3"/> 
    <Label Margin="5" Loaded="TodayLabel_Loaded" Name="TodayLabel" HorizontalAlignment="Left" VerticalAlignment="Top" Width="204"/> 

Последней меткой отображение даты, это единственный лейбл, который переведенной другие метки не переводится ,

ответ

0

Вы забыли указать «Ресурсы» на пути, когда вы сравнивали:

// We want our specific culture  
string requestedCulture = string.Format("Resources/StringResources.{0}.xaml", culture); 
ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString == requestedCulture); 
Смежные вопросы