d69c3290d8
Reviewed-on: #2
47 lines
1.5 KiB
C#
47 lines
1.5 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();
|
|
|
|
// Construct the ResourceManager with the explicit manifest base name rather
|
|
// than Strings.ResourceManager: the generated designer carries a stale base
|
|
// name ("SharepointToolbox.Strings") from before the project was renamed to
|
|
// *.Web, so its lookups throw MissingManifestResourceException. The embedded
|
|
// resource is "SharepointToolbox.Web.Localization.Strings".
|
|
private ResourceManager _resourceManager =
|
|
new ResourceManager("SharepointToolbox.Web.Localization.Strings", typeof(TranslationSource).Assembly);
|
|
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
|
|
};
|
|
}
|
|
}
|