未找到本地化资源。部分视图和控制器

localization resource not found. partialViews and controllers

我在这一行中得到 resource not found = true

ViewData["MenuItem_AboutUs"] = localizer["MenuItem_AboutUs"];

我也不确定如何在部分视图上使用本地化,我找不到任何示例。

局部视图

Microsoft.AspNetCore.Mvc.Localization

@inject IViewLocalizer Localizer


<a href="#">@Localizer["MenuItem_AboutUs"]</a>    

启动

services.AddMvc(options =>

            {
    options.Filters.Add(typeof(MyAuthorizeAttribute));

})
                    // Add support for finding localized views, based on file name suffix, e.g. Index.fr.cshtml
                    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                    // Add support for localizing strings in data annotations (e.g. validation messages) via the
                    // IStringLocalizer abstractions.
                    .AddDataAnnotationsLocalization(); services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[]
            {
                new CultureInfo("en"),
                new CultureInfo("fr")
            };

// State what the default culture for your application is. This will be used if no specific culture
// can be determined for a given request.
options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");


// You must explicitly state which cultures your application supports.
// These are the cultures the app supports for formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;


 // These are the cultures the app supports for UI strings, i.e. we have localized resources for.
            options.SupportedUICultures = supportedCultures;
        });
    }
    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

{
    loggerFactory.AddFile("Logs/GWP-{Date}.log");


    var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();

    app.UseRequestLocalization(locOptions.Value);`

控制器

namespace Web.Controllers
{
    public class IndexController : BaseController
    {
        private readonly IAppSettings appSettings;
        private readonly IStringLocalizer<IndexController> localizer;
        public IndexController(IAppSettings appSettings, IStringLocalizer<IndexController> localizer) : base(appSettings)
        {
            this.localizer = localizer;
        }

        [AllowAnonymous]
        public IActionResult Index()
        {
            ViewData["MenuItem_AboutUs"] = localizer["MenuItem_AboutUs"];
            return View();
        }
    }
}`

你快到了。只要保持一致的目录结构。

顺便说一句,你已经在启动时配置了一个 supportedCultures class :

var supportedCultures = new[]
{
    new CultureInfo("en"),
    new CultureInfo("fr")
};

但是您的 resx 文件是:

  • _Header.en.resx
  • _Header.resx
  • _Header.tr.resx

好像有错别字。您应该将最后一个资源文件重命名为 _Header.fr.resx.

详细方法

默认局部视图位于 Views/Shared 文件夹中。您还可以创建自己的部分文件夹:

Views/
    Home/
    Index/
        Index.cshtml
    Shared/
        _HeaderPartial.cshtml
    PartialViews/
        _Header2Partial.cshtml

你的资源目录结构应该是

Resources/
    Controllers/
        IndexController.fr.resx
    Views/
    Shared/
        _HeaderPartial.fr.resx
    PartialViews/
        _Header2Partial.fr.resx

当您想使用定位器时,只需使用命名空间并注入服务即可:

@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer

you can use @Localizer[] now

测试用例:

Views/Shared/_HeaderPartial.cshtml的局部视图:

@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer

<header>This is header from partial : @Localizer["hello world"] </header>

Shared/_HeaderPartial.fr.resx:

|    Name          |     value                                      |
|------------------+------------------------------------------------+
| hello world      |    Bonjour le monde (from `/Shared/` folder)   |

PartialViews/_Header2Partial.cshtml :

|    Name          |     value                                       |
|------------------+-------------------------------------------------+
| hello world      | Bonjour le monde (from `/PartialViews/` folder) |

资源文件:

呈现的页面: