| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 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
- /// <summary>
- /// 查找资料
- /// </summary>
- /// <param name="key">The key.</param>
- /// <returns>System.String.</returns>
- 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;
- }
- }
- /// <summary>
- /// 设置图表语言
- /// </summary>
- /// <param name="cultureString">The culture string.</param>
- /// <exception cref="System.Exception">cannot find resource. + ex.ToString()</exception>
- 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());
- }
- }
- /// <summary>
- /// 设置编辑器的语言
- /// </summary>
- /// <param name="languageType"></param>
- public static void SetEditorLanguage(ChartLanguageType languageType)
- {
- var service = new DataService();
- service.SetEditorLanguage(languageType);
- }
- #endregion Public Static Methods
- #endregion Methods
- }
- }
|