123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace AwInitilizer.Cultures
- {
- public enum Languages
- {
- English, TraditionalChinese, SimplfyChinese
- }
- public class CulturesHelper
- {
- private static Lazy<CulturesHelper> _Instance = new Lazy<CulturesHelper>(() => new CulturesHelper());
- public static CulturesHelper Instance
- {
- get
- {
- return _Instance.Value;
- }
- }
- private static string resourcePrefix = "StringResource";
- private static string culturesFolder = "Cultures";
- private static List<CultureInfo> _supportedCultures = new List<CultureInfo>();
- public static List<CultureInfo> SupportedCultures
- {
- get
- {
- return _supportedCultures;
- }
- }
- private static ResourceDictionary resourceDictionary;
- public static ResourceDictionary ResourceDictionary
- {
- get
- {
- return resourceDictionary;
- }
- }
- public Languages CurrentCulture { get; private set; }
- public event EventHandler<Languages> OnCultureChange;
- private CulturesHelper()
- {
- CultureInfo cultureInfo = new CultureInfo("");
- List<string> files = Directory.GetFiles(string.Format("{0}\\{1}", Directory.GetCurrentDirectory(), culturesFolder))
- .Where(s => s.Contains(resourcePrefix) && s.ToLower().EndsWith("xaml")).ToList();
- foreach (string file in files)
- {
- try
- {
- string cultureName = file.Substring(file.IndexOf(".") + 1).Replace(".xaml", "");
- cultureInfo = CultureInfo.GetCultureInfo(cultureName);
- if (cultureInfo != null)
- {
- _supportedCultures.Add(cultureInfo);
- }
- }
- catch (ArgumentException)
- {
- }
- }
- }
- public void ChangeCulture(CultureInfo culture)
- {
- if (_supportedCultures.Contains(culture))
- {
- string loadedFileName = string.Format("{0}\\{1}\\{2}.{3}.xaml",
- Directory.GetCurrentDirectory(),
- culturesFolder,
- resourcePrefix,
- culture.Name);
- //FileStream fileStream = new FileStream(loadedFileName, FileMode.Open);
- //resourceDictionary = XamlReader.Load(fileStream) as ResourceDictionary;
- resourceDictionary = new ResourceDictionary() { Source = new Uri(loadedFileName) };
- Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
- //MainWindow.window.Resources.MergedDictionaries.Add(resourceDictionary);
- //Properties.Settings.Default.DefaultCulture = culture;
- //Properties.Settings.Default.Save();
- if (culture.Name == "en-US")
- {
- CurrentCulture = Languages.English;
- }
- else if (culture.Name == "zh-TW")
- {
- CurrentCulture = Languages.TraditionalChinese;
- }
- else if (culture.Name == "zh-CHS")
- {
- CurrentCulture = Languages.SimplfyChinese;
- }
- OnCultureChange?.Invoke(this, CurrentCulture);
- }
- }
- }
- }
|