如何在ASP.NET Core中以强类型的方式获取资源字符串? [英] How to get resource strings in strongly typed way in asp.net core?

查看:53
本文介绍了如何在ASP.NET Core中以强类型的方式获取资源字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下程序中,为了获取资源字符串,我正在使用_localizer ["About Title"],其中"About Title"是一个魔术字符串.如何避免使用这样的字符串?有什么强类型的方式吗?

In the following program, in order to get resource strings i am using _localizer["About Title"] where "About Title" is a magic string. How to avoid using strings like this? Is there any strongly typed way?

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;

namespace Localization.StarterWeb.Controllers
{
    [Route("api/[controller]")]
    public class AboutController : Controller
    {
        private readonly IStringLocalizer<AboutController> _localizer;

        public AboutController(IStringLocalizer<AboutController> localizer)
        {
            _localizer = localizer;
        }

        [HttpGet]
        public string Get()
        {
            return _localizer["About Title"];
        }
    }
}

推荐答案

如果您尝试避免使用硬编码的字符串(键)来查找本地化转换,则可以创建一个 LocalizationKeys 类,包含适合您的查找键.然后,您可以利用 C#6 nameof 运算符.这将有助于减轻对魔术弦"的担忧.

If you're trying to avoid using a hardcoded string (key) to lookup the localization conversion you could create a LocalizationKeys class that contains the lookup keys for you. You could then leverage the C# 6 nameof operator. This would help alleviate the concern of "magic strings".

public static class LocalizationKeys
{
    public const string AboutTitle = nameof(AboutTitle); // Note: this is "AboutTitle"

    // ... other keys here
}

然后您可以在任何地方使用它.好处之一是,由于这是类的成员,因此,如果对密钥进行了更改,则可以使用通用的重构工具来安全地替换对它的所有引用,而不必尝试在魔术字符串"上进行全局字符串替换.另一个好处是您可以在访问类时使用智能感知.我想可以认为这是强类型".

Then you could consume it wherever. One of the benefits are that since this is a member of the class, if the key changes you can use common refactoring tools to safely replace all the references to it rather than trying to do a global string replace on the "magic string". Another benefit is you'd get to use intellisense when accessing the class. I suppose one could consider this "strongly typed".

您会这样消费它:

[Route("api/[controller]")]
public class AboutController : Controller
{
    private readonly IStringLocalizer<AboutController> _localizer;

    public AboutController(IStringLocalizer<AboutController> localizer)
    {
        _localizer = localizer;
    }

    [HttpGet]
    public string Get()
    {
        return _localizer[LocalizationKeys.AboutTitle];
    }
}

如果您真的想使用 C#6 ,也可以使用静态用法.这将允许您引用指定类型的成员.最后,对于简单的单行"返回,我们可以使它们成为表达式主体.例如:

If you really want to get fancy with C# 6 you can also utilize a static using. This will allow you to reference the members of the type you specify. Finally, for simple "single line" returns, we can make them expression bodies. For example:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using static Localization.StarterWeb.LocalizationKeys; // Note: static keyword

namespace Localization.StarterWeb.Controllers
{
    [Route("api/[controller]")]
    public class AboutController : Controller
    {
        private readonly IStringLocalizer<AboutController> _localizer;

        public AboutController(IStringLocalizer<AboutController> localizer)
        {
            _localizer = localizer;
        }

        [HttpGet]
        public string Get() => _localizer[AboutTitle]; // Note: omission of qualifier
    }
}

这篇关于如何在ASP.NET Core中以强类型的方式获取资源字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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