[英]How to use localization in Razor Class Library in Asp.Net Core
I have tried to create the Razor Class Library with Asp.Net Core in following project structure:我尝试在以下项目结构中使用 Asp.Net Core 创建 Razor 类库:
I have used in my web application these settings for localization in Startup
class:我在我的 Web 应用程序中使用了这些设置来在
Startup
类中进行本地化:
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);
In Index.cshtml
:在
Index.cshtml
:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
<h1>@Localizer["Title"]</h1>
Unfortunately, the result is only string "Title".不幸的是,结果只是字符串“Title”。 I can't load these resx files from Razor Class Library.
我无法从 Razor 类库加载这些 resx 文件。
How can I use the localization in Razor Class Library like above?如何使用上述 Razor 类库中的本地化?
UPDATE : This is very similiar use case - https://github.com/aspnet/Localization/issues/328 - that provides some example.更新:这是非常相似的用例 - https://github.com/aspnet/Localization/issues/328 - 提供了一些示例。
You appear to have forgotten to configure localization correctly using AddLocalization
您似乎忘记使用
AddLocalization
正确配置本地化
Using details provided from documentation使用文档中提供的详细信息
Reference Globalization and localization in ASP.NET Core参考ASP.NET Core 中的全球化和本地化
Configure localization
配置本地化
Localization is configured in the
ConfigureServices
method:在
ConfigureServices
方法中ConfigureServices
本地化:
services.AddLocalization(options => options.ResourcesPath = "Resources"); //<<< This is required
services
.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
AddLocalization
Adds the localization services to the services container. AddLocalization
将本地化服务添加到服务容器。 The code above also sets the resources path to "Resources".上面的代码还将资源路径设置为“Resources”。
AddViewLocalization
Adds support for localized view files. AddViewLocalization
添加对本地化视图文件的支持。
AddDataAnnotationsLocalization
Adds support for localized DataAnnotations
validation messages through IStringLocalizer
abstractions. 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.本地化中间件在
Configure
方法中启用。 The localization middleware must be configured before any middleware which might check the request culture (for example,app.UseMvcWithDefaultRoute()
).本地化中间件必须在任何可能检查请求文化的中间件之前配置(例如,
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();
The path to the resource file shown in the example image follows the path naming convention given that you are using the ResourcesPath
option which was set to "Resources" .鉴于您使用的是设置为"Resources"的
ResourcesPath
选项,示例图像中显示的资源文件的路径遵循路径命名约定。 This should allow the view to find the resource file in the relative path to the "Resources" folder.这应该允许视图在“Resources”文件夹的相对路径中找到资源文件。
An alternative is to not use the ResourcesPath
option, and place the .resx file in the same folder as the view, following the naming convention of course.另一种方法是不使用
ResourcesPath
选项,并将.resx文件放在与视图相同的文件夹中,当然遵循命名约定。
Base on additional details provided it was indicated that the UI project would be packaged as a nuget package.根据提供的其他详细信息,表明 UI 项目将打包为 nuget 包。
Then have the resources files packaged into the nuget package and have them unpacked to the resources folder of the target project when when installed.然后将资源文件打包到nuget包中,并在安装时将它们解压到目标项目的资源文件夹中。
The resources need to be in the site root to be available to the view, so you then need to reference all the files in your .nuspec
:资源需要位于站点根目录中才能供视图使用,因此您需要引用
.nuspec
所有文件:
<?xml version="1.0"?>
<package>
<metadata>...
</metadata>
<files>
<!-- Add all resource files -->
<file src="Resources\**\*.resx" target="content\Resources" />
</files>
</package>
Reference Creating NuGet packages参考创建 NuGet 包
I haven't tried the accepted answer and based on the comments, it seems the OP didn't get it to work.我还没有尝试接受的答案,根据评论,似乎 OP 没有让它起作用。 I implemented a pattern similar to the View/Page locator pattern that MVC/Razor Pages uses namely, that resources can be provided in a RCL or separate assembly and use
ViewLocalizer
and it'll just find the matching resource string from the highest precedence resource.我实现了一个类似于 MVC/Razor Pages 使用的视图/页面定位器模式的模式,即资源可以在 RCL 或单独的程序
ViewLocalizer
提供,并使用ViewLocalizer
,它只会从最高优先级资源中找到匹配的资源字符串。 You can read my implementation and see if it might work for you.你可以阅读我的实现,看看它是否适合你。
https://terryaney.wordpress.com/2021/01/04/migrating-to-net-core-overridable-localization-in-razor-class-libraries/ https://terryaney.wordpress.com/2021/01/04/migrating-to-net-core-overridable-localization-in-razor-class-libraries/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.