使用一种语言的多个值在应用中本地化字符串

时间:2018-02-06 15:05:56

标签: wpf string localization

我想在我的WPF应用程序中本地化字符串。 因此,我找到了这个教程https://www.codeproject.com/Articles/299436/WPF-Localization-for-Dummies,这有助于我理解WPF本地化的主要内容。

我的应用程序不需要使用多种语言翻译字符串,但不同品牌的价值不同。我正在为具有不同行为(图像,文本等)的不同客户编译应用程序。

我认为上面的教程没有解决我的问题,因为

CurrentUICulture 

已被使用,我不想用不同的语言控制它。

有没有更好的方法来满足我的需求?

我想到的另一种方式是创建包含所有字符串的静态类。由于我已经在使用编译符号,因此实现起来非常容易。但如果WPF已经提供了更好的方法来处理这种情况,我不需要自己实现它。

提前致谢!

1 个答案:

答案 0 :(得分:1)

由于您正在使用WPF,您可以将相关的字符串和图像定义为资源,并为每个客户使用单独的ResourceDictionary(Xaml资源文件)。

在您的应用程序资源中,您可以先导入默认资源,然后导入客户特定的替代资源。它应该是最后一个输出,因此客户文件中存在的任何资源都将优先于默认文件中的资源。同样,如果在客户的资源中定义了资源 ,您将使用默认值中的资源。

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="Defaults.xaml" />
      <ResourceDictionary Source="CustomerA.xaml" />
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

您可能希望在C#中以编程方式执行此操作,并使用#if CUSTOMER_A之类的内容来选择引入的资源。

单个Xaml文件很简单:

<强> Defaults.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:s="clr-namespace:System;assembly=mscorlib">
  <s:String x:Key="MainWindowTitle">Snazzy Application</s:String>
  <BitmapImage x:Key="MainLogo" UriSource="Images/DefaultLogo.png" />
</ResourceDictionary>

<强> CustomerA.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:s="clr-namespace:System;assembly=mscorlib">
  <s:String x:Key="MainWindowTitle">Customer A's Amazeballs Application</s:String>
  <BitmapImage x:Key="MainLogo" UriSource="Images/CustomerALogo.png" />
</ResourceDictionary>

以与使用任何Xaml资源相同的方式使用资源:

<Window Title="{StaticResource MainWindowTitle}" />
<Image Source="{StaticResource MainLogo}" />