JsonLocalizationDictionary.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using Abp.Collections.Extensions;
  6. using Abp.Extensions;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Serialization;
  9. namespace Abp.Localization.Dictionaries.Json
  10. {
  11. /// <summary>
  12. /// This class is used to build a localization dictionary from json.
  13. /// </summary>
  14. /// <remarks>
  15. /// Use static Build methods to create instance of this class.
  16. /// </remarks>
  17. public class JsonLocalizationDictionary : LocalizationDictionary
  18. {
  19. /// <summary>
  20. /// Private constructor.
  21. /// </summary>
  22. /// <param name="cultureInfo">Culture of the dictionary</param>
  23. private JsonLocalizationDictionary(CultureInfo cultureInfo)
  24. : base(cultureInfo)
  25. {
  26. }
  27. /// <summary>
  28. /// Builds an <see cref="JsonLocalizationDictionary" /> from given file.
  29. /// </summary>
  30. /// <param name="filePath">Path of the file</param>
  31. public static JsonLocalizationDictionary BuildFromFile(string filePath)
  32. {
  33. try
  34. {
  35. return BuildFromJsonString(File.ReadAllText(filePath));
  36. }
  37. catch (Exception ex)
  38. {
  39. throw new AbpException("Invalid localization file format! " + filePath, ex);
  40. }
  41. }
  42. /// <summary>
  43. /// Builds an <see cref="JsonLocalizationDictionary" /> from given json string.
  44. /// </summary>
  45. /// <param name="jsonString">Json string</param>
  46. public static JsonLocalizationDictionary BuildFromJsonString(string jsonString)
  47. {
  48. JsonLocalizationFile jsonFile;
  49. try
  50. {
  51. jsonFile = JsonConvert.DeserializeObject<JsonLocalizationFile>(
  52. jsonString,
  53. new JsonSerializerSettings
  54. {
  55. ContractResolver = new CamelCasePropertyNamesContractResolver()
  56. });
  57. }
  58. catch (JsonException ex)
  59. {
  60. throw new AbpException("Can not parse json string. " + ex.Message);
  61. }
  62. var cultureCode = jsonFile.Culture;
  63. if (string.IsNullOrEmpty(cultureCode))
  64. {
  65. throw new AbpException("Culture is empty in language json file.");
  66. }
  67. var dictionary = new JsonLocalizationDictionary(CultureInfo.GetCultureInfo(cultureCode));
  68. var dublicateNames = new List<string>();
  69. foreach (var item in jsonFile.Texts)
  70. {
  71. if (string.IsNullOrEmpty(item.Key))
  72. {
  73. throw new AbpException("The key is empty in given json string.");
  74. }
  75. if (dictionary.Contains(item.Key))
  76. {
  77. dublicateNames.Add(item.Key);
  78. }
  79. dictionary[item.Key] = item.Value.NormalizeLineEndings();
  80. }
  81. if (dublicateNames.Count > 0)
  82. {
  83. throw new AbpException(
  84. "A dictionary can not contain same key twice. There are some duplicated names: " +
  85. dublicateNames.JoinAsString(", "));
  86. }
  87. return dictionary;
  88. }
  89. }
  90. }