/// <summary> /// 获取指定 Type 的资源文件 /// </summary> /// <param name="type"></param> /// <param name="key"></param> /// <param name="text"></param> /// <returns></returns> public static bool TryGetLocalizerString(Type type, string key, [MaybeNullWhen(false)] out string?text) { var localizer = JsonStringLocalizerFactory.CreateLocalizer(type); var l = localizer[key]; text = !l.ResourceNotFound ? l.Value : null; return(!l.ResourceNotFound); }
/// <summary> /// 通过指定类型创建 IStringLocalizer 实例 /// </summary> /// <returns></returns> public static IStringLocalizer CreateLocalizer(Type type) { var options = ServiceProviderHelper.ServiceProvider.GetRequiredService <IOptions <JsonLocalizationOptions> >(); var loggerFactory = ServiceProviderHelper.ServiceProvider.GetRequiredService <ILoggerFactory>(); var factory = new JsonStringLocalizerFactory(options, loggerFactory); return(factory.Create(type)); }