This commit is contained in:
2026-06-02 17:39:58 +02:00
36 changed files with 2520 additions and 463 deletions
+30 -22
View File
@@ -4,8 +4,17 @@ 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.
/// String lookup backed by Strings.resx / Strings.fr.resx.
///
/// Registered as Scoped: in Blazor Server each circuit gets its own instance with its own
/// explicit <see cref="Culture"/>. The culture is stored as a field (not read from the
/// ambient <see cref="CultureInfo.CurrentUICulture"/>) because the interactive circuit does
/// NOT inherit the request/middleware culture, and ambient culture does not reliably flow
/// across render batches and SPA navigations. An explicit per-circuit field is deterministic:
/// set it once at circuit start and every page in that circuit renders in the same language.
///
/// The static <see cref="Instance"/> (used by the export services) has no explicit culture and
/// falls back to the ambient culture.
/// </summary>
public class TranslationSource
{
@@ -16,31 +25,30 @@ public class TranslationSource
// 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 =
private readonly ResourceManager _resourceManager =
new ResourceManager("SharepointToolbox.Web.Localization.Strings", typeof(TranslationSource).Assembly);
private CultureInfo _currentCulture = CultureInfo.CurrentUICulture;
private TranslationSource() { }
private CultureInfo? _culture;
public TranslationSource() { }
/// <summary>Explicit lookup culture. When unset, falls back to the ambient UI culture.</summary>
public CultureInfo Culture
{
get => _culture ?? CultureInfo.CurrentUICulture;
set => _culture = value;
}
public string this[string key] =>
_resourceManager.GetString(key, _currentCulture) ?? $"[{key}]";
_resourceManager.GetString(key, Culture) ?? $"[{key}]";
public CultureInfo CurrentCulture
{
get => _currentCulture;
set
{
if (Equals(_currentCulture, value)) return;
_currentCulture = value;
}
}
/// <summary>Sets this instance's culture from a language code ("fr" → French, else English/invariant).</summary>
public void SetCulture(string lang) => Culture = Resolve(lang);
public void SetCulture(string lang)
/// <summary>"fr" → French; anything else → invariant (the base Strings.resx, i.e. English).</summary>
public static CultureInfo Resolve(string lang) => lang switch
{
CurrentCulture = lang switch
{
"fr" => new CultureInfo("fr"),
_ => CultureInfo.InvariantCulture
};
}
"fr" => new CultureInfo("fr"),
_ => CultureInfo.InvariantCulture
};
}