C# 如何在asp.net core中获取.resx文件字符串
如何在asp.net core中获取resx文件字符串? 我在mvc中使用ResxResourceReader获取字符串。但是我在asp.net core中无法得到相同的结果。旧版本(如asp.net中)是创建一个默认资源文件,如MyResources.resx和其他用于不同区域性的文件MyResources.fr.resx。。。并使用MyResources.MyValue1从中检索值。创建MyResources.resx将生成一个.cs文件,其中包含所有资源值作为静态属性 .Net Core建议使用C# 如何在asp.net core中获取.resx文件字符串,c#,asp.net-core,C#,Asp.net Core,如何在asp.net core中获取resx文件字符串? 我在mvc中使用ResxResourceReader获取字符串。但是我在asp.net core中无法得到相同的结果。旧版本(如asp.net中)是创建一个默认资源文件,如MyResources.resx和其他用于不同区域性的文件MyResources.fr.resx。。。并使用MyResources.MyValue1从中检索值。创建MyResources.resx将生成一个.cs文件,其中包含所有资源值作为静态属性 .Net Core建
IStringLocalizer
,其中T是您创建的一个类,它与资源文件的名称匹配。您可以在没有任何资源文件的情况下开始开发,然后再添加它们。您必须在控制器上注入(IStringLocalizer您可以查看.net core官方文档。net core改变了资源文件的工作方式,我觉得这种方式不够标准且令人困惑(我花了几天时间才弄清楚),但您需要做的是:
o.ResourcesPath=“Resources”
只需使用服务。AddLocalization()
,否则它将开始查找Resources.Resources文件夹,该文件夹不存在
services.AddLocalization(o=>o.ResourcesPath=“Resources”);
配置(选项=>
{
var supportedCultures=new[]
{
新文化信息(“美国”),
新文化信息(“en GB”),
新文化信息(“de de”)
};
options.DefaultRequestCulture=新的RequestCulture(“en-US”、“en-US”);
//必须明确说明应用程序支持哪些区域性。
//这些是应用程序支持的格式化区域性
//数字、日期等。
options.SupportedCultures=SupportedCultures;
//这些是应用程序对UI字符串支持的区域性,
//也就是说,我们为您提供了本地化的资源。
options.supportedCultures=supportedCultures;
});
public interface ICommonResource
{
string ErrorUnexpectedNumberOfRowsSaved { get; }
string ErrorNoRecordsSaved { get; }
string ErrorConcurrency { get; }
string ErrorGeneric { get; }
string RuleAlreadyInUse { get; }
string RuleDoesNotExist { get; }
string RuleInvalid { get; }
string RuleMaxLength { get; }
string RuleRequired { get; }
}
public class CommonResource : ICommonResource
{
private readonly IStringLocalizer<CommonResource> _localizer;
public CommonResource(IStringLocalizer<CommonResource> localizer) =>
_localizer = localizer;
public string ErrorUnexpectedNumberOfRowsSaved => GetString(nameof(ErrorUnexpectedNumberOfRowsSaved));
public string ErrorNoRecordsSaved => GetString(nameof(ErrorNoRecordsSaved));
public string ErrorConcurrency => GetString(nameof(ErrorConcurrency));
public string ErrorGeneric => GetString(nameof(ErrorGeneric));
public string RuleAlreadyInUse => GetString(nameof(RuleAlreadyInUse));
public string RuleDoesNotExist => GetString(nameof(RuleDoesNotExist));
public string RuleInvalid => GetString(nameof(RuleInvalid));
public string RuleMaxLength => GetString(nameof(RuleMaxLength));
public string RuleRequired => GetString(nameof(RuleRequired));
private string GetString(string name) =>
_localizer[name];
}
公共接口资源
{
字符串错误UnexpectedNumberOfRowsSaved{get;}
字符串ErrorNoRecordsSaved{get;}
字符串错误并发{get;}
字符串ErrorGeneric{get;}
字符串RuleAlreadyInUse{get;}
字符串RuleDoesNotExist{get;}
字符串规则无效{get;}
字符串RuleMaxLength{get;}
需要字符串规则{get;}
}
公共类CommonResource:ICommonResource
{
专用只读IStringLocalizer\u localizer;
公共公共资源(IStringLocalizer定位器)=>
_定位器=定位器;
公共字符串ErrorUnexpectedNumberOfRowsSaved=>GetString(名称(ErrorUnexpectedNumberOfRowsSaved));
public string ErrorNoRecordsSaved=>GetString(nameof(ErrorNoRecordsSaved))
public interface ICommonResource
{
string ErrorUnexpectedNumberOfRowsSaved { get; }
string ErrorNoRecordsSaved { get; }
string ErrorConcurrency { get; }
string ErrorGeneric { get; }
string RuleAlreadyInUse { get; }
string RuleDoesNotExist { get; }
string RuleInvalid { get; }
string RuleMaxLength { get; }
string RuleRequired { get; }
}
public class CommonResource : ICommonResource
{
private readonly IStringLocalizer<CommonResource> _localizer;
public CommonResource(IStringLocalizer<CommonResource> localizer) =>
_localizer = localizer;
public string ErrorUnexpectedNumberOfRowsSaved => GetString(nameof(ErrorUnexpectedNumberOfRowsSaved));
public string ErrorNoRecordsSaved => GetString(nameof(ErrorNoRecordsSaved));
public string ErrorConcurrency => GetString(nameof(ErrorConcurrency));
public string ErrorGeneric => GetString(nameof(ErrorGeneric));
public string RuleAlreadyInUse => GetString(nameof(RuleAlreadyInUse));
public string RuleDoesNotExist => GetString(nameof(RuleDoesNotExist));
public string RuleInvalid => GetString(nameof(RuleInvalid));
public string RuleMaxLength => GetString(nameof(RuleMaxLength));
public string RuleRequired => GetString(nameof(RuleRequired));
private string GetString(string name) =>
_localizer[name];
}