带有 json 文件的 Asp.net Core 本地化 [英] Asp.net Core localization with json files

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

问题描述

我正在努力寻找一个关于 asp.net 本地化的好教程.在官方文档中,解释是关于.resx文件,我想使用json文件.

I'm trying to find a good tutorial about asp.net localization. In the official documentation, the explanation are about .resx files and I want to use json files.

如果有人有关于如何编写中间件的好教程.

If someone have a good tutorial on how to write the middleware to do that.

谢谢

推荐答案

Nuget 包

https://www.nuget.org/packages/Askmethat.Aspnet.JsonLocalizer/

解决方案

经过一番调查,终于在Asp/Localization GitHub中找到了一个例子.

After some investigations, I finally find an example in Asp/Localization GitHub.

我在这里为那些不想在不破坏默认文化提供程序的情况下使用平面 json 的人提供.

I provide here for people that wan't to use a flat json without breaking default culture provider.

数据:

平面 json :

[
  {
    "Key": "Hello",
    "LocalizedValue" : {
      "fr-FR": "Bonjour",
      "en-US": "Hello"
    }
  }
]

C# 模型:

class JsonLocalization
    {
        public string Key { get; set; }
        public Dictionary<string, string> LocalizedValue = new Dictionary<string, string>();

    }

中间件

工厂

这只是为了访问CultureInfo 是StringLocalizer.

 public class JsonStringLocalizerFactory : IStringLocalizerFactory
    {
        public IStringLocalizer Create(Type resourceSource)
        {
            return new JsonStringLocalizer();
        }

        public IStringLocalizer Create(string baseName, string location)
        {
            return new JsonStringLocalizer();
        }
    }

定位器

从 JSON 文件中获取数据的逻辑

public class JsonStringLocalizer : IStringLocalizer
{
    List<JsonLocalization> localization = new List<JsonLocalization>();
    public JsonStringLocalizer()
    {
        //read all json file
        JsonSerializer serializer = new JsonSerializer();
        localization = JsonConvert.DeserializeObject<List<JsonLocalization>>(File.ReadAllText(@"localization.json"));

    }


    public LocalizedString this[string name]
    {
        get
        {
            var value = GetString(name);
            return new LocalizedString(name, value ?? name, resourceNotFound: value == null);
        }
    }

    public LocalizedString this[string name, params object[] arguments]
    {
        get
        {
            var format = GetString(name);
            var value = string.Format(format ?? name, arguments);
            return new LocalizedString(name, value, resourceNotFound: format == null);
        }
    }

    public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
    {
        return localization.Where(l => l.LocalizedValue.Keys.Any(lv => lv == CultureInfo.CurrentCulture.Name)).Select(l => new LocalizedString(l.Key, l.LocalizedValue[CultureInfo.CurrentCulture.Name], true));
    }

    public IStringLocalizer WithCulture(CultureInfo culture)
    {
        return new JsonStringLocalizer();
    }

    private string GetString(string name)
    {
        var query = localization.Where(l => l.LocalizedValue.Keys.Any(lv => lv == CultureInfo.CurrentCulture.Name));
        var value = query.FirstOrDefault(l => l.Key == name);
        return value.LocalizedValue[CultureInfo.CurrentCulture.Name];
    }
}

通过这个解决方案,您可以在 ViewsControllers 中使用基本的 IStringLocalizer.

With this solution you are able to use the basic IStringLocalizer in your Views and Controllers.

当然,如果你有一个很大的 json 文件,你可以使用 IMemoryCacheIDistributedMemoryCache 来提高性能.

Of course if you have a big json file, you can use IMemoryCache or IDistributedMemoryCache to improve performance.

在应用程序启动中添加此行以使用您自己的实现:

In the application Startup add this lines to use your own implementation :

services.AddSingleton<IStringLocalizerFactory, JsonStringLocalizerFactory>();
services.AddSingleton<IStringLocalizer, JsonStringLocalizer>();
services.AddLocalization(options => options.ResourcesPath = "Resources");

之后,您可以根据自己的全球化偏好进行配置.

After that you can configure as you want your globalization preferences.

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

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