mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-07-17 17:09:19 +02:00
Added system language detection
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
<StackPanel Spacing="8" Orientation="Vertical">
|
||||
<StackPanel Spacing="8" Orientation="Horizontal">
|
||||
<Label Content="{Binding [Language], Source={x:Static starterNg:App.Loc}}" VerticalAlignment="Center" FontSize="14"/>
|
||||
<ComboBox MinWidth="350" MaxWidth="350" SelectedIndex="1" SelectionChanged="LanguageComboBox_OnSelectionChanged">
|
||||
<ComboBox x:Name="ChangeLanguageCb" MinWidth="350" MaxWidth="350" SelectedIndex="1" SelectionChanged="LanguageComboBox_OnSelectionChanged">
|
||||
<ComboBoxItem>Polski</ComboBoxItem>
|
||||
<ComboBoxItem>English</ComboBoxItem>
|
||||
</ComboBox>
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Avalonia.Markup.Xaml.Styling;
|
||||
|
||||
namespace StarterNG.Views;
|
||||
@@ -19,6 +17,12 @@ public partial class Settings : UserControl
|
||||
CabTextureResolutionSlider_OnValueChanged(null, null);
|
||||
shaderResolutionSlider_OnValueChanged(null, null);
|
||||
};
|
||||
|
||||
ChangeLanguageCb.SelectedIndex = App.Loc.CurrentLanguage switch
|
||||
{
|
||||
"Polski" => 0,
|
||||
_ => 1
|
||||
};
|
||||
}
|
||||
|
||||
private void TextureResolutionSlider_OnValueChanged(object? sender, RangeBaseValueChangedEventArgs e)
|
||||
@@ -50,7 +54,9 @@ public partial class Settings : UserControl
|
||||
|
||||
private void LanguageComboBox_OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
|
||||
if (sender is not ComboBox { SelectedItem: ComboBoxItem item }) return;
|
||||
if (!item.IsKeyboardFocusWithin) return;
|
||||
var lang = item.Content?.ToString();
|
||||
if (string.IsNullOrEmpty(lang)) return;
|
||||
|
||||
@@ -69,6 +75,6 @@ public partial class Settings : UserControl
|
||||
}
|
||||
App.Current.Resources.MergedDictionaries.Add(langDict);
|
||||
|
||||
App.Loc.SetLanguage(langDict);
|
||||
App.Loc.SetLanguage(langDict, lang);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user