using System.Globalization; using System.Windows; using Titano.Core.Localization; namespace Titano.App.Infrastructure; /// Switches the UI language at run time without restart (BRIEF ยง16), mirroring right-to-left languages. public static class UiLanguage { static UiLanguage() { // Windows created later (the main window at start, every dialog) take the reading direction when they load. EventManager.RegisterClassHandler(typeof(Window), FrameworkElement.LoadedEvent, new RoutedEventHandler((sender, _) => ((Window)sender).FlowDirection = Direction)); } /// The reading direction of the current language. public static FlowDirection Direction => Loc.IsRightToLeft ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; /// 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); if (Application.Current is { } app) { foreach (Window window in app.Windows) window.FlowDirection = Direction; } return culture; } }