/// <summary> /// Builds an <see cref="XmlLocalizationDictionary"/> from given xml string. /// </summary> /// <param name="xmlString">XML string</param> public static XmlLocalizationDictionary BuildFomXmlString(string xmlString) { var xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xmlString); XmlNodeList localizationDictionaryNode = xmlDocument.SelectNodes("/localizationDictionary"); if (localizationDictionaryNode == null || localizationDictionaryNode.Count <= 0) { throw new ArgumentException("A Localization Xml must include localizationDictionary as root node."); } var cultureName = localizationDictionaryNode[0].GetAttributeValueOrNull("culture"); if (string.IsNullOrEmpty(cultureName)) { throw new ArgumentException("culture is not defined in language XML file!"); } var dictionary = new XmlLocalizationDictionary(CultureInfo.GetCultureInfo(cultureName)); var duplicateNames = new List <string>(); XmlNodeList textNodes = xmlDocument.SelectNodes("/localizationDictionary/texts/text"); if (textNodes != null) { foreach (XmlNode node in textNodes) { var name = node.GetAttributeValueOrNull("name"); if (string.IsNullOrEmpty(name)) { throw new ArgumentException("name attribute of a text is empty in given xml string."); } if (dictionary.Contains(name)) { duplicateNames.Add(name); } dictionary[name] = (node.GetAttributeValueOrNull("value") ?? node.InnerText).NormalizeLineEndings(); } } if (duplicateNames.Count > 0) { throw new ArgumentException("A dictionary can not contain same key twice. There are some duplicated names: " + duplicateNames.JoinAsString(", ")); } return(dictionary); }
public override void Initialize(string sourceName, ILocalizationConfiguration localizationConfiguration) { CultureInfo[] allCultureInfos = CultureInfo.GetCultures(CultureTypes.AllCultures); List <string> resourceNames = _assembly.GetManifestResourceNames().Where(resourceName => allCultureInfos.Any(culture => resourceName.EndsWith($"{sourceName}.xml", true, null) || resourceName.EndsWith($"{sourceName}-{culture.Name}.xml", true, null))).ToList(); foreach (string resourceName in resourceNames) { if (!resourceName.StartsWith(_rootNamespace)) { continue; } using (Stream stream = _assembly.GetManifestResourceStream(resourceName)) { string xmlString = Utf8Helper.ReadStringFromStream(stream); XmlLocalizationDictionary dictionary = CreateXmlLocalizationDictionary(xmlString); string dicCultureInfoName = dictionary.CultureInfo.Name; if (Dictionaries.ContainsKey(dicCultureInfoName)) { throw new Exception(sourceName + " source contains more than one dictionary for the culture: " + dicCultureInfoName); } Dictionaries[dicCultureInfoName] = dictionary; LanguageIcon languageIcon = LocalizationConsts.LanguageIcons.Where(p => dicCultureInfoName.StartsWith(p.CountryName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); localizationConfiguration.Languages.Add( new LanguageInfo(dicCultureInfoName, dictionary.CultureInfo.NativeName, icon: languageIcon?.Icon, isDefault: CultureInfo.CurrentUICulture.Name.Equals(dicCultureInfoName))); if (!resourceName.EndsWith(sourceName + ".xml")) { continue; } if (DefaultDictionary != null) { throw new Exception("Only one default localization dictionary can be for source: " + sourceName); } DefaultDictionary = dictionary; } } }
protected virtual XmlLocalizationDictionary CreateXmlLocalizationDictionary(string xmlString) { return(XmlLocalizationDictionary.BuildFomXmlString(xmlString)); }
protected virtual XmlLocalizationDictionary CreateXmlLocalizationDictionary(string fileName) { return(XmlLocalizationDictionary.BuildFomFile(fileName)); }