From ba9c16e4c4c7c2fbe9c4580af2619a2973678387 Mon Sep 17 00:00:00 2001 From: Daniu Date: Thu, 12 Feb 2026 11:07:36 +0100 Subject: [PATCH] Added system language detection --- StarterNG/App.axaml.cs | 17 ++++++++++++++--- StarterNG/Services/LocalizationService.cs | 6 ++++-- StarterNG/Views/Settings.axaml | 2 +- StarterNG/Views/Settings.axaml.cs | 12 +++++++++--- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/StarterNG/App.axaml.cs b/StarterNG/App.axaml.cs index f28ab44..6b5d2e0 100644 --- a/StarterNG/App.axaml.cs +++ b/StarterNG/App.axaml.cs @@ -1,7 +1,7 @@ using System; +using System.Globalization; using System.Linq; using Avalonia; -using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml.Styling; @@ -18,9 +18,17 @@ public partial class App : Application { AvaloniaXamlLoader.Load(this); + CultureInfo ci = CultureInfo.InstalledUICulture ; + + var langName = ci.Name switch + { + "pl-PL" => "Polski", + _ => "English" + }; + var langDict = new ResourceInclude(new Uri("avares://StarterNG/")) { - Source = new Uri("Assets/Langs/English.axaml", UriKind.Relative) + Source = new Uri($"Assets/Langs/{langName}.axaml", UriKind.Relative) }; var resources = Current!.Resources; @@ -36,7 +44,10 @@ public partial class App : Application } resources.MergedDictionaries.Add(langDict); - Loc.SetLanguage(langDict); + + + + Loc.SetLanguage(langDict, langName); } public override void OnFrameworkInitializationCompleted() diff --git a/StarterNG/Services/LocalizationService.cs b/StarterNG/Services/LocalizationService.cs index 084e381..02839ce 100644 --- a/StarterNG/Services/LocalizationService.cs +++ b/StarterNG/Services/LocalizationService.cs @@ -1,6 +1,5 @@ using System.ComponentModel; using Avalonia.Controls; -using Avalonia.Styling; namespace StarterNG.Services; @@ -11,6 +10,8 @@ public class LocalizationService : INotifyPropertyChanged public event PropertyChangedEventHandler? PropertyChanged; + public string CurrentLanguage; + public string this[string key] { get @@ -23,9 +24,10 @@ public class LocalizationService : INotifyPropertyChanged } } - public void SetLanguage(IResourceProvider provider) + public void SetLanguage(IResourceProvider provider, string langName) { _currentProvider = provider; + CurrentLanguage = langName; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Item")); } diff --git a/StarterNG/Views/Settings.axaml b/StarterNG/Views/Settings.axaml index 8f10973..70fc150 100644 --- a/StarterNG/Views/Settings.axaml +++ b/StarterNG/Views/Settings.axaml @@ -17,7 +17,7 @@