久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <small id='kJZmX'></small><noframes id='kJZmX'>

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

    2. <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 class="qwawimqqmiuu" id='kJZmX'><tfoot id='kJZmX'></tfoot><dl id='kJZmX'><fieldset id='kJZmX'></fieldset></dl></div>

        <legend id='kJZmX'><style id='kJZmX'><dir id='kJZmX'><q id='kJZmX'></q></dir></style></legend>

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

        Asp.net Core localization with json files(帶有 json 文件的 Asp.net Core 本地化)

            • <bdo id='TRFSe'></bdo><ul id='TRFSe'></ul>
              <tfoot id='TRFSe'></tfoot>
                <tbody id='TRFSe'></tbody>
            • <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 class="qwawimqqmiuu" 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 本地化的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在努力尋找一個關于 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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                  XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                  Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標簽的 XML)
                  Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                  delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                  Parse malformed XML(解析格式錯誤的 XML)

                      <tbody id='oMPxV'></tbody>

                    <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 class="qwawimqqmiuu" id='oMPxV'><tfoot id='oMPxV'></tfoot><dl id='oMPxV'><fieldset id='oMPxV'></fieldset></dl></div>

                      • <bdo id='oMPxV'></bdo><ul id='oMPxV'></ul>
                        • <legend id='oMPxV'><style id='oMPxV'><dir id='oMPxV'><q id='oMPxV'></q></dir></style></legend>
                          1. 主站蜘蛛池模板: 男人天堂国产 | 亚洲欧美日韩精品久久亚洲区 | 欧美在线精品一区 | 波多野结衣先锋影音 | 国产高清视频在线观看播放 | 一区二区免费高清视频 | 中文字幕成人在线 | 欧美一级欧美三级在线观看 | 九九热免费在线观看 | 99热国产在线播放 | 久久免费国产 | 欧美黄a| 国产三级在线观看播放 | 日操操夜操操 | 国产一级片久久久 | 91精品国产综合久久久久久 | 亚洲一区二区三区乱码aⅴ 四虎在线视频 | 久久精品黄色 | 国产欧美一区二区精品久导航 | 欧美一区在线看 | 国产精品久久久久久久毛片 | 亚洲一区毛片 | 欧美一二三四成人免费视频 | 69性欧美高清影院 | 久久天天躁狠狠躁夜夜躁2014 | 91亚洲国产成人精品一区二三 | 一区二区三区视频在线 | 国产精品成人一区二区三区夜夜夜 | 中文字幕一区二区三区乱码在线 | 日本不卡视频 | 视频一区二区三区中文字幕 | 久久精品久久久 | 欧美激情视频一区二区三区免费 | 国产精品久久国产精品 | 天天操天天射天天 | 天天综合网天天综合 | 亚洲一区二区在线播放 | 久久久久综合 | 国产视频一区在线 | 日韩中文字幕视频 | 免费成人午夜 |