wpf 如何在不同语言的资源文件之间动态切换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17398833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:06:43  来源:igfitidea点击:

How I dynamically switch between different language resource files?

wpflocalization

提问by olidev

In my wpf project, I have added two resource files:

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

Resources\English.resx and Resources\German.resx

in MainWindow.xml, I try to find the value from the resource file:

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

For English, it works perfectly this way. However, based on the local language, how I can make it automatically switch to German by using: Resource:German.LabelHello?

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

回答by DHN

Well usually, you would create resource files with the standard culture string in its name. E.g.

通常,您会创建名称中带有标准区域性字符串的资源文件。例如

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

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

The resource manager would switch the culture according to the Thread.CurrentUICulture. I think thisis good article about it. The localization has also a fallback behavior, so that unknown cultures would be answered with the enresources.

资源管理器将根据Thread.CurrentUICulture. 我认为是一篇关于它的好文章。本地化也有回退行为,因此未知的文化可以用en资源来回答。

The usage in the XAML would be.

XAML 中的用法是。

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