WPF语言国际化实现 Resources+Settings


在属性下创建不同语言的资源文件resx。
Setting的好处是可以绑定, 这样就可以省去每个控件的获取过程, 缺点是需要多维护一份语言文件(即Setting文件,要与resx的键保持一致)。
切换语言时, 只要转换Setting的键值即可。

设置静态全局变量

public static class GLOBAL
{
    /// <summary>
    /// 语言本地化资源管理器
    /// </summary>
    public static ResourceManager Localization { get; set; }
}

根据系统语言本地化语言

using System.Configuration;
/// <summary>
/// 根据系统语言本地化语言
/// </summary>
private void Localization()
{
    // ResourceZH-CN
    string baseName = "MyApplication.Properties.Resource" + Thread.CurrentThread.CurrentCulture.Name.ToUpper();
    GLOBAL.Localization = new System.Resources.ResourceManager(baseName, typeof(Properties.ResourceZH_CN).Assembly);
    foreach (SettingsPropertyValue item in Properties.Settings.Default.PropertyValues)
    {
        item.PropertyValue = GetSettingsValue(item.Name, item.Property.DefaultValue.ToString());
    }
    Properties.Settings.Default.Save();
    Properties.Settings.Default.Upgrade();
}
private string GetSettingsValue(string nameKey, string defaultValue)
{
    string rslt = GLOBAL.Localization.GetString(nameKey);
    return string.IsNullOrEmpty(rslt) ? defaultValue : rslt;
}

前端绑定或后台直接调用


xmlns:properties="clr-namespace:MyApplication.Properties"
<Button Content="{Binding Path=ToolbarMenu_InfoStatistic, Source={x:Static properties:Settings.Default}}" />

var tip = Properties.Settings.Default.ToolbarMenu_InfoStatistic;
原文地址:https://www.cnblogs.com/wesson2019-blog/p/14044564.html