本地化文件无法有效呈现MVC ASP.NET Core 2.2中的Razor页面 [英] Localization file not effective rendering a Razor page in MVC ASP.NET Core 2.2

查看:100
本文介绍了本地化文件无法有效呈现MVC ASP.NET Core 2.2中的Razor页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的剃刀页面看起来像这样.

My Razor page looks like this.

@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
<h1>@Localizer["Index"]</h1>
...

我的 Startup.cs 包含以下内容.

public void ConfigureServices(IServiceCollection services)
{
  ...
  services.AddLocalization(a => a.ResourcesPath = "/");

  services.Configure<RequestLocalizationOptions>(a =>
  {
    CultureInfo[] supportedCultures = {
      new CultureInfo("sv-SE"),
      new CultureInfo("se")
    };
    a.DefaultRequestCulture = new RequestCulture("se");
    a.SupportedCultures = supportedCultures;
    a.SupportedUICultures = supportedCultures;
  });
  ...
}

我直接在项目的根目录中放置了一个名为 Controllers.HomeController.se.resx 的文件.控制器 HomeController 包含注入.

I placed a file called Controllers.HomeController.se.resx directly in the project's root. The controller HomeController contains the injection.

public class HomeController : Controller
{
  private readonly Context _context;
  private readonly IStringLocalizer<HomeController> _localizer;

  public HomeController(Context context, IStringLocalizer<HomeController> localizer)
  {
    _context = context;
    _localizer = localizer;
  }
  ...
}

该应用程序不会崩溃,但是重新定义的字符串为 Index ,而不是RESX文件中的值.我尝试遵循文档尽可能接近,但显然我错过了一些东西.我需要帮助才能找到答案.

The application doesn't crash but the string renedered is Index and not the value from the RESX file. I've tried to follow the docs as closely as possible but apparently I've missed something. I need help finding what that would be.

我断点了并检查了构造函数中 _localizer ["Index"] 的值.如预期的那样,未找到文件的标志设置为 true .检查 SearchedLocation 的值会得到 Web ... Controllers.MemberController .我无法确定这三个点是否是项目根目录中RESX文件的正确点.我也希望 se 出现在名称中.

I breakpointed and checked the value of _localizer["Index"] in the constructor. As expected, the flag for the file not being found is set to true. Checking the value of SearchedLocation gives me Web...Controllers.MemberController. I can't tell if those three dots is the correct one for the RESX file in the project's root. I was expecting se somewhere in the name too.

推荐答案

如果要将资源文件放置在项目的根目录中,则应将ResourcesPath设置如下:

If you want to place you resources files in root of the project you should set ResourcesPath as following

services.AddLocalization(a => a.ResourcesPath = ""); //empty string

使用此设置,SearchedLocation将为您提供Web.Controllers.MemberController,它指向项目根目录中的Controllers.MemberController.resx文件.

With this settings SearchedLocation will give you Web.Controllers.MemberController which points to Controllers.MemberController.resx file in the root of the project.

要在视图中使用本地化,必须遵循Views.{ControllerName}.{ViewName}.resx模式.例如,如果其中具有HomeControllerAbout视图,则需要具有Views.Home.About.resx文件才能使用本地化.

To use localization in view you have to follow Views.{ControllerName}.{ViewName}.resx pattern. For example if you have HomeController and About view in it you need to have Views.Home.About.resx file to use localization.

搜索本地化文件时遵循的另一个约定资源阅读器是在各个文件夹中搜索文件,而不是按点分隔的名称进行搜索.例如,如果ResourcesPath设置为"Resources",则以下变体相同

Another convention resource reader follows when searching for localization files is searching the files in respective folders rather than by dot separated names. For example if ResourcesPath is set to "Resources" the following variants are equal

Resources.Views.Home.About.resx
Resources\Views.Home.About.resx
Resources\Views\Home.About.resx
Resources\Views\Home\About.resx

因此可以通过文件夹来组织本地化文件.

So it's possible to structure your localization files by folders.

并且您未指定在Startup.cs中添加了app.UseRequestLocalization().如果您不这样做,则您的应用程序将无法确定请求区域性,它将始终指向默认资源文件.在文档中阅读更多 /a>.

And you didn't specify you added app.UseRequestLocalization() in your Startup.cs. If you don't do this your application won't be able to determine request culture and it will always point to default resource file. Read more in the docs.

注意

通过services.Configure<RequestLocalizationOptions>或将构造的选项对象(或委托)传递给app.UseRequestLocalization,有两种方法配置RequestLocalizationOptions进行请求本地化.实际上,这些方法之间没有区别,就本地化中间件而言,它们是完全相等的.但是,如果在应用程序的任何时候都需要获取RequestLocalizationOptions,则将无法获取传递给app.UseRequestLocalization的值.但是很容易理解services.Configure<RequestLocalizationOptions>(这是在文档中)

There are 2 ways of configuring RequestLocalizationOptions for request localization, via services.Configure<RequestLocalizationOptions> or passing constructed options object (or delegate) to app.UseRequestLocalization. Effectively there is no difference between these approaches, they are totally equal in terms of localization middleware. But if at any point of an application you need to get RequestLocalizationOptions you won't be able to get value passed to app.UseRequestLocalization. But it is easy to accompish with services.Configure<RequestLocalizationOptions> (it's general approach described in the docs)

public class HomeController : Controller
{
    private readonly RequestLocalizationOptions _requestLocalizationOptions;

    public HomeController(IOptions<RequestLocalizationOptions> options)
    {
        _requestLocalizationOptions = options.Value;
    }

    //..
}

这篇关于本地化文件无法有效呈现MVC ASP.NET Core 2.2中的Razor页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆