Ejemplo n.º 1
0
        /// <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);
        }