using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Windows;
using MuchInfo.Chart.Data.EnumTypes;
using MuchInfo.Chart.DataAdapter;
namespace MuchInfo.Chart.WPF.Helpers
{
public class LanguageManager
{
#region Methods
#region Public Static Methods
///
/// 查找资料
///
/// The key.
/// System.String.
public static string FindResource(string key)
{
try
{
var findResource = Application.Current.FindResource(key);
if (findResource == null) return string.Empty;
return findResource.ToString();
}
catch
{
return string.Empty;
}
}
///
/// 设置图表语言
///
/// The culture string.
/// cannot find resource. + ex.ToString()
public static void SetLanguage(string cultureString)
{
try
{
var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList();
//先删除languages文件夹下加载了的资源
var languageDic = dictionaryList.Where(z => z.Source != null && z.Source.OriginalString.StartsWith(LanguageConst.ResourceStart)).ToList();
if (languageDic.Any())
{
foreach (var dictionary in languageDic)
{
Application.Current.Resources.MergedDictionaries.Remove(dictionary);
}
}
//添加新资源
var resourceDictionary = new ResourceDictionary
{
Source = new Uri(LanguageConst.ResourceStart + cultureString + ".xaml", UriKind.Relative)
};
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
//更改整个环境语言
var culture = new CultureInfo(cultureString);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
///
/// 设置编辑器的语言
///
///
public static void SetEditorLanguage(ChartLanguageType languageType)
{
var service = new DataService();
service.SetEditorLanguage(languageType);
}
#endregion Public Static Methods
#endregion Methods
}
}