【问题标题】:How to get request culture from IStringLocalizer implementation如何从 IStringLocalizer 实现中获取请求文化
【发布时间】:2017-02-21 22:23:25
【问题描述】:

我尝试在 asp.net core 1.1 中创建 IStringLocalizer 的实现,以便为我的整个项目使用每种语言的单个资源文件(而不是每种语言的每页一个资源文件......)。而且我找不到如何在这里获取请求语言以返回具有良好文化的值。

我可能在实现它的方式上迷路了,但是关于这个主题的文档很差。有人可以告诉我如何在这里获得良好的文化(用户的一个 eq 请求用户语言):

    public class CustomLocalizer : IStringLocalizer
{
    private readonly CultureInfo _currentCulture;
    public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
    {
        ResourceManager rm = new ResourceManager(typeof(MyResources));
        foreach(DictionaryEntry value in rm.GetResourceSet(_currentCulture, false, true))
        {
            yield return new LocalizedString((string)value.Key, (string)value.Value);
        }
    }

    public CustomLocalizer(CultureInfo  culture = null)
    {
        _currentCulture = culture ?? CultureInfo.DefaultThreadCurrentUICulture;
    }

    public IStringLocalizer WithCulture(CultureInfo culture)
    {
        return new CustomLocalizer(culture);

    }

    LocalizedString IStringLocalizer.this[string name]
    {
        get
        {
            ResourceManager rm  = new ResourceManager(typeof(MyResources));
            return new LocalizedString(name, rm.GetString(name, _currentCulture));
        }
    }

    LocalizedString IStringLocalizer.this[string name, params object[] arguments]
    {
        get
        {
            ResourceManager rm = new ResourceManager(typeof(MyResources));
            return new LocalizedString(name, rm.GetString(name, _currentCulture));
        }
    }
}

【问题讨论】:

    标签: c# asp.net-mvc localization asp.net-core


    【解决方案1】:

    终于找到了错误在哪里(在我的startup.cs中,在Configure方法中我在设置本地化选项之前使用了AddMvc......)。如果它可以帮助某人,在其他情况下,我会删除这个帖子。

    【讨论】:

      猜你喜欢
      • 2023-01-02
      • 1970-01-01
      • 2012-03-30
      • 2018-07-09
      • 1970-01-01
      • 2021-03-07
      • 2018-10-12
      • 2016-03-13
      • 1970-01-01
      相关资源
      最近更新 更多