当前位置:   article > 正文

WPF国际化 - ResXManager 多语言切换

resxmanager

WPF国际化

环境:

VisualStudio2022

ResXManager 1.84.6269.0

gjh

实现方法

step1:安装VisualStudio扩展工具 - ResXManager

image-20240229182826309

step2:创建资源文件

image-20240229183005000

image-20240229183052069

step3:设置访问级别

image-20240229183439424

step4:打开 ResXManager

image-20240229183231047

step5:设置/添加语言

image-20240229183541252

image-20240229183647389

step6:添加语言管理类

public class LanguageManager : INotifyPropertyChanged
{
    /// <summary>
    /// 资源
    /// </summary>
    private readonly ResourceManager _resourceManager;

    /// <summary>
    /// 懒加载
    /// </summary>
    private static readonly Lazy<LanguageManager> _lazy = new Lazy<LanguageManager>(() => new LanguageManager());
    public static LanguageManager Instance => _lazy.Value;
    public event PropertyChangedEventHandler PropertyChanged;

    public LanguageManager()
    {
        //获取此命名空间下Resources的Lang的资源,Lang可以修改
        _resourceManager = new ResourceManager("WpfAppZBC.Resources.lang", typeof(LanguageManager).Assembly);
    }

    /// <summary>
    /// 索引器的写法,传入字符串的下标
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    /// <exception cref="ArgumentNullException"></exception>
    public string this[string name]
    {
        get
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            return _resourceManager.GetString(name);
        }
    }

    public void ChangeLanguage(CultureInfo cultureInfo)
    {
        CultureInfo.CurrentCulture = cultureInfo;
        CultureInfo.CurrentUICulture = cultureInfo;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("item[]"));  //字符串集合,对应资源的值
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

step7:在xaml中引用资源

<Window
    x:Class="WpfAppZBC.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfAppZBC"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="{Binding [mainWindow], Source={x:Static local:LanguageManager.Instance}}"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Menu>
            <MenuItem Header="{Binding [file], Source={x:Static local:LanguageManager.Instance}}">
                <MenuItem Header="{Binding [open], Source={x:Static local:LanguageManager.Instance}}" />
                <MenuItem Header="{Binding [save], Source={x:Static local:LanguageManager.Instance}}" />
                <MenuItem Header="{Binding [saveAs], Source={x:Static local:LanguageManager.Instance}}" />
                <MenuItem Header="{Binding [exit], Source={x:Static local:LanguageManager.Instance}}" />
            </MenuItem>
            <MenuItem Header="{Binding [edit], Source={x:Static local:LanguageManager.Instance}}">
                <MenuItem Header="{Binding [cut], Source={x:Static local:LanguageManager.Instance}}" />
                <MenuItem Header="{Binding [copy], Source={x:Static local:LanguageManager.Instance}}" />
                <MenuItem Header="{Binding [paste], Source={x:Static local:LanguageManager.Instance}}" />
                <MenuItem Header="{Binding [delete], Source={x:Static local:LanguageManager.Instance}}" />
            </MenuItem>
            <MenuItem Header="{Binding [system], Source={x:Static local:LanguageManager.Instance}}">
                <MenuItem Header="{Binding [internationalization], Source={x:Static local:LanguageManager.Instance}}">
                    <MenuItem
                        x:Name="menuItemZHCN"
                        Click="menuItemZHCN_Click"
                        Header="中文简体" />
                    <MenuItem
                        x:Name="menuItemZHHANT"
                        Click="menuItemZHHANT_Click"
                        Header="中文繁体" />
                    <MenuItem
                        x:Name="menuItemEN"
                        Click="menuItemEN_Click"
                        Header="English" />
                </MenuItem>
            </MenuItem>
        </Menu>
        <TextBlock
            Grid.Row="1"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            FontSize="65"
            Text="{Binding [contentText], Source={x:Static local:LanguageManager.Instance}}" />
    </Grid>
</Window>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

step8:语言切换

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void menuItemZHCN_Click(object sender, RoutedEventArgs e)
    {
        LanguageManager.Instance.ChangeLanguage(new CultureInfo("zh-CN"));
    }

    private void menuItemZHHANT_Click(object sender, RoutedEventArgs e)
    {
        LanguageManager.Instance.ChangeLanguage(new CultureInfo("zh-Hant"));
    }

    private void menuItemEN_Click(object sender, RoutedEventArgs e)
    {
        LanguageManager.Instance.ChangeLanguage(new CultureInfo("en"));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

源码仓库

WineMonk/ResXManagerStudy: WPF国际化 (github.com)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/427822
推荐阅读
相关标签
  

闽ICP备14008679号