|
| 1 | +using System.Globalization; |
| 2 | +using System.Text.Json; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Runtime.CompilerServices; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Reflection; |
| 7 | + |
| 8 | +namespace SharpCoreDB.Viewer.Services; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Localization service for the SharpCoreDB Viewer with reactive property support |
| 12 | +/// </summary> |
| 13 | +public class LocalizationService : INotifyPropertyChanged |
| 14 | +{ |
| 15 | + private static LocalizationService? _instance; |
| 16 | + private Dictionary<string, string> _strings = []; |
| 17 | + private CultureInfo _currentCulture; |
| 18 | + |
| 19 | + public static LocalizationService Instance => _instance ??= new LocalizationService(); |
| 20 | + |
| 21 | + public event PropertyChangedEventHandler? PropertyChanged; |
| 22 | + public event EventHandler? LanguageChanged; |
| 23 | + |
| 24 | + private LocalizationService() |
| 25 | + { |
| 26 | + _currentCulture = CultureInfo.CurrentUICulture; |
| 27 | + Debug.WriteLine($"[Localization] Service initialized with culture: {_currentCulture.Name}"); |
| 28 | + LoadLanguage(_currentCulture.Name); |
| 29 | + } |
| 30 | + |
| 31 | + public string this[string key] => _strings.TryGetValue(key, out var value) ? value : key; |
| 32 | + |
| 33 | + public void SetLanguage(string cultureName) |
| 34 | + { |
| 35 | + Debug.WriteLine($"[Localization] SetLanguage called with: {cultureName}"); |
| 36 | + |
| 37 | + _currentCulture = new CultureInfo(cultureName); |
| 38 | + CultureInfo.CurrentUICulture = _currentCulture; |
| 39 | + LoadLanguage(cultureName); |
| 40 | + |
| 41 | + Debug.WriteLine($"[Localization] Language changed to: {cultureName}, firing events..."); |
| 42 | + |
| 43 | + // Notify all properties have changed |
| 44 | + OnPropertyChanged(string.Empty); |
| 45 | + LanguageChanged?.Invoke(this, EventArgs.Empty); |
| 46 | + |
| 47 | + Debug.WriteLine($"[Localization] LanguageChanged event fired. Subscribers: {LanguageChanged?.GetInvocationList().Length ?? 0}"); |
| 48 | + } |
| 49 | + |
| 50 | + private void LoadLanguage(string cultureName) |
| 51 | + { |
| 52 | + Debug.WriteLine($"[Localization] LoadLanguage called for: {cultureName}"); |
| 53 | + |
| 54 | + // Try specific culture first (e.g., nl-NL) |
| 55 | + if (!TryLoadLanguageFile(cultureName)) |
| 56 | + { |
| 57 | + // Fall back to language only (e.g., nl) |
| 58 | + var language = cultureName.Split('-')[0]; |
| 59 | + Debug.WriteLine($"[Localization] Trying fallback language: {language}"); |
| 60 | + if (!TryLoadLanguageFile(language)) |
| 61 | + { |
| 62 | + // Fall back to en-US |
| 63 | + Debug.WriteLine("[Localization] Falling back to en-US"); |
| 64 | + TryLoadLanguageFile("en-US"); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private bool TryLoadLanguageFile(string cultureName) |
| 70 | + { |
| 71 | + // First try embedded resources |
| 72 | + var resourceName = $"SharpCoreDB.Viewer.Resources.Strings.{cultureName}.json"; |
| 73 | + Debug.WriteLine($"[Localization] Attempting to load resource: {resourceName}"); |
| 74 | + |
| 75 | + var assembly = typeof(LocalizationService).Assembly; |
| 76 | + |
| 77 | + // List all embedded resources for debugging |
| 78 | + var allResources = assembly.GetManifestResourceNames(); |
| 79 | + Debug.WriteLine($"[Localization] Available manifest resources ({allResources.Length}): {string.Join(", ", allResources)}"); |
| 80 | + |
| 81 | + using var stream = assembly.GetManifestResourceStream(resourceName); |
| 82 | + if (stream != null) |
| 83 | + { |
| 84 | + _strings = JsonSerializer.Deserialize<Dictionary<string, string>>(stream) ?? new(); |
| 85 | + Debug.WriteLine($"[Localization] ? Loaded {_strings.Count} strings from embedded resource for culture {cultureName}"); |
| 86 | + |
| 87 | + var sample = _strings.Take(3).Select(kvp => $"{kvp.Key}={kvp.Value}"); |
| 88 | + Debug.WriteLine($"[Localization] Sample strings: {string.Join(", ", sample)}"); |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + // Fallback: Try to load from file system (relative to executable) |
| 93 | + var exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? ""; |
| 94 | + var filePath = Path.Combine(exeDir, "Resources", $"Strings.{cultureName}.json"); |
| 95 | + |
| 96 | + Debug.WriteLine($"[Localization] Trying file system: {filePath}"); |
| 97 | + |
| 98 | + if (File.Exists(filePath)) |
| 99 | + { |
| 100 | + var json = File.ReadAllText(filePath); |
| 101 | + _strings = JsonSerializer.Deserialize<Dictionary<string, string>>(json) ?? new(); |
| 102 | + Debug.WriteLine($"[Localization] ? Loaded {_strings.Count} strings from file for culture {cultureName}"); |
| 103 | + |
| 104 | + var sample = _strings.Take(3).Select(kvp => $"{kvp.Key}={kvp.Value}"); |
| 105 | + Debug.WriteLine($"[Localization] Sample strings: {string.Join(", ", sample)}"); |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + Debug.WriteLine($"[Localization] ? Could not find resource or file for culture: {cultureName}"); |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + public string Format(string key, params object[] args) |
| 114 | + { |
| 115 | + var template = this[key]; |
| 116 | + return string.Format(template, args); |
| 117 | + } |
| 118 | + |
| 119 | + protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) |
| 120 | + { |
| 121 | + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| 122 | + } |
| 123 | +} |
0 commit comments