我想创建一个可以根据要求以不同语言显示的WPF应用程序 [英] I want to create a wpf application that can be displayed in different languages according to the requirement

查看:114
本文介绍了我想创建一个可以根据要求以不同语言显示的WPF应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
在我们的wpf项目中,我们希望提供对不同语言的支持.
如何在wpf中实现

谢谢!

Hi guys,
In our wpf project, we want to provide support for different languages.
How can it be accomplished in wpf

Thanks you!

推荐答案

语言代码 [ ^ ].例如,您的语言代码为hi-IN(印度印地语).在其他国家/地区,某些语言略有不同.美国人说美国英语(英国),英国人说英语(英国).在多个国家/地区使用的语言也有通用版本.通用英语的代码为"en"(无国家代码).

要添加特定语言的资源文件,您需要添加资源文件模板的新项(在模板中搜索资源"或浏览可用模板).该名称必须遵循严格的格式,例如 Resources.[languagecode] .resx .例如:

Resources.en.resx
Resources.zh-CN.resx
Resources.en-GB.resx
Resources.hi-IN.resx

创建文件后,需要将其拖放到解决方案资源管理器中的属性"文件夹((默认)Resources.resx所在的文件夹).否则它将无法正常工作.

接下来,您需要添加资源.默认的Resources.resx需要包含所有资源.例如以下内容(第一个值是资源名称,第二个值是资源名称)

HelloWorld-Hello World!
ClickMe-单击我
ExitApplication-退出应用程序

假设您要添加荷兰资源文件(如果我的北印度语足够好,我会添加北印度语资源文件:))在Resources.nl-NL.resx中,添加以下内容

HelloWorld-哈罗·韦雷德!
ClickMe-Klik mij

(出于示例目的,特意省略了ExitAppication)

假设您在应用程序中有两个按钮,其内容如
Read this blog on Using Resources.resx in WPF application[^]. If you follow the steps (don''t forget to change the access modifier to public or it will not work), you are prepared to use the Resource.resx in WPF.

The items you add to Resource.resx are your default language. You can add different languages. First you will need to know the language codes[^]. For instance the code of your language would be hi-IN (hindi, India). Some language are slightly different in other countries. Americans speak en-US (english United States) and Brits speak en-GB (english Great Britain). Languages spoken in more than one country also have a general version. General english has the code ''en'' (without a country code).

To add a resource file for a specific language, you need to add a New Item of the Resource file template (search the templates for ''Resource'' or browse the available templates). The name has to follow a strict pattern like Resources.[languagecode].resx. For instance:

Resources.en.resx
Resources.en-US.resx
Resources.en-GB.resx
Resources.hi-IN.resx

After creating the file, you need to drag-and-drop it to the Properties folder in the Solution explorer (the folder where the (default) Resources.resx resides). Otherwise it won''t work.

Next you''ll need to add the resources. The default Resources.resx needs to contain all the resources. For instance the following (1st value is the resource name, 2nd value is the value)

HelloWorld - Hello World!
ClickMe - Click Me
ExitApplication - Exit application

Let''s say that you want to add a dutch resources file (if my Hindi was well enough I would have added a Hindi resource file :)) In the Resources.nl-NL.resx you add the following

HelloWorld - Hallo Wereld!
ClickMe - Klik mij

(ExitAppication omitted on purpose for the examples sake)

Lets say you have two buttons in your application and made the content like discribed in the Using Resources.resx in WPF application blog[^]
<button content="{x:Static p:Resources.ClickMe}" />
<button content="{x:Static p:Resources.ExitApplication}" />



基于System.Threading.Thread.CurrentThread.CurrentUICulture,您的应用程序将根据以下原则从不同的资源文件中加载资源.

*当前的UICulture是hi-IN.是否有隐藏资源文件?是→使用它.
*否→是否支持通用的喜语言(无印度国家代码)?是→使用它
*否→使用默认的Resources.resx(resources.resx包含默认语言)

此原理适用于您搜索的每个资源.在我的英语/荷兰语示例中,我没有为ExitApplication提供荷兰语值.该应用程序将搜索ExitApplication的荷兰语值.因为我没有提供它,所以如果回退到退出应用程序"的默认值(搜索ClickMe不会失败,并且Click Me的荷兰语版本将显示在按钮上).

要更改CurrentUI语言,可以使用以下代码.



Based on the System.Threading.Thread.CurrentThread.CurrentUICulture you''re application will load resources from the different resources files according to the following principle.

* Current UICulture is hi-IN. Is there a hi-IN resource file? Yes → use it.
* No → Is there support for general hi language (without the Country code India)? Yes → use it
* No → Use the default Resources.resx (the resources.resx contains the default language)

This principle is used for each resource you search. In my english/dutch example I had not supplied a dutch value for ExitApplication. The application will search for a dutch value for ExitApplication. Because I didn''t supply it, if will fallback to the defautl value of ''Exit application''(Searching for ClickMe won''t fail and the dutch version of Click Me will be shown on the button).

To change the CurrentUI language you could use the following code.

using System.Globalization;

namespace LocalizationTest
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-NL");
        }
    }
}



我希望这能使您了解如何在应用程序中使用多种语言.



I hope this gives you an idea how to use multiple languages in your application.


这篇关于我想创建一个可以根据要求以不同语言显示的WPF应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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