41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
using System.Globalization;
|
|
using System.Resources;
|
|
|
|
namespace SharepointToolbox.Web.Localization;
|
|
|
|
/// <summary>
|
|
/// Singleton string lookup backed by Strings.resx / Strings.fr.resx.
|
|
/// Web version: no INotifyPropertyChanged — culture switching is per-request.
|
|
/// </summary>
|
|
public class TranslationSource
|
|
{
|
|
public static readonly TranslationSource Instance = new();
|
|
|
|
private ResourceManager _resourceManager = Strings.ResourceManager;
|
|
private CultureInfo _currentCulture = CultureInfo.CurrentUICulture;
|
|
|
|
private TranslationSource() { }
|
|
|
|
public string this[string key] =>
|
|
_resourceManager.GetString(key, _currentCulture) ?? $"[{key}]";
|
|
|
|
public CultureInfo CurrentCulture
|
|
{
|
|
get => _currentCulture;
|
|
set
|
|
{
|
|
if (Equals(_currentCulture, value)) return;
|
|
_currentCulture = value;
|
|
}
|
|
}
|
|
|
|
public void SetCulture(string lang)
|
|
{
|
|
CurrentCulture = lang switch
|
|
{
|
|
"fr" => new CultureInfo("fr"),
|
|
_ => CultureInfo.InvariantCulture
|
|
};
|
|
}
|
|
}
|