• <bdo id='TRFSe'></bdo><ul id='TRFSe'></ul>
    <tfoot id='TRFSe'></tfoot>
  1. <small id='TRFSe'></small><noframes id='TRFSe'>

    <i id='TRFSe'><tr id='TRFSe'><dt id='TRFSe'><q id='TRFSe'><span id='TRFSe'><b id='TRFSe'><form id='TRFSe'><ins id='TRFSe'></ins><ul id='TRFSe'></ul><sub id='TRFSe'></sub></form><legend id='TRFSe'></legend><bdo id='TRFSe'><pre id='TRFSe'><center id='TRFSe'></center></pre></bdo></b><th id='TRFSe'></th></span></q></dt></tr></i><div id='TRFSe'><tfoot id='TRFSe'></tfoot><dl id='TRFSe'><fieldset id='TRFSe'></fieldset></dl></div>
      <legend id='TRFSe'><style id='TRFSe'><dir id='TRFSe'><q id='TRFSe'></q></dir></style></legend>

      带有 json 文件的 Asp.net Core 本地化

      时间:2023-08-26

    1. <small id='kJZmX'></small><noframes id='kJZmX'>

      • <bdo id='kJZmX'></bdo><ul id='kJZmX'></ul>
          <tfoot id='kJZmX'></tfoot>

            • <i id='kJZmX'><tr id='kJZmX'><dt id='kJZmX'><q id='kJZmX'><span id='kJZmX'><b id='kJZmX'><form id='kJZmX'><ins id='kJZmX'></ins><ul id='kJZmX'></ul><sub id='kJZmX'></sub></form><legend id='kJZmX'></legend><bdo id='kJZmX'><pre id='kJZmX'><center id='kJZmX'></center></pre></bdo></b><th id='kJZmX'></th></span></q></dt></tr></i><div id='kJZmX'><tfoot id='kJZmX'></tfoot><dl id='kJZmX'><fieldset id='kJZmX'></fieldset></dl></div>

                  <tbody id='kJZmX'></tbody>

                <legend id='kJZmX'><style id='kJZmX'><dir id='kJZmX'><q id='kJZmX'></q></dir></style></legend>
                本文介绍了带有 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 本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:C# 类库本地化 下一篇:用于编辑 .resx 文件的 ASP.NET 组件

                相关文章

                最新文章

                  <tfoot id='oMPxV'></tfoot>

                  <small id='oMPxV'></small><noframes id='oMPxV'>

                  <i id='oMPxV'><tr id='oMPxV'><dt id='oMPxV'><q id='oMPxV'><span id='oMPxV'><b id='oMPxV'><form id='oMPxV'><ins id='oMPxV'></ins><ul id='oMPxV'></ul><sub id='oMPxV'></sub></form><legend id='oMPxV'></legend><bdo id='oMPxV'><pre id='oMPxV'><center id='oMPxV'></center></pre></bdo></b><th id='oMPxV'></th></span></q></dt></tr></i><div id='oMPxV'><tfoot id='oMPxV'></tfoot><dl id='oMPxV'><fieldset id='oMPxV'></fieldset></dl></div>

                  • <bdo id='oMPxV'></bdo><ul id='oMPxV'></ul>
                1. <legend id='oMPxV'><style id='oMPxV'><dir id='oMPxV'><q id='oMPxV'></q></dir></style></legend>