目前的文化没有任何表现形式

时间:2017-06-04 16:07:03

标签: c# asp.net-core embedded-resource

教自己.Net Core asp。

我已将资源文件添加到我的解决方案中。 我在此资源文件中添加了一个测试字符串。 我现在正试图在我的控制器中访问此测试字符串/键。

所以,在我的startup.cs中我有这个:

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

    services.AddMvc()
       .AddViewLocalization(
            LanguageViewLocationExpanderFormat.Suffix,
                opts => { opts.ResourcesPath = "Resources"; })
    .AddDataAnnotationsLocalization();

    services.Configure<RequestLocalizationOptions>(
        opts =>
        {
            var supportedCultures = new List<CultureInfo>
            {
                    new CultureInfo("en-GB"),
                    new CultureInfo("en-US"),
                    new CultureInfo("en"),
                    new CultureInfo("fr-FR"),
                    new CultureInfo("fr"),
            };

            opts.DefaultRequestCulture = new RequestCulture("en");
            // Formatting numbers, dates, etc.
            opts.SupportedCultures = supportedCultures;
            // UI strings that we have localized.
            opts.SupportedUICultures = supportedCultures;
        });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, WorkerContext context)
{
    app.UseStaticFiles();

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

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

在我的控制器中我有这个:

public class HomeController : Controller
{
    private readonly IStringLocalizer<HomeController> _localizer;
    public HomeController(IStringLocalizer<HomeController> localizer)
    {
        _localizer = localizer;
    }
}

我的资源文件存储在这里:

enter image description here

此资源文件的我的属性是: enter image description here

我在这里设置了一个断点:

_localizer = localizer;

我检查这个变量,你看到找不到清单...... enter image description here

我不理解的是什么?

2 个答案:

答案 0 :(得分:2)

第1期

基于https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization

您应该将Resources放在根目录下名为Resources的文件夹中。你有Properties文件夹......

此代码块(与您的略有不同)
Statup.cs

services.AddLocalization(options => options.ResourcesPath = "Resources");

在我的项目中为我解析为该文件夹。

enter image description here

第2期

另一个问题是你的命名没有做好。看一下名为Working With Resource Files的部分,它会比你把它放在这里更好地向你解释。

答案 1 :(得分:2)

在我完成所有步骤时,您似乎只是描述了我的问题,但事实并非如此。

我注意到本地化需要扩展类,但该扩展的DLL不在我的依赖项中。无论如何,我使用NuGet来安装几个依赖项,我的项目就像一个魅力。尝试安装以下内容并重试。

Microsoft.Extensions.Localization, Microsoft.AspNetCore.Mvc.Localization