【问题标题】:How I dynamically switch between different language resource files?如何在不同的语言资源文件之间动态切换?
【发布时间】:2013-07-01 06:50:42
【问题描述】:

在我的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>

对于英语,这种方式可以完美运行。但是,根据本地语言,如何使用:Resource:German.LabelHello 使其自动切换到德语?

【问题讨论】:

    标签: wpf localization


    【解决方案1】:

    通常,您会创建名称中包含标准文化字符串的资源文件。例如

    Strings.en.resx
    Strings.zh-CN.resx
    Strings.de-DE.resx
    Strings.de-AU.resx
    ...

    资源管理器将根据Thread.CurrentUICulture 切换文化。我认为this 是关于它的好文章。本地化还具有后备行为,因此未知文化将通过en 资源得到答复。

    XAML 中的用法是。

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

    【讨论】:

    • 感谢您的回答。我创建了另一个资源:fr-CA,但是当我这样设置时:Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-CA");无法切换到 fr-CA 语言文件
    • 好吧,既然我不知道,你改变了什么等等。这有点难说,出了什么问题......
    • 您不应更改CurrentUICulture。您需要将Strings.Culture 设置为您要使用的文化。
    猜你喜欢
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多