英文:
SharedResource not found in .NetCore 6 MVC
问题
在.NET Core 6的MVC应用程序中进行本地化。我可以在视图部分完成,但无法使用sharedResource.resx文件。所以我按照以下步骤进行操作:
在Startup.cs文件中,在ConfigureServices方法中,我添加了以下代码:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix,
options => { options.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(typeof(SharedResource));
});
然后,在Configure方法中,我添加了以下代码:
var supportedCultures = new List<CultureInfo>()
{
new CultureInfo("fa-IR"),
new CultureInfo("en-US")
};
var options = new RequestLocalizationOptions()
{
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures,
DefaultRequestCulture = new RequestCulture("fa-IR"),
RequestCultureProviders = new List<IRequestCultureProvider>()
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
}
};
app.UseRequestLocalization(options);
接下来,我在应用程序的Model文件夹中创建了名为"SharedResource"的类。然后,我在应用程序的根级别创建了一个名为"Resources"的文件夹。在"Resources"文件夹下,我创建了两个资源文件,分别命名为"SharedResource.en-US"和"SharedResource.fa-IR",并在其中添加了元素。
最后,我在视图的顶部添加了以下代码:
@using System.Globalization
@using Microsoft.AspNetCore.Mvc.Localization
@inject IHtmlLocalizer<SharedResource> SharedLocalizer
但是当我尝试使用@SharedLocalizer["Year"]
获取值时,它无法正常工作,显示找不到"SharedResource"的错误。
问题可能出在哪里?
英文:
I am trying to do localication on .Net Core 6, MVC application. I could have done in view section, but I can not have a sharedResource.resx. so I did as following
in Startup , in ConfigureServices I added the following
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix,
options => { options.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(typeof(SharedResource));
});
then in Configure Method I added the following
var supportedCulture = new List<CultureInfo>()
{
new CultureInfo("fa-IR"),
new CultureInfo("en-US")
};
var options = new RequestLocalizationOptions()
{
SupportedCultures = supportedCulture,
SupportedUICultures = supportedCulture,
DefaultRequestCulture = new RequestCulture("fa-IR"),
RequestCultureProviders = new List<IRequestCultureProvider>()
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
}
};
app.UseRequestLocalization(options);
Then I created the class File named "SharedResource" in Model folder of the application.
Then I created a folder named "Resources" on root level of the application.
Exactly under the Resources Folder I created two resource files. name SharedResource.en-US and SharedResource.fa-IR, with elements inside of them.
The I addedd these lines above the view
@using System.Globalization
@using Microsoft.AspNetCore.Mvc.Localization
@inject IHtmlLocalizer<SharedResource> SharedLocalizer
but when I want to have the value of @SharedLocalizer["Year"] , it doesn't work and shows that it has not found the sharedresource
[![enter image description here][1]][1]
where is the my problem?
[1]: https://i.stack.imgur.com/nuG56.jpg
答案1
得分: 0
SharedResource类文件应该正好位于根目录下。这解决了问题。
英文:
the class SharedResource class file should be exactly under the root directory. that solved the problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论