private static ResourceDictionary _currentSource;
private static LanguageType _type;
public static void ChangeLanguage(LanguageType type)
{
_type = type;
string languageName = string.Empty;
switch (_type)
{
case LanguageType.US:
languageName = "en-US.xaml";
break;
case LanguageType.CN:
languageName = "zh-CN.xaml";
break;
case LanguageType.TW:
languageName = "zh-TW.xaml";
break;
default:
break;
}
_currentSource = Application.LoadComponent(new Uri(@"Language\" + languageName, UriKind.Relative)) as ResourceDictionary;
Application.Current.Resources.MergedDictionaries.Add(_currentSource);
}
public static string GetValueByKey(string key)
{
try
{
string data = Convert.ToString(_currentSource[key]);
return data;
}
catch (Exception ex)
{
LogHelper.WriteLogByType(LogEnum.Info, $"获取{key}翻译出现异常,信息为{ex.Message}");
return string.Empty;
}
}
/// <summary>
/// 当前语言不是英文,就是中文
/// </summary>
/// <returns></returns>
public static bool IsChinese() => _type != LanguageType.US;
根据传递过来的参数重新加载不同的语言文件;
<sys:String x:Key="HorizontalFlip" xml:space="preserve">左右 翻转</sys:String>
<sys:String x:Key="HorizontalFlip" xml:space="preserve">Horizontal 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"/>
页面绑定