Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LocalizationManager.cs
1using Microsoft.Extensions.Localization;
4using System.ComponentModel;
5using System.Globalization;
6using Waher.Events;
7
9{
10 public class LocalizationManager : INotifyPropertyChanged
11 {
12 private static LocalizationManager? current;
13 public static LocalizationManager Current => current ??= new();
14
15#pragma warning disable CA2211 // Non-constant fields should not be visible
16 public static Type DefaultStringResource = typeof(AppResources);
17 public static EventHandler<CultureInfo>? CurrentCultureChanged;
18 public static EventHandler<FlowDirection>? FlowDirectionChanged;
19#pragma warning restore CA2211 // Non-constant fields should not be visible
20
21 public static IStringLocalizer GetStringLocalizer(Type? StringResource = null)
22 {
23 Type[] Arguments = [StringResource ?? DefaultStringResource];
24 Type GenericType = typeof(IStringLocalizer<>).MakeGenericType(Arguments);
25
26 try
27 {
28 return (IStringLocalizer)ServiceHelper.GetService(GenericType);
29 }
30 catch (Exception ex)
31 {
32 throw new LocalizationException("There is no localization service", ex);
33 }
34 }
35
36 public static IStringLocalizer GetStringLocalizer<TStringResource>()
37 {
38 try
39 {
40 return ServiceHelper.GetService<IStringLocalizer<TStringResource>>();
41 }
42 catch (Exception ex)
43 {
44 throw new LocalizationException("There is no localization service", ex);
45 }
46 }
47
48 public static FlowDirection FlowDirection
49 {
50 get
51 {
52 return CultureInfo.CurrentCulture.TextInfo.IsRightToLeft
53 ? FlowDirection.RightToLeft
54 : FlowDirection.LeftToRight;
55 }
56 }
57
58 public CultureInfo CurrentCulture
59 {
60 get => CultureInfo.CurrentCulture;
61
62 set
63 {
64 if (CultureInfo.CurrentCulture.Name == value.Name)
65 {
66 return;
67 }
68
69 CultureInfo.CurrentCulture = value;
70 CultureInfo.CurrentUICulture = value;
71 CultureInfo.DefaultThreadCurrentCulture = value;
72 CultureInfo.DefaultThreadCurrentUICulture = value;
73
74 AppResources.Culture = value;
75
76 CurrentCultureChanged.Raise(null, value);
77 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(this.CurrentCulture)));
78
79 FlowDirectionChanged.Raise(this, FlowDirection);
80 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FlowDirection)));
81 }
82 }
83
84 public event PropertyChangedEventHandler? PropertyChanged;
85 }
86}
A strongly-typed resource class, for looking up localized strings, etc.