如何在不同的语言资源文件之间动态切换? [英] How I dynamically switch between different language resource files?

查看:55
本文介绍了如何在不同的语言资源文件之间动态切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Resources\English.resx and Resources\German.resx

在MainWindow.xml中,我尝试从资源文件中找到该值:

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

<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使其自动切换为德语?

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?

推荐答案

通常,您将在名称中使用标准区域性字符串创建资源文件.例如

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

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

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

资源管理器将根据Thread.CurrentUICulture切换区域性.我认为是关于它的好文章.本地化还具有回退行为,因此未知文化将通过en资源得到解答.

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

在XAML中的用法将是.

The usage in the XAML would be.

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

这篇关于如何在不同的语言资源文件之间动态切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆