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

标签 c# wpf localization

我们正在开发应支持多种语言的 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/

相关文章:

c# - 如何修复 "You must install or update .NET to run this application."错误?

c# - 理解 'Center' 对于 RadialGradientBrushes

c# - Visual Studio 的快捷方式(从 Eclipse 移动)

wpf - xaml 中数据模板的设计时数据

java - Java 中 XSD 验证错误消息的 I18n

c# - session 状态超时

c# - 如何从 WPF 项目中运行控制台项目

c# - UserControl 中的依赖属性 List<string>

excel - Excel 中具有本地化功能的 SSRS 日期格式

silverlight - Silverlight MVVM业务应用程序: Where to place the resource files?