add jobs
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
using Microsoft.Extensions.Localization;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Atomx.Admin.Client.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 基于 ILocalizationProvider 的 IStringLocalizer 实现:
|
||||
/// 使用 JSON 文件中的键值,未找到返回 key 本身。
|
||||
/// 名称改为 JsonStringLocalizer 避免与框架的 StringLocalizer 冲突。
|
||||
/// </summary>
|
||||
public class JsonStringLocalizer<T> : IStringLocalizer<T>
|
||||
{
|
||||
private readonly ILocalizationProvider _provider;
|
||||
|
||||
public JsonStringLocalizer(ILocalizationProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
public LocalizedString this[string name]
|
||||
{
|
||||
get
|
||||
{
|
||||
var value = _provider.GetString(name);
|
||||
if (value == null)
|
||||
{
|
||||
// Avoid synchronous blocking during server prerender. Start background load and return key.
|
||||
try
|
||||
{
|
||||
_ = _provider.LoadCultureAsync(_provider.CurrentCulture);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
var result = value ?? name;
|
||||
return new LocalizedString(name, result, resourceNotFound: result == name);
|
||||
}
|
||||
}
|
||||
|
||||
public LocalizedString this[string name, params object[] arguments]
|
||||
{
|
||||
get
|
||||
{
|
||||
var fmt = _provider.GetString(name);
|
||||
if (fmt == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_ = _provider.LoadCultureAsync(_provider.CurrentCulture);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
var format = fmt ?? name;
|
||||
var value = string.Format(format, arguments);
|
||||
return new LocalizedString(name, value, resourceNotFound: format == name);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
|
||||
{
|
||||
var list = new List<LocalizedString>();
|
||||
var providerType = _provider.GetType();
|
||||
var currentProp = providerType.GetProperty("CurrentCulture");
|
||||
var culture = currentProp?.GetValue(_provider) as string ?? string.Empty;
|
||||
var cacheField = providerType.GetField("_cache", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
||||
if (!string.IsNullOrEmpty(culture) && cacheField?.GetValue(_provider) is Dictionary<string, Dictionary<string, string>> cache && cache.TryGetValue(culture, out var dict))
|
||||
{
|
||||
foreach (var kv in dict)
|
||||
{
|
||||
list.Add(new LocalizedString(kv.Key, kv.Value, resourceNotFound: false));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public IStringLocalizer WithCulture(CultureInfo culture)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user