| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using Abp.Collections.Extensions;
- using Abp.Extensions;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Serialization;
- namespace Abp.Localization.Dictionaries.Json
- {
- /// <summary>
- /// This class is used to build a localization dictionary from json.
- /// </summary>
- /// <remarks>
- /// Use static Build methods to create instance of this class.
- /// </remarks>
- public class JsonLocalizationDictionary : LocalizationDictionary
- {
- /// <summary>
- /// Private constructor.
- /// </summary>
- /// <param name="cultureInfo">Culture of the dictionary</param>
- private JsonLocalizationDictionary(CultureInfo cultureInfo)
- : base(cultureInfo)
- {
- }
- /// <summary>
- /// Builds an <see cref="JsonLocalizationDictionary" /> from given file.
- /// </summary>
- /// <param name="filePath">Path of the file</param>
- public static JsonLocalizationDictionary BuildFromFile(string filePath)
- {
- try
- {
- return BuildFromJsonString(File.ReadAllText(filePath));
- }
- catch (Exception ex)
- {
- throw new AbpException("Invalid localization file format! " + filePath, ex);
- }
- }
- /// <summary>
- /// Builds an <see cref="JsonLocalizationDictionary" /> from given json string.
- /// </summary>
- /// <param name="jsonString">Json string</param>
- public static JsonLocalizationDictionary BuildFromJsonString(string jsonString)
- {
- JsonLocalizationFile jsonFile;
- try
- {
- jsonFile = JsonConvert.DeserializeObject<JsonLocalizationFile>(
- jsonString,
- new JsonSerializerSettings
- {
- ContractResolver = new CamelCasePropertyNamesContractResolver()
- });
- }
- catch (JsonException ex)
- {
- throw new AbpException("Can not parse json string. " + ex.Message);
- }
- var cultureCode = jsonFile.Culture;
- if (string.IsNullOrEmpty(cultureCode))
- {
- throw new AbpException("Culture is empty in language json file.");
- }
- var dictionary = new JsonLocalizationDictionary(CultureInfo.GetCultureInfo(cultureCode));
- var dublicateNames = new List<string>();
- foreach (var item in jsonFile.Texts)
- {
- if (string.IsNullOrEmpty(item.Key))
- {
- throw new AbpException("The key is empty in given json string.");
- }
- if (dictionary.Contains(item.Key))
- {
- dublicateNames.Add(item.Key);
- }
- dictionary[item.Key] = item.Value.NormalizeLineEndings();
- }
- if (dublicateNames.Count > 0)
- {
- throw new AbpException(
- "A dictionary can not contain same key twice. There are some duplicated names: " +
- dublicateNames.JoinAsString(", "));
- }
- return dictionary;
- }
- }
- }
|