我如何动态切换不同的语言资源文件?

时间:2013-07-01 06:50:42

标签: wpf localization

在我的wpf项目中,我添加了两个资源文件:

Resources\English.resx and Resources\German.resx
在MainWindow.xml中,我尝试从资源文件中找到值:

<Window x:Uid="Window_1" x:Class="LocalizationInvestigate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Resources="clr-namespace:LocalizationInvestigate.Resources"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Uid="Grid_1">
        <Label x:Uid="Label_1" Content="{x:Static Resources:English.LabelHello}"></Label>
    </Grid>
</Window>

对于英语,它以这种方式完美运作。但是,基于本地语言,我如何使用以下内容自动切换到德语:资源:German.LabelHello?

2 个答案:

答案 0 :(得分:6)

通常,您可以在其名称中创建标准文化字符串的资源文件。 E.g。

  

Strings.en.resx
  Strings.en-US.resx
  Strings.de-DE.resx
  Strings.de-AU.resx
  ...

资源管理器将根据Thread.CurrentUICulture切换文化。我认为this是关于它的好文章。本地化还具有后备行为,因此可以使用en资源来回答未知文化。

XAML中的用法是。

<Label Content="{x:Static Resources:Strings.LabelHello}" />

答案 1 :(得分:2)

对我来说,这篇文章是关于WPF和本地化的最佳文章! WPF Localization Using RESX Files

我之前使用它,它对我来说就像一个魅力。

相关问题