79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
using Microsoft.Extensions.Localization;
|
|
using System.Globalization;
|
|
|
|
internal class MockStringLocalizer : IStringLocalizer
|
|
{
|
|
private readonly Dictionary<string, Dictionary<string, string>> _localizations;
|
|
|
|
public MockStringLocalizer()
|
|
{
|
|
// Initialize your mock data here
|
|
_localizations = new Dictionary<string, Dictionary<string, string>>
|
|
{
|
|
{"en", new Dictionary<string, string>
|
|
{
|
|
{"Hello", "Hello"},
|
|
{"Goodbye", "Goodbye"}
|
|
}
|
|
},
|
|
{"fr", new Dictionary<string, string>
|
|
{
|
|
{"Hello", "Bonjour"},
|
|
{"Goodbye", "Au revoir"}
|
|
}
|
|
}
|
|
// Add more languages as needed
|
|
};
|
|
}
|
|
|
|
public LocalizedString this[string name]
|
|
{
|
|
get
|
|
{
|
|
var value = GetString(name);
|
|
return new LocalizedString(name, value ?? name, resourceNotFound: value == null);
|
|
}
|
|
}
|
|
|
|
public LocalizedString this[string name, params object[] arguments]
|
|
{
|
|
get
|
|
{
|
|
var format = GetString(name);
|
|
var value = string.Format(format ?? name, arguments);
|
|
return new LocalizedString(name, value, resourceNotFound: format == null);
|
|
}
|
|
}
|
|
|
|
private string GetString(string name)
|
|
{
|
|
var culture = CultureInfo.CurrentUICulture.Name;
|
|
if (_localizations.ContainsKey(culture) && _localizations[culture].ContainsKey(name))
|
|
{
|
|
return _localizations[culture][name];
|
|
}
|
|
|
|
// Fallback to English if specific culture not found
|
|
return _localizations["en"].ContainsKey(name) ? _localizations["en"][name] : null;
|
|
}
|
|
|
|
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
|
|
{
|
|
var culture = CultureInfo.CurrentUICulture.Name;
|
|
if (!_localizations.ContainsKey(culture))
|
|
{
|
|
culture = "en"; // Default to English
|
|
}
|
|
|
|
foreach (var localization in _localizations[culture])
|
|
{
|
|
yield return new LocalizedString(localization.Key, localization.Value);
|
|
}
|
|
}
|
|
|
|
public IStringLocalizer WithCulture(CultureInfo culture)
|
|
{
|
|
// This method is obsolete and not recommended for use in .NET Core 3.0+ applications.
|
|
throw new NotImplementedException();
|
|
}
|
|
} |