SharedResource在.NetCore 6 MVC中未找到。

huangapple go评论61阅读模式
英文:

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 =&gt; options.ResourcesPath = &quot;Resources&quot;);
  services.AddControllersWithViews()
     .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix,
       options =&gt; { options.ResourcesPath = &quot;Resources&quot;; })
     .AddDataAnnotationsLocalization(options =&gt;
      {
          options.DataAnnotationLocalizerProvider = (type, factory) =&gt; factory.Create(typeof(SharedResource));
       });

then in Configure Method I added the following

 var supportedCulture = new List&lt;CultureInfo&gt;()
        {
            new CultureInfo(&quot;fa-IR&quot;),
            new CultureInfo(&quot;en-US&quot;)
        };
        var options = new RequestLocalizationOptions()
        {
            SupportedCultures = supportedCulture,
            SupportedUICultures = supportedCulture,
            DefaultRequestCulture = new RequestCulture(&quot;fa-IR&quot;),
            RequestCultureProviders = new List&lt;IRequestCultureProvider&gt;()
            {
                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&lt;SharedResource&gt; 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.

huangapple
  • 本文由 发表于 2023年2月19日 17:38:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定