找不到本地化资源。局部视图和控制器

时间:2018-12-12 16:26:35

标签: asp.net-core location asp.net-core-mvc

我在这一行中找不到资源= true

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

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

PartialView

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();
        }
    }
}`

enter image description here

1 个答案:

答案 0 :(得分:1)

您快到了。只需保持一致的目录结构即可。

顺便说一下,您已经在启动类中配置了supportedCultures

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) |

资源文件:

enter image description here

呈现的页面:

enter image description here