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;
6
8{
9 public class LocalizationManager : INotifyPropertyChanged
10 {
11 private static LocalizationManager? current;
12 public static LocalizationManager Current => current ??= new();
13
14#pragma warning disable CA2211 // Non-constant fields should not be visible
15 public static Type DefaultStringResource = typeof(AppResources);
16 public static EventHandler<CultureInfo>? CurrentCultureChanged;
17 public static EventHandler<FlowDirection>? FlowDirectionChanged;
18#pragma warning restore CA2211 // Non-constant fields should not be visible
19
20 public static IStringLocalizer GetStringLocalizer(Type? StringResource = null)
21 {
22 Type[] Arguments = [StringResource ?? DefaultStringResource];
23 Type GenericType = typeof(IStringLocalizer<>).MakeGenericType(Arguments);
24
25 try
26 {
27 return (IStringLocalizer)ServiceHelper.GetService(GenericType);
28 }
29 catch (Exception ex)
30 {
31 throw new LocalizationException("There is no localization service", ex);
32 }
33 }
34
35 public static IStringLocalizer GetStringLocalizer<TStringResource>()
36 {
37 try
38 {
39 return ServiceHelper.GetService<IStringLocalizer<TStringResource>>();
40 }
41 catch (Exception ex)
42 {
43 throw new LocalizationException("There is no localization service", ex);
44 }
45 }
46
47 public static FlowDirection FlowDirection
48 {
49 get
50 {
51 return CultureInfo.CurrentCulture.TextInfo.IsRightToLeft
52 ? FlowDirection.RightToLeft
53 : FlowDirection.LeftToRight;
54 }
55 }
56
57 public CultureInfo CurrentCulture
58 {
59 get => CultureInfo.CurrentCulture;
60
61 set
62 {
63 if (CultureInfo.CurrentCulture.Name == value.Name)
64 {
65 return;
66 }
67
68 CultureInfo.CurrentCulture = value;
69 CultureInfo.CurrentUICulture = CultureInfo.CurrentCulture = value;
70
71 CurrentCultureChanged?.Invoke(null, value);
72 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(this.CurrentCulture)));
73
74 FlowDirectionChanged?.Invoke(this, FlowDirection);
75 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FlowDirection)));
76 }
77 }
78
79 public event PropertyChangedEventHandler? PropertyChanged;
80 }
81}