ASP.NET核心全球化和本地化 - 不加载该值

时间:2017-03-08 01:03:24

标签: localization asp.net-core globalization

您好我正在尝试实现ASP.NET Core的本地化但我无法返回已翻译的值 - 它总是返回"键"价值 - "加载"。当我打印文化时 - 它会返回" fr",因此文化设置正确。老实说,我没有想法......

我正在关注本教程: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization

也看这个样本: https://github.com/aspnet/Entropy/tree/dev/samples/Localization.StarterWeb

当我构建项目时,资源文件被编译并复制到: 的 OBJ /调试/ netcoreapp1.1 / BoilerPlate.Resources.Controllers.ValuesController.fr.resources

仓/调试/ netcoreapp1.1 / FR / BoilerPlate.resources.dll

$ dotnet --info
.NET Command Line Tools (1.0.0-rc3-004530)

Product Information:
 Version:            1.0.0-rc3-004530
 Commit SHA-1 hash:  0de3338607
  

Startup.cs

 public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
        services.AddLocalization(options => options.ResourcesPath = "Resources");

        services.Configure<RequestLocalizationOptions>(options =>
       {
           var supportedCultures = new[]
           {
                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: "fr", uiCulture: "fr");

           // 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.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseStatusCodePages();
        app.UseDeveloperExceptionPage();

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

        app.UseMvc();
    }
  

ValuesController.cs

private readonly IStringLocalizer<ValuesController> _localizer;
        public ValuesController(IStringLocalizer<ValuesController> localizer)
        {
            _localizer = localizer;
        }

 // GET api/values/5
        [HttpGet("{id}")]
        public string Get(int id)
        {

            var rqf = Request.HttpContext.Features.Get<IRequestCultureFeature>();
            var culture = rqf.RequestCulture.Culture;
            System.Console.WriteLine($"Culture: {culture}");

            return _localizer["Load"];
            // return "value";
        }
  

资源/ Controllers.ValuesController.fr.resx

...
  <data name="Load" xml:space="preserve">
    <value>Load this value!</value>
  </data>
...

1 个答案:

答案 0 :(得分:1)

我认为问题在于与资源相关的文件夹结构。您应该将资源存储在此结构中:

资源&gt;控制器&gt; YourController.fr.resx

资源&gt;意见&gt; YourController&gt; YourView.fr.resx

另外,我错过了services.AddMvc()。AddViewLocalization();在启动时本地化视图。

修改

我刚刚对一个新项目进行了测试。这是Startup.cs:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddLocalization(options => options.ResourcesPath = "Resources");

        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseRequestLocalization(BuildLocalizationOptions());

        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseMvc();
    }

    private RequestLocalizationOptions BuildLocalizationOptions()
    {
        var supportedCultures = new List<CultureInfo>
        {
            new CultureInfo("fr")
        };

        var options = new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture("fr"),
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures
        };

        // this will force the culture to be fr. 
        // It must be changed to allow multiple cultures, but you can use to test now
        options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context =>
        {
            var result = new ProviderCultureResult("fr", "fr");

            return Task.FromResult(result);
        }));

        return options;
    }

希望这会对你有所帮助。

此致