Repository navigation
Expand file tree
/
Copy pathLocalizationManager.cs
More file actions
93 lines (79 loc) · 2.83 KB
/
Copy pathLocalizationManager.cs
File metadata and controls
93 lines (79 loc) · 2.83 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
/// <file>LocalizationManager.cs</file>
/// <author>Laurent Barraud</author>
/// <version>1.8.3</version>
/// <date>July 5th, 2026</date>
using System;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Windows.Forms;
namespace LifeProManager
{
/// <summary>
/// Central localization manager for WinForms.
/// Automatically loads culture-specific satellite files
/// based on the current UI culture.
/// </summary>
public static class LocalizationManager
{
// Single ResourceManager for the neutral resource file
private static readonly ResourceManager _resourceManager =
new ResourceManager("LifeProManager.locStrings", typeof(LocalizationManager).Assembly);
// Stores the current language code
private static string _currentLanguageCode = "en";
/// <summary>
/// Sets the current language and updates thread cultures.
/// </summary>
public static void SetLanguage(string languageCode)
{
if (string.IsNullOrWhiteSpace(languageCode))
{
languageCode = "en";
}
_currentLanguageCode = languageCode;
CultureInfo culture = new CultureInfo(languageCode);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
/// <summary>
/// Gets the current language code.
/// </summary>
public static string GetCurrentLanguageCode()
{
return _currentLanguageCode;
}
/// <summary>
/// Returns a localized string for the given key.
/// Uses the current UI culture and falls back to English if missing.
/// Returns the key itself if nothing is found.
/// </summary>
public static string GetString(string key)
{
if (string.IsNullOrWhiteSpace(key))
{
return string.Empty;
}
try
{
// Tries to get the value using the current UI culture
string? localizedText = _resourceManager.GetString(key, CultureInfo.CurrentUICulture);
if (!string.IsNullOrEmpty(localizedText))
{
return localizedText;
}
// Fallback to English explicitly
localizedText = _resourceManager.GetString(key, new CultureInfo("en"));
if (!string.IsNullOrEmpty(localizedText))
{
return localizedText;
}
}
catch
{
// Ignores missing keys or culture errors.
}
// Last resort: returns the key itself.
return key;
}
}
}