Razor .resx 视图本地化

Razor .resx view localization

如何轻松地本地化视图(此文件中只有 .cshtml 和 javascript)。 请帮助我,我已经花了大约 5 个小时得到 0 个结果,官方文档过于复杂 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.1 一切容易的都已经过时了。

我无法为 resx 文件创建名称

视图名为IndexPage.cshtml,资源文件名为SharedResource.en-US.resx

使用这个

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

打印我只是你好,而不是这个的翻译。

它打印Hello的原因是因为它cannot find匹配the class or view you want to localize的本地化资源文件,当它找不到资源name/key时它会打印文本(键)原样。

有两种本地化方法:

A) 如果您决定采用官方文档中提到的方式 (using Localizer/IStringLocalizer) 您需要执行以下操作:

  1. 创建一个 Resources 文件夹并在其中放入 .resx files 并使用正确的命名约定 :

You need to name your .resx files to mimic the path of their associated view/class file that you want to localize.

E.g.: if you have a HomeController with Index action, and you have a view for that Index located under Views/Home/Index.cshtml you then need to name your resx file as: views.home.index.en-US.resx

or you can create same folder structure as the views/controller inside your resources folder, e.g.: Resources/Views/Home/Index.en.resx.

  1. 在您的 startup.cs 中,您需要注册所需的服务以支持本地化并通过以下方式配置它们:
//in your ConfigureServices method

services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });

services.AddMvc()
     .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
     .AddDataAnnotationsLocalization();

和:

//in your Configure method

var supportedCultures = new[]
{
//add the cultures you support..
     new CultureInfo("en-US"),
     new CultureInfo("fr"),
};

app.UseRequestLocalization(new RequestLocalizationOptions
{
     // Set the default culture.
     DefaultRequestCulture = new RequestCulture("en-US"), 
     // Formatting numbers, dates, etc.
     SupportedCultures = supportedCultures,
     // UI strings that we have localized.
     SupportedUICultures = supportedCultures
});
  1. 当收到请求时,它会通过 specific middleware 来决定使用哪种文化 using one of these providers:

    • QueryStringRequestCultureProvider.
    • CookieRequestCultureProvider。大多数应用程序使用这种机制来保持用户文化。
    • AcceptLanguageHeaderRequestCultureProvider.
  2. 运行 您的应用并使用 QueryStringRequestCultureProvider 对其进行测试,例如:https://localhost:5001/?culture=fr

Note: You need to have a culture fallback behavior which will handle any unsupported cultures by having a .resx file without .Language_Code prefix, e.g.: views.home.index.resx which basically contains the main language of your app.


B) 如果你觉得方法 A 太多了:

如果您只想拥有特定数量的 .resx 个文件,例如:SharedResource.resxSharedResource.fr.resxMessages.resxMessages.fr.resxErrors.resx , Errors.fr.resx, 然后你可以在你的 Resources folder.

中创建那些 .resx files

然后在你的 startup.cs:

//in your ConfigureServices method
services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
services.AddMvc();

和:

//in your Configure method

var supportedCultures = new[]
{
//add the cultures you support..
     new CultureInfo("en-US"),
     new CultureInfo("fr"),
};

app.UseRequestLocalization(new RequestLocalizationOptions
{
     // Set the default culture.
     DefaultRequestCulture = new RequestCulture("en-US"), 
     // Formatting numbers, dates, etc.
     SupportedCultures = supportedCultures,
     // UI strings that we have localized.
     SupportedUICultures = supportedCultures
});

最后,您可以使用资源文件名访问资源文件,例如:@SharedResource.Hello

Note: you need to reference Resources in your _ViewImports.cshtml to be able to use them in your views.


我知道这是一个很长的答案,但我想为您提供 2 种不同的方法,以便您可以决定哪种方法最适合您的用例。