|
| 1 | +using Biyori.Settings; |
| 2 | +using Biyori.Settings.Frames; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Windows.Controls; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Windows; |
| 10 | +using System.Windows.Media; |
| 11 | + |
| 12 | +namespace Biyori.Lib.Languages |
| 13 | +{ |
| 14 | + public partial class Languages : ResourceDictionary |
| 15 | + { |
| 16 | + public static ApplicationLanguage DefaultAppLanguage { get; set; } |
| 17 | + public Languages() |
| 18 | + { |
| 19 | + } |
| 20 | + public static Languages Instance() |
| 21 | + { |
| 22 | + return Application.Current.Resources.MergedDictionaries.FirstOrDefault(x => x.Source.PathAndQuery.ToLower().EndsWith("languages.xaml")) as Languages; |
| 23 | + } |
| 24 | + public void Initialize() |
| 25 | + { |
| 26 | + var provider = App.ServiceProvider.GetProvider<SettingsProviderService>(); |
| 27 | + var appConfig = provider.GetConfig<ApplicationSettings>(); |
| 28 | + var selectedLanguageResource = appConfig?.SelectedLanguage?.Name != null && |
| 29 | + this.MergedDictionaries.Where(x => x.Contains("LangKey") && x["LangKey"].ToString() == appConfig.SelectedLanguage.Name).Count() > 0 ? |
| 30 | + this.getLanguageResource(appConfig.SelectedLanguage.Name) : |
| 31 | + this.getLanguageResource("en-us"); |
| 32 | + var selectedLanguage = new ApplicationLanguage() |
| 33 | + { |
| 34 | + ImageUrl = selectedLanguageResource["ImageUrl"]?.ToString(), |
| 35 | + DisplayName = selectedLanguageResource["DisplayName"]?.ToString(), |
| 36 | + Name = selectedLanguageResource["LangKey"]?.ToString(), |
| 37 | + }; |
| 38 | + appConfig.Languages.AddRange(this.getLanguages()); |
| 39 | + provider.UpdateConfig(appConfig, true); |
| 40 | + this.MergedDictionaries.Clear(); |
| 41 | + this.MergedDictionaries.Add(selectedLanguageResource); |
| 42 | + } |
| 43 | + |
| 44 | + private ApplicationLanguage getDefaultLanguage() |
| 45 | + { |
| 46 | + return this.getLanguageByLangKey("en-us"); |
| 47 | + } |
| 48 | + private ApplicationLanguage getLanguageByLangKey(string langKey) |
| 49 | + { |
| 50 | + |
| 51 | + var resx = this |
| 52 | + .MergedDictionaries.Where(x => x.Source?.PathAndQuery?.ToLower().EndsWith(".lang.xaml") == true).ToList(); |
| 53 | + var resource = resx.Find(x => x["LangKey"]?.ToString() == langKey); |
| 54 | + var lang = new ApplicationLanguage() |
| 55 | + { |
| 56 | + ImageUrl = resource["ImageUrl"]?.ToString(), |
| 57 | + DisplayName = resource["DisplayName"]?.ToString(), |
| 58 | + Name = resource["LangKey"]?.ToString(), |
| 59 | + }; |
| 60 | + return lang; |
| 61 | + } |
| 62 | + private IEnumerable<ApplicationLanguage> getLanguages() |
| 63 | + { |
| 64 | + var languages = new List<ApplicationLanguage>(); |
| 65 | + languages.AddRange(this.MergedDictionaries.Where(x => x.Source?.PathAndQuery?.ToLower().EndsWith(".lang.xaml") == true).Select(resource => new ApplicationLanguage() |
| 66 | + { |
| 67 | + ImageUrl = resource["ImageUrl"]?.ToString(), |
| 68 | + DisplayName = resource["Name"]?.ToString(), |
| 69 | + Name = resource["LangKey"]?.ToString(), |
| 70 | + })); |
| 71 | + return languages; |
| 72 | + } |
| 73 | + private ResourceDictionary getLanguageResource(string langKey) |
| 74 | + { |
| 75 | + var resx = this |
| 76 | + .MergedDictionaries.Where(x => x.Source?.PathAndQuery?.ToLower().EndsWith(".lang.xaml") == true).ToList(); |
| 77 | + return resx.Find(x => x["LangKey"]?.ToString() == langKey); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments