added multilang and settings
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using System.Globalization;
|
||||
using System.Resources;
|
||||
using System.Windows.Data;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
namespace CursorLang.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Берёт строки из ресурсов и при смене языка просит WPF перечитать все привязки.
|
||||
/// </summary>
|
||||
public sealed class LocalizationService : ObservableObject, ILocalizationService
|
||||
{
|
||||
private static readonly ResourceManager Resources =
|
||||
new("CursorLang.Resources.Strings", typeof(App).Assembly);
|
||||
|
||||
private CultureInfo _culture = CultureInfo.GetCultureInfo("en");
|
||||
|
||||
public string this[string key] => Resources.GetString(key, _culture) ?? key;
|
||||
|
||||
public IReadOnlyList<LanguageOption> AvailableLanguages { get; } =
|
||||
[
|
||||
new LanguageOption("en", "English"),
|
||||
new LanguageOption("ru", "Русский"),
|
||||
];
|
||||
|
||||
public string CurrentLanguage
|
||||
{
|
||||
get => _culture.TwoLetterISOLanguageName;
|
||||
set
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value) || value == CurrentLanguage)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_culture = CultureInfo.GetCultureInfo(value);
|
||||
CultureInfo.CurrentUICulture = _culture;
|
||||
|
||||
OnPropertyChanged(nameof(CurrentLanguage));
|
||||
|
||||
// Сообщаем об изменении индексатора: так обновляются все привязки
|
||||
// вида {Binding Localization[Key]}, то есть весь текст интерфейса
|
||||
OnPropertyChanged(Binding.IndexerName);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user