ASPNET Core和带有resx文件的本地化 [英] ASPNET Core and localization with resx files

查看:366
本文介绍了ASPNET Core和带有resx文件的本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法加载我的资源文件,或者其他原因使我的应用程序无法加载正确的值.

I cant get my resource files loaded, or some thing else is keeping my app to load correct values.

这是从我的Startup.cs中获得的:

This is from my Startup.cs:

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

services.Configure<RequestLocalizationOptions>(options =>
{
    var supportedCultures = new[]
    {
        new CultureInfo("da-DK")
    };

    options.DefaultRequestCulture = new RequestCulture(culture: "da-DK", 
    uiCulture: "da-DK");
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
});

这来自我的控制器:

public class CustomerController : Controller
{
    private readonly IHtmlLocalizer<CustomerController> _localizer;

    public CustomerController(IHtmlLocalizer<CustomerController> localizer)
    {
        _localizer = localizer;
    }

    public IActionResult MyAccount()
    {
        string test = Language.MyAccount;
        ViewData["Message"] = _localizer["MyAccount"];

        return View();
    }

我的资源文件位于我的应用程序根目录中名为Resources的文件夹中,并且名为:

My resource files are located in a folder named Resources in the root of my app, and are called:

  • Language.da-DK.resx
  • Language.resx

_localizer ["MyAccount"]; 将返回字符串"MyAccount",就像在本地化中找不到任何内容一样.

The _localizer["MyAccount"]; Will return a string "MyAccount" as if it did not find anything in the localization.

The Language.MyAccount;将返回默认值我的帐户". 没有人会找到我对这把钥匙的丹麦语翻译.

The Language.MyAccount; will return "My account" which is the default value. No one will find my danish translation of this key.

有人可以看到我在做什么吗?

Can anyone see what i am doing wrong?

推荐答案

现在我想通了,部分由Daniel J. G.帮助. 是的,我需要拥有

Now i figured it out, partly helped by Daniel J. G. Yes, i needed to have the

app.UseRequestLocalization(new RequestLocalizationOptions(...))

在我的Startup.cs的配置"部分中.

in the Configure part of my Startup.cs.

但是使_localizer实际上找到资源文件的是更改了resx.designer文件的名称空间.

But the thing that made the _localizer actually find the resource file, was changing the namespace of the resx.designer file.

代替

namespace AO.Customer.Resources

应该是

namespace AO.Customer

名称空间的资源部分是由服务本身添加的.

The Resources part of the namespace was added by the service it self.

感谢丹尼尔

这篇关于ASPNET Core和带有resx文件的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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