gpt4 book ai didi

c# - WPF 应用程序的本地化

转载 作者:太空宇宙 更新时间:2023-11-03 20:22:37 25 4
gpt4 key购买 nike

我们正在开发应支持多种语言的 WPF 应用程序。由于我们必须支持一些开发团队都不知道的语言,我们同意允许用户(管理员)输入这些语言的文本。这样做的最佳方法是什么?

最佳答案

您可以按照以下步骤操作

1 创建资源文件

将此文件 StringResources.xaml 添加到 Ressources 目录。示例在这里:

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">

<system:String x:Key="close">Close</system:String>
</ResourceDictionary>

您可以创建多个文件,每种语言一个。

2 添加资源(启动应用程序时调用)

private void SetLanguageDictionary()
{
ResourceDictionary dict = new ResourceDictionary();
switch (Thread.CurrentThread.CurrentCulture.ToString())
{
case "en-US":
dict.Source = new Uri("..\\Resources\\StringResources.xaml", UriKind.Relative);
break;
case "fr-CA":
dict.Source = new Uri("..\\Resources\\StringResources.fr-CA.xaml", UriKind.Relative);
break;
default :
dict.Source = new Uri("..\\Resources\\StringResources.xaml",UriKind.Relative);
break;
}
this.Resources.MergedDictionaries.Add(dict);
}

3 使用资源,像这样-

<Button      
x:Name="btnLogin"
Click="btnLogin_Click"
Content="{DynamicResource login}"
Grid.Row="3"
Grid.Column="0"
Padding="10" />

关于c# - WPF 应用程序的本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12584781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com