为了在 ASP.NET Core 5.0 应用程序中处理分离类库的本地化问题,可以按照以下步骤进行操作:
在 .NET 类库项目中定义本地化资源文件。可以通过右键单击项目文件夹并选择“添加 -> 新建项”来创建一个 resx 文件。
在 resx 文件中添加需要本地化的字符串和对应的本地化值。
在 Startup.cs 文件中进行配置。在 ConfigureServices 方法中,添加以下代码:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc().AddViewLocalization();
var supportedCultures = new[] { new CultureInfo("en-US"), new CultureInfo("fr-FR"), };
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
app.UseRequestLocalization(localizationOptions);
为了在类库中使用本地化字符串,需要注入 IStringLocalizer
最后,在需要使用本地化字符串的类中,使用以下方式获取本地化字符串:
private readonly IStringLocalizer
public SomeClass(IStringLocalizer
{
_localizer = localizer;
}
string localizedString = _localizer.GetString("Hello");
通过以上步骤,您就可以在 ASP.NET Core 5.0 应用程序中处理分离类库的本地化问题。