2016-08-24 59 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

也適用於我...... –