使用.resx文件并在运行时更新语言环境的WPF国际化

时间:2019-03-05 15:54:40

标签: wpf internationalization runtime locale resx

有人可以显示针对每个语言环境使用.resx文件来国际化两种不同语言的WPF应用程序的最佳解决方案吗? 我还希望能够即时切换语言环境并在运行时更改xaml文本组件的内容

1 个答案:

答案 0 :(得分:1)

这里是一个链接,逐步说明了您的需求。 参考: http://www.thebestcsharpprogrammerintheworld.com/2017/07/26/localizing-a-wpf-program-using-c/

XAML:

<Label Content="{x:Static properties:Resources.LabelAddress}" Height="28" HorizontalAlignment="Left" Margin="35,103,0,0" Name="labelAddress" VerticalAlignment="Top" />

<TextBox Height="23" HorizontalAlignment="Left" Margin="119,26,0,0" Name="textBoxFName" VerticalAlignment="Top" Width="152" />

后面的代码:

    using System.Globalization;
    using System.Configuration;

    Properties.Resources.Culture = new CultureInfo(ConfigurationManager.AppSettings["Culture"]);

//To change the locale in runtime you need to set

    public static void SetLanguage(string locale)
    {
        if (string.IsNullOrEmpty(locale)) locale = "en-US";
        Properties.Resources.Culture= new System.Globalization.CultureInfo(locale);
    }

AppConfig.cs:

<appSettings>
    <add key="Culture" value="zh-CN" />
  </appSettings>