• 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;
    
  • 相关阅读:
    [NOI2004]郁闷的出纳员
    【数论】[SDOI2010]古代猪文
    【2-SAT】[JSOI2010]满汉全席
    【数位DP】CF55D Beautiful numbers
    c语言编译的四个阶段
    转评:Python集合
    转评:Python字典
    Python iterators and generators:迭代器和生成器
    Python函数-具备特定功能的可以有参数和返回值的可重用的代码块儿
    Python-简版List和Tuple
  • 原文地址:https://www.cnblogs.com/wesson2019-blog/p/14044564.html
Copyright © 2020-2023  润新知