ASP.Net Core MVC中的本地化无法正常工作-无法找到资源文件 [英] Localization in ASP.Net core MVC not working - unable to locate resource file

查看:333
本文介绍了ASP.Net Core MVC中的本地化无法正常工作-无法找到资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试本地化应用程序时,我遵循了以下步骤: https://docs.asp.net/en/latest/fundamentals/localization.html

In trying to localize my application, I've followed the steps here: https://docs.asp.net/en/latest/fundamentals/localization.html

这是我的代码:

Startup.cs

public List<IRequestCultureProvider> RequestCultureProviders { get; private set; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddLocalization(options => options.ResourcesPath = "Resources");

    services.AddMvc()
        .AddViewLocalization(options => options.ResourcesPath = "Resources")
        .AddDataAnnotationsLocalization();

    services.AddOptions();

    services.AddTransient<IViewRenderingService, ViewRenderingService>();

    services.AddTransient<IEmailSender, EmailSender>();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
    app.UseRequestLocalization(locOptions.Value);

    app.UseStaticFiles();
    app.UseFileServer(new FileServerOptions()
    {
        FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory())),
        EnableDirectoryBrowsing = true
    });

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

    app.UseRequestLocalization(new RequestLocalizationOptions
    {
        DefaultRequestCulture = new RequestCulture("fr"),
        // Formatting numbers, dates, etc.
        SupportedCultures = supportedCultures,
        // UI strings that we have localized.
        SupportedUICultures = supportedCultures,
        RequestCultureProviders = new List<IRequestCultureProvider>
        {
           new QueryStringRequestCultureProvider
           {
               QueryStringKey = "culture",
               UIQueryStringKey = "ui-culture"
           }
        }
    });


}

MyController.cs

public class MyController : Controller
{
    private readonly IViewRenderingService _viewRenderingService;
    private IStringLocalizer<MyController> _localizer;
    private MyOptions _options;
    //Constructor for dependency injection principle
    public MyController(IViewRenderingService viewRenderingService, IStringLocalizer<MyController> localizer, IOptions<MyOptions> options)
    {
        _viewRenderingService = viewRenderingService;
        _localizer = localizer;
        _options = options.Value;
    }

    [HttpGet]
    public string Get()
    {
        // _localizer["Name"] 
        return _localizer["Product"];
    }
}

*.resx文件存储在Resources文件夹中,文件名为Controllers.MyController.fr.resx(其中有产品"条目).

The *.resx file is stored in the Resources folder, with the name Controllers.MyController.fr.resx (which has an entry for "Product").

但是,它找不到资源文件,并且永远不会以法语返回"Product".我正在使用querystring,所以这里是查询字符串:

However, it's not able to find the resource file, and "Product" is never returned in French. I am using querystring, so here is the query string:

localhost:3333/my?culture=fr

也在视图中,@Localizer["Product"]返回产品".

Also in the View, @Localizer["Product"] returns "Product".

任何人都可以帮助我找到丢失的内容吗?

Can anyone please help me find whats missing?

经过一番调查,我发现文化正在发生变化,但是找不到资源文件.我正在使用VS2015.有人可以帮忙吗?

After some investigation, I found that culture is getting changed, however it is unable to locate the Resource file. I am using VS2015. can anyone help?

推荐答案

我遇到了类似的问题.比我想像的 我的项目中缺少" Localization.AspNetCore.TagHelpers " nuget packag. 看起来这是QueryStringRequestCultureProvider的必需包.

I had similar problem. Than I figured out that the "Localization.AspNetCore.TagHelpers" nuget packag was missing from my project. It's look like it is a required package for the QueryStringRequestCultureProvider.

这篇关于ASP.Net Core MVC中的本地化无法正常工作-无法找到资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆