-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalizationManager.cs
More file actions
102 lines (92 loc) · 3.49 KB
/
LocalizationManager.cs
File metadata and controls
102 lines (92 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// I:\Clear-Cache-Icons\ClearCacheIcons\LocalizationManager.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Windows.Forms;
namespace ClearCacheIcons
{
public static class LocalizationManager
{
private static Dictionary<string, string> _localizedStrings = new Dictionary<string, string>(); // Inicializar
private static string _currentLanguage = "es"; // Default language
public static string CurrentLanguage
{
get => _currentLanguage;
set
{
if (_currentLanguage != value)
{
_currentLanguage = value;
LoadLanguage(_currentLanguage);
}
}
}
static LocalizationManager()
{
LoadLanguage(_currentLanguage);
}
private static void LoadLanguage(string languageCode)
{
_localizedStrings = new Dictionary<string, string>();
var assembly = Assembly.GetExecutingAssembly();
string resourceName = $"ClearCacheIcons.lang.{languageCode}.json";
using (Stream? stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null)
{
Logger.Log($"Error: Embedded language resource '{resourceName}' not found. Using default texts.", true);
return;
}
try
{
using (StreamReader reader = new StreamReader(stream))
{
string jsonString = reader.ReadToEnd();
var jsonDocument = JsonDocument.Parse(jsonString);
foreach (JsonProperty property in jsonDocument.RootElement.EnumerateObject())
{
_localizedStrings[property.Name] = property.Value.GetString() ?? property.Name;
}
// Logger.Log($"Idioma '{languageCode}' cargado correctamente desde recursos incrustados.");
}
}
catch (Exception ex)
{
Logger.Log($"Error al cargar el recurso de idioma incrustado '{resourceName}': {ex.Message}", true);
}
}
}
public static string GetString(string key)
{
if (_localizedStrings.TryGetValue(key, out string? value)) // Hacer value anulable
{
return value ?? key; // Si es nulo, devolver la clave
}
// Fallback to key if not found
return key;
}
public static void ApplyLocalization(Control control)
{
if (control == null) return;
// Apply to the control itself
string? tag = control.Tag as string; // Usar string? para el tag
if (!string.IsNullOrEmpty(tag)) // Comprobar si el tag no es nulo o vacío
{
string localizedText = GetString(tag); // Pasar el tag no nulo
if (!string.IsNullOrEmpty(localizedText))
{
control.Text = localizedText;
}
}
// Apply to child controls
foreach (Control childControl in control.Controls)
{
ApplyLocalization(childControl);
}
}
}
}