ASP Core 2.0本地化程序找不到资源文件

时间:2018-01-13 01:36:13

标签: c# asp.net-core localization

我有一个asp核心应用程序,我正在尝试本地化。我会直接进入它。

在startup.cs中 - ConfigureServices:

        services.AddLocalization(options => options.ResourcesPath = "Resources");

        services.AddMvc()
            .AddViewLocalization(opts => 
                { opts.ResourcesPath = "Resources"; })
            .AddDataAnnotationsLocalization();

startup.cs - configure:

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

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

我的资源文件是有序的。以fr为例(对于HomeControllerHome操作): {root}/Resources/Views/Home/Home.fr.resx

这是一个不起作用的视图:

@using Microsoft.AspNetCore.Mvc.Localization

@inject IViewLocalizer Localizer

@{
    ViewData["Title"] = @Localizer["test"];
}

<div class="row">
    <div class="col-lg-6">
        <div class="panel panel-default">
            <div class="panel-heading">My Toolboxes</div>
            <div class="panel-body">
            </div>
        </div>
    </div>
    <div class="col-lg-6">
        <div class="panel panel-default">
            <div class="panel-heading">Notices</div>
            <div class="panel-body">
                Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            </div>
        </div>
    </div>
</div>

Title总是&#34;测试&#34;,它应该是&#34; essai&#34;。

当我检查Localizer["test"]时,IsResourceNotFound = true

我100%确定chrome正在请求fr区域设置:

GET /Home/Home HTTP/1.1
Host: localhost:62677
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: fr,en-US;q=0.9,en;q=0.8

3 个答案:

答案 0 :(得分:2)

我遇到了同样的错误,在我的情况下,它在进入项目设置并将程序集名称更改为与默认名称空间完全相同的值后得到了解决。 (我的程序集名称为<MyProjectName>,而我的默认名称空间设置为<MyCompany.MyProjectName>)。在内部,LocalizerFactory似乎创建了基于程序集名称查找资源的路径。如果资源的命名空间不以程序集名称开头,则找不到它们。

答案 1 :(得分:0)

services.AddLocalization(options => options.ResourcesPath = "Resources");

    services.AddMvc()
        .AddViewLocalization(opts => 
            { opts.ResourcesPath = "Resources"; })
        .AddDataAnnotationsLocalization();

可能需要

services.AddLocalization(options => options.ResourcesPath = "Resources");

services.AddMvc()
    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)  // <<< looks like intellisense failed you here...
    .AddDataAnnotationsLocalization();

答案 2 :(得分:0)

我认为问题在于您是直接在应用中设置受支持的区域性。并且受支持的区域性未注册为服务配置的一部分。因此找不到文化。
首先在ConfigureServices中注册所需的区域性。然后在配置中访问这些区域性以将其设置为应用程序。参见下面的代码:

public void ConfigureServices(IServiceCollection services)
        {
            //removed other configurations

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

            // Register cultures here
            services.Configure<RequestLocalizationOptions>(
                opts =>
                {
                    var supportedCultures = new List<CultureInfo>
                    {
                        new CultureInfo("en-GB"),
                        new CultureInfo("en-US"),
                        new CultureInfo("fr-FR"),
                    };

                    opts.DefaultRequestCulture = new RequestCulture("en-US");
                    opts.SupportedCultures = supportedCultures;
                    opts.SupportedUICultures = supportedCultures;
                });

            services.AddMvc()
           
// other registrations
        }
        
public void Configure(IApplicationBuilder app,
            IHostingEnvironment env)
{
                       
// Access the registered cultures here and set it to the app

  app.UseRequestLocalization(app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);

            app.UseMvc();
}