/// <summary> /// Loads dictionaries from the specified file. /// </summary> /// <remarks> /// If several dictionaries have the same key, they are merged. /// If several phrases within a dictionary have the same key, the last value is taken. /// </remarks> private static bool LoadDictionaries(string fileName, out string errMsg) { if (File.Exists(fileName)) { try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fileName); foreach (XmlElement dictElem in xmlDoc.DocumentElement.SelectNodes("Dictionary")) { string dictKey = dictElem.GetAttribute("key"); if (!Dictionaries.TryGetValue(dictKey, out LocaleDict dict)) { dict = new LocaleDict(dictKey); Dictionaries.Add(dictKey, dict); } foreach (XmlElement phraseElem in dictElem.SelectNodes("Phrase")) { string phraseKey = phraseElem.GetAttribute("key"); dict.Phrases[phraseKey] = phraseElem.InnerText; } } errMsg = ""; return(true); } catch (Exception ex) { errMsg = string.Format(IsRussian ? "Ошибка при загрузке словарей из файла {0}: {1}" : "Error loading dictionaries from file {0}: {1}", fileName, ex.Message); return(false); } } else { errMsg = string.Format(IsRussian ? "Не найден файл словарей: {0}" : "Dictionary file not found: {0}", fileName); return(false); } }
/// <summary> /// Gets a dictionary that contains phrases accessed by control names. /// </summary> public static Dictionary <string, ControlPhrases> GetControlDict(LocaleDict dict) { Dictionary <string, ControlPhrases> controlDict = new Dictionary <string, ControlPhrases>(); foreach (var pair in dict.Phrases) { string phraseKey = pair.Key; string phraseVal = pair.Value; int dotIdx = phraseKey.IndexOf('.'); if (dotIdx < 0) { // if there is no dot in the key, set the text property if (controlDict.ContainsKey(phraseKey)) { controlDict[phraseKey].Text = phraseVal; } else { controlDict[phraseKey] = new ControlPhrases { Text = phraseVal } }; } else if (0 < dotIdx && dotIdx < phraseKey.Length - 1) { // the left part of the key contains a control name, and the right part is a property name string controlName = phraseKey.Substring(0, dotIdx); string propName = phraseKey.Substring(dotIdx + 1); bool propAssigned = true; if (!controlDict.TryGetValue(controlName, out ControlPhrases controlPhrases)) { controlPhrases = new ControlPhrases(); } if (propName == "Text") { controlPhrases.Text = phraseVal; } else if (propName == "ToolTip") { controlPhrases.ToolTip = phraseVal; } else if (propName.StartsWith("Items[")) { int pos = propName.IndexOf(']'); if (pos >= 0 && int.TryParse(propName.Substring(6, pos - 6), out int ind)) { controlPhrases.SetItem(ind, phraseVal); } } else if (propName != "") { controlPhrases.SetPhrase(propName, phraseVal); } else { propAssigned = false; } if (propAssigned) { controlDict[controlName] = controlPhrases; } } } return(controlDict); }