[英]How to use localization in asp.net?
I'm trying to figure out how to use the localization in ASP.NET
, I actually followed the Microsoft Documentation but I'm pretty confused right now.我试图弄清楚如何在
ASP.NET
使用本地化,我实际上遵循了 Microsoft 文档,但我现在很困惑。
This is what I did so far:这是我到目前为止所做的:
Inside the Configure
method I added the following code (at the top of all):在
Configure
方法中,我添加了以下代码(在所有顶部):
var supportedCultures = new[]
{
new CultureInfo("it-IT"),
new CultureInfo("en-EN")
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("it"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
so essentially I declared two supported languages, and set the italian to default.所以基本上我声明了两种支持的语言,并将意大利语设置为默认值。
Then inside the ConfigureServices
I specified the ResourcesPath
:然后在
ConfigureServices
我指定了ResourcesPath
:
services.AddLocalization(options => options.ResourcesPath = "Resources");
This is actually the folder content:这实际上是文件夹内容:
I setted for both .resx
file the access modificator to public
, and then inside the _ViewImports.cshtml
I added this:我将
.resx
文件的访问_ViewImports.cshtml
都设置为public
,然后在_ViewImports.cshtml
添加了以下内容:
@using Microsoft.AspNetCore.Mvc.Localization @using Microsoft.AspNetCore.Mvc.Localization
The problem is that when I type @Resources
inside a View
I get:问题是,当我在
View
输入@Resources
时,我得到:
'Resources' is not accessible due to the level of security
由于安全级别,“资源”不可访问
If you want to access the localization strings for controller in the view, you could do it that way:如果你想在视图中访问控制器的本地化字符串,你可以这样做:
@inject IStringLocalizer<HomeController> localazier
After that @localazier["YourKey"]
在那之后
@localazier["YourKey"]
I advise you to create empty class in your project for an example SharedResources
and create specific resx file for it and after that just use it everywhere with @inject IStringLocalizer<SharedResources> localazier
我建议您在您的项目中为示例
SharedResources
创建空类并为其创建特定的 resx 文件,然后在任何地方使用它@inject IStringLocalizer<SharedResources> localazier
Additionally a possible problem is your default culture.另外一个可能的问题是您的默认文化。 Asp.net core looks for the culture into one of this 3 places:
Asp.net core 将文化寻找到以下 3 个地方之一:
QueryStringRequestCultureProvider
CookieRequestCultureProvider
AcceptLanguageHeaderRequestCultureProvider
and only if the culture is empty for any of them it will take your default culture.并且仅当其中任何一个的文化为空时,它才会采用您的默认文化。 So you should turn off
AcceptLanguageHeaderRequestCultureProvider
as possibility, because a lot of user could have defined it in their browser and it is possible to be different from IT.所以你应该关闭
AcceptLanguageHeaderRequestCultureProvider
作为可能性,因为很多用户可能已经在他们的浏览器中定义了它,并且可能与 IT 不同。
This is the way to take it only from query string or cookie, so I advise you to implement it that way.这是仅从查询字符串或 cookie 中获取它的方法,因此我建议您以这种方式实现它。
services.Configure<RequestLocalizationOptions>(options =>
{
options.RequestCultureProviders = new List<IRequestCultureProvider>()
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
};
});
Open up the resx file in design mode (just double click the file) and set the access modifier to public as the following -在设计模式下打开 resx 文件(只需双击文件)并将访问修饰符设置为 public,如下所示 -
I have found this following article a great help when working with localisation -我发现以下这篇文章对本地化工作有很大帮助 -
http://afana.me/archive/2011/01/14/aspnet-mvc-internationalization.aspx/ http://afana.me/archive/2011/01/14/aspnet-mvc-internationalization.aspx/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.