在实现多语言ASP.NET核心MVC项目时找不到资源文件

时间:2018-04-14 14:07:04

标签: c# asp.net-core

我有ASP.NET核心项目,在这个项目中,我想要实现多语言应用程序。 对于我的问题,我检查这个问题Views are localized but resources are not found,但我找不到正确的答案。 提到的问题是关于本地化的视图和不同的配置。

所以这是我的代码,无法在控制器中找到本地化资源文件

资源文件位置:

/Resources/Controllers/HomeController.fa.resx

resource file location

启动:

 public class Startup
{
    ..... 

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

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
       .....

        app.UseStaticFiles();

        IList<CultureInfo> supportedCultures = new List<CultureInfo>
        {
                new CultureInfo("en-US"),
                new CultureInfo("fa-IR"),
        };

        var options = new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture("fa-IR"),
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures
        };
        app.UseRequestLocalization(options);

       ....
    }

..... 


}

HomeController中:

 public class HomeController : Controller
{
    .....

    private readonly IStringLocalizer<HomeController> _stringLocalizer;

    public HomeController(IStringLocalizer<HomeController> stringLocalizer)
    {
        _stringLocalizer = stringLocalizer;
    }

    public IActionResult About()

    {
        ViewData["Message"] = _stringLocalizer["Hello"];
        return View();
    }

     .....

}

这是我的测试:

request method

the result of request

1 个答案:

答案 0 :(得分:1)

最终3小时后,我能够解决此问题,要解决此问题,需要进行以下更改:

启动:

public class Startup
{
  ......

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLocalization(o => { o.ResourcesPath = "Resources"; });
        services.AddMvc();
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
            app.UseRequestLocalization();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        IList<CultureInfo> supportedCultures = new List<CultureInfo>
        {
                new CultureInfo("en-US"),
                new CultureInfo("fa-IR"),
        };
        var options = new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture("fa-IR","fa-IR"),
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures
        };

        app.UseRequestLocalization(options);
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

我的资源名称和路径更改如下图所示:

enter image description here

并且这种变化的结果是:

enter image description here