2016-08-24 130 views
6

在试图本地化我的申请,我在这里遵循的步骤: https://docs.asp.net/en/latest/fundamentals/localization.html本地化在ASP.Net MVC的核心不工作 - 无法找到资源文件

这里是我的代码:

启动的.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(其中有一个“产品”条目)。

但是,它无法找到资源文件,并且“产品”永远不会以法语返回。我使用的查询字符串,所以这里的查询字符串:

localhost:3333/my?culture=fr 
在视图, @Localizer["Product"]返回“产品”

也。

任何人都可以请帮助我找到最新消息吗?

编辑:

经过一番研究,我发现,文化得到改变,但它无法找到资源文件。我正在使用VS2015。谁能帮忙?

+0

法国的文化字符串是'fr-FR' –

+0

@JK。还是相同的结果,如果你检查我提供的链接,他们也只使用“fr”文化。 – genericuser

+0

你有没有想过有什么问题? – Simon

回答

20

我有类似的问题。比我想象的 “Localization.AspNetCore.TagHelpers”nuget packag从我的项目中丢失。 它看起来像是QueryStringRequestCultureProvider的必需包。

+1

为我工作谢谢:) – OrcusZ

+1

感谢这*无证*步骤! – Alessandro

+0

也适用于我...... –