/// <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 Exception("Can not parse json string. " + ex.Message); } var cultureCode = jsonFile.Culture; if (string.IsNullOrEmpty(cultureCode)) { throw new Exception("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 Exception("The key is empty in given json string."); } if (dictionary.Contains(item.Key)) { dublicateNames.Add(item.Key); } dictionary[item.Key] = item.Value; } if (dublicateNames.Count > 0) { throw new Exception( "A dictionary can not contain same key twice. There are some duplicated names: " + string.Join(", ", dublicateNames)); } return(dictionary); }
/// <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(new CultureInfo(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; }
/// <summary> /// Builds an <see cref="JsonLocalizationDictionary" /> from given json string. /// </summary> /// <param name="jsonString">Json string</param> public static JsonLocalizationDictionary BuildFomJsonString(string jsonString) { LocalizedJson deserialized; try { deserialized = JsonConvert.DeserializeObject <LocalizedJson>(jsonString); } catch (JsonException ex) { throw new AbpException("Can not parse json string. " + ex.Message); } var cultureCode = deserialized.Culture; if (string.IsNullOrEmpty(cultureCode)) { throw new AbpException("Culture is empty in language json file."); } var dictionary = new JsonLocalizationDictionary(new CultureInfo(cultureCode)); var dublicateNames = new List <string>(); foreach (var item in deserialized.KeyValuePairs) { 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); }
/// <summary> /// Builds an <see cref="JsonLocalizationDictionary"/> from given json string. /// </summary> /// <param name="jsonString">Json string</param> public static JsonLocalizationDictionary BuildFomJsonString(string jsonString) { LocalizedJson deserialized; try { deserialized = JsonConvert.DeserializeObject<LocalizedJson>(jsonString); } catch (JsonException ex) { throw new AbpException("Can not parse json string. " + ex.Message); } var cultureCode = deserialized.Culture; if (string.IsNullOrEmpty(cultureCode)) { throw new AbpException("Culture is empty in language json file."); } var dictionary = new JsonLocalizationDictionary(new CultureInfo(cultureCode)); var dublicateNames = new List<string>(); foreach (var item in deserialized.KeyValuePairs) { 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; }