如何在 Asp.Net Core 中的 Razor Class 库中使用本地化
How to use localization in Razor Class Library in Asp.Net Core
我尝试在以下项目结构中创建具有 Asp.Net 核心的 Razor Class 库:
我在我的 Web 应用程序中使用了这些设置以在 Startup
class 中进行本地化:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddViewLocalization(
LanguageViewLocationExpanderFormat.Suffix,
opts => { opts.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(
opts =>
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("en")
};
opts.DefaultRequestCulture = new RequestCulture("en");
opts.SupportedCultures = supportedCultures;
opts.SupportedUICultures = supportedCultures;
});
....
var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(options.Value);
在Index.cshtml
中:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
<h1>@Localizer["Title"]</h1>
不幸的是,结果只是字符串"Title"。我无法从 Razor Class 库加载这些 resx 文件。
如何像上面那样使用 Razor Class 库中的本地化?
UPDATE:这是非常相似的用例 - https://github.com/aspnet/Localization/issues/328 - 提供了一些示例。
您似乎忘记使用 AddLocalization
正确配置本地化
使用文档中提供的详细信息
引用Globalization and localization in ASP.NET Core
Configure localization
Localization is configured in the ConfigureServices
method:
services.AddLocalization(options => options.ResourcesPath = "Resources"); //<<< This is required
services
.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
AddLocalization
将本地化服务添加到服务容器。上面的代码还将资源路径设置为“Resources”。
AddViewLocalization
添加对本地化视图文件的支持。
AddDataAnnotationsLocalization
通过 IStringLocalizer
抽象添加对本地化 DataAnnotations
验证消息的支持。
Localization middleware
The current culture on a request is set in the localization Middleware. The localization middleware is enabled in the Configure
method. The localization middleware must be configured before any middleware which might check the request culture (for example, app.UseMvcWithDefaultRoute()
).
var supportedCultures = new[] {
new CultureInfo("en-US"),
new CultureInfo("en")
};
app.UseRequestLocalization(new RequestLocalizationOptions{
DefaultRequestCulture = new RequestCulture("en"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures;
});
//...other middleware
app.UseMvcWithDefaultRoute();
示例图像中显示的资源文件路径遵循路径命名约定,因为
您正在使用设置为 “资源” 的 ResourcesPath
选项。这应该允许视图在“资源”文件夹的相对路径中找到资源文件。
另一种方法是不使用 ResourcesPath
选项,并将 .resx 文件放在与视图相同的文件夹中,当然要遵循命名约定。
根据提供的其他详细信息,表明 UI 项目将打包为 nuget 包。
然后将资源文件打包到nuget包中,安装时解压到目标项目的resources文件夹下
资源需要位于站点根目录中才能供视图使用,因此您需要引用 .nuspec
:
中的所有文件
<?xml version="1.0"?>
<package>
<metadata>...
</metadata>
<files>
<!-- Add all resource files -->
<file src="Resources\**\*.resx" target="content\Resources" />
</files>
</package>
我还没有尝试接受的答案,根据评论,OP 似乎没有让它起作用。我实现了一个类似于 MVC/Razor 页面使用的 View/Page 定位器模式的模式,即可以在 RCL 或单独的程序集中提供资源并使用 ViewLocalizer
,它只会找到匹配的来自最高优先级资源的资源字符串。您可以阅读我的实现,看看它是否适合您。
我尝试在以下项目结构中创建具有 Asp.Net 核心的 Razor Class 库:
我在我的 Web 应用程序中使用了这些设置以在 Startup
class 中进行本地化:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddViewLocalization(
LanguageViewLocationExpanderFormat.Suffix,
opts => { opts.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(
opts =>
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("en")
};
opts.DefaultRequestCulture = new RequestCulture("en");
opts.SupportedCultures = supportedCultures;
opts.SupportedUICultures = supportedCultures;
});
....
var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(options.Value);
在Index.cshtml
中:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
<h1>@Localizer["Title"]</h1>
不幸的是,结果只是字符串"Title"。我无法从 Razor Class 库加载这些 resx 文件。
如何像上面那样使用 Razor Class 库中的本地化?
UPDATE:这是非常相似的用例 - https://github.com/aspnet/Localization/issues/328 - 提供了一些示例。
您似乎忘记使用 AddLocalization
使用文档中提供的详细信息
引用Globalization and localization in ASP.NET Core
Configure localization
Localization is configured in the
ConfigureServices
method:
services.AddLocalization(options => options.ResourcesPath = "Resources"); //<<< This is required
services
.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
AddLocalization
将本地化服务添加到服务容器。上面的代码还将资源路径设置为“Resources”。
AddViewLocalization
添加对本地化视图文件的支持。
AddDataAnnotationsLocalization
通过 IStringLocalizer
抽象添加对本地化 DataAnnotations
验证消息的支持。
Localization middleware
The current culture on a request is set in the localization Middleware. The localization middleware is enabled in the
Configure
method. The localization middleware must be configured before any middleware which might check the request culture (for example,app.UseMvcWithDefaultRoute()
).
var supportedCultures = new[] {
new CultureInfo("en-US"),
new CultureInfo("en")
};
app.UseRequestLocalization(new RequestLocalizationOptions{
DefaultRequestCulture = new RequestCulture("en"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures;
});
//...other middleware
app.UseMvcWithDefaultRoute();
示例图像中显示的资源文件路径遵循路径命名约定,因为
您正在使用设置为 “资源” 的 ResourcesPath
选项。这应该允许视图在“资源”文件夹的相对路径中找到资源文件。
另一种方法是不使用 ResourcesPath
选项,并将 .resx 文件放在与视图相同的文件夹中,当然要遵循命名约定。
根据提供的其他详细信息,表明 UI 项目将打包为 nuget 包。
然后将资源文件打包到nuget包中,安装时解压到目标项目的resources文件夹下
资源需要位于站点根目录中才能供视图使用,因此您需要引用 .nuspec
:
<?xml version="1.0"?>
<package>
<metadata>...
</metadata>
<files>
<!-- Add all resource files -->
<file src="Resources\**\*.resx" target="content\Resources" />
</files>
</package>
我还没有尝试接受的答案,根据评论,OP 似乎没有让它起作用。我实现了一个类似于 MVC/Razor 页面使用的 View/Page 定位器模式的模式,即可以在 RCL 或单独的程序集中提供资源并使用 ViewLocalizer
,它只会找到匹配的来自最高优先级资源的资源字符串。您可以阅读我的实现,看看它是否适合您。