当前位置:   article > 正文

WPF多语言切换_wpf 多语言

wpf 多语言
  1. private static ResourceDictionary _currentSource;
  2. private static LanguageType _type;
  3. public static void ChangeLanguage(LanguageType type)
  4. {
  5. _type = type;
  6. string languageName = string.Empty;
  7. switch (_type)
  8. {
  9. case LanguageType.US:
  10. languageName = "en-US.xaml";
  11. break;
  12. case LanguageType.CN:
  13. languageName = "zh-CN.xaml";
  14. break;
  15. case LanguageType.TW:
  16. languageName = "zh-TW.xaml";
  17. break;
  18. default:
  19. break;
  20. }
  21. _currentSource = Application.LoadComponent(new Uri(@"Language\" + languageName, UriKind.Relative)) as ResourceDictionary;
  22. Application.Current.Resources.MergedDictionaries.Add(_currentSource);
  23. }
  24. public static string GetValueByKey(string key)
  25. {
  26. try
  27. {
  28. string data = Convert.ToString(_currentSource[key]);
  29. return data;
  30. }
  31. catch (Exception ex)
  32. {
  33. LogHelper.WriteLogByType(LogEnum.Info, $"获取{key}翻译出现异常,信息为{ex.Message}");
  34. return string.Empty;
  35. }
  36. }
  37. /// <summary>
  38. /// 当前语言不是英文,就是中文
  39. /// </summary>
  40. /// <returns></returns>
  41. public static bool IsChinese() => _type != LanguageType.US;

根据传递过来的参数重新加载不同的语言文件;

  1. <sys:String x:Key="HorizontalFlip" xml:space="preserve">左右&#13;翻转</sys:String>
  2. <sys:String x:Key="HorizontalFlip" xml:space="preserve">Horizontal&#13; flip</sys:String>

支持文字换行

 <Button hc:BorderElement.CornerRadius="10" Margin="0,15,0,0" FocusVisualStyle="{x:Null}" x:Name="Horizontal" Content="{DynamicResource HorizontalFlip}" Width="89" Height="60" FontSize="15" Foreground="White" BorderBrush="#6CADFF" Background="#6CADFF" Grid.Row="2" Grid.Column="3"/>

页面绑定

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

闽ICP备14008679号