ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

WPF 本地化资源文件及运行时切换语言

2022-05-20 17:34:21  阅读:232  来源: 互联网

标签:ResourceService Resource 语言 本地化 void Properties 切换 WPF public


本地化资源文件创建

前期准备
Visual Studio 搜索并安装扩展插件 ResXManager

  1. 在项目内 Properties 文件夹内添加新建项 资源文件 Resource.resx

  2. 手动重新编译项目,然后 Resource.resx 右键菜单 -> 在 ResX Manager 中打开

  3. 打开后界面如下

    3.1.
    添加新语言,由于语言比较多,点开下拉稍等一会后选择需要添加的新语言

    3.2.
    检测代码引用,指示该键在项目中被调用的次数
    3.3.
    索引序号,表内键序号
    3.4.
    添加新键
    3.5.(重要)
    导入导出Excel是这个扩展最重要的一个功能,是用于给运维、产品、翻译等非开发人员做语言翻译的一个Excel表格,根据这个表内已有的Key来翻译相应语言,翻译完成后可直接导入到项目使用(第一语言在Excel中是空列名)

    (扩展内查看如下)

运行时切换语言
1.新建类 ResourceService

public class ResourceService : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private static readonly ResourceService _current = new ResourceService();
    public static ResourceService Current => _current;

    readonly Properties.Resource resource = new Properties.Resource();
    public Properties.Resource Resources => resource;

    protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = this.PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

    public void ChangedCulture(string name)
    {
        Properties.Resource.Culture = CultureInfo.GetCultureInfo(name);
        this.RaisePropertyChanged("Resources");
    }
}

2.XAML使用
<Button Content="{Binding Resources.Hello, Source={x:Static local:ResourceService.Current}}"/>

3.切换语言

private void Changed()
{
    ResourceService.Current.ChangedCulture("en-US");
}

标签:ResourceService,Resource,语言,本地化,void,Properties,切换,WPF,public
来源: https://www.cnblogs.com/SunsetAzure/p/16293087.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有