using System.Globalization;
using System.Windows;
using Chronos.Core.Localization;
using Chronos.Ui.Controls;
namespace Chronos.App.Infrastructure;
///
/// Switches the UI language at run time without restart (BRIEF ยง16). A takes the
/// language's direction when it is created and follows each switch, so right-to-left languages mirror windows opened
/// before and after this call alike; plain windows (the full-screen preview) take it when they load.
///
public static class UiLanguage
{
static UiLanguage()
{
EventManager.RegisterClassHandler(typeof(Window), FrameworkElement.LoadedEvent, new RoutedEventHandler((sender, _) => ((Window)sender).FlowDirection = Bidi.UiDirection));
}
/// Applies (or the OS language when null) and returns the culture in use.
public static CultureInfo Apply(string? code)
{
CultureInfo requested;
try
{
requested = code is null ? CultureInfo.CurrentUICulture : CultureInfo.GetCultureInfo(code);
}
catch (CultureNotFoundException)
{
requested = CultureInfo.CurrentUICulture;
}
var culture = Languages.BestMatch(requested);
CultureInfo.DefaultThreadCurrentUICulture = culture;
CultureInfo.CurrentUICulture = culture;
Loc.SetCulture(culture);
return culture;
}
}