コード例 #1
0
        public string this[string key]
        {
            get
            {
                try
                {
                    var temp = StringsTable[key];
                    if (temp.AliasedKey)
                    {
                        return(this[temp.CloneOf]);
                    }
                    if (temp.DeriveFromParent)
                    {
                        return(null);
                    }

                    return(temp.Value);
                }
                catch (KeyNotFoundException)
                {
                    throw new StringNotFoundException(key);
                }
            }
            set
            {
                if (StringsTable.ContainsKey(key))
                {
                    StringsTable[key] = new StringTranslation(key, value);
                }
                else
                {
                    StringsTable[key].Value = value;
                }
            }
        }
コード例 #2
0
        public string this[string key]
        {
            get
            {
                try
                {
                    var temp = StringsTable[key];
                    if(temp.AliasedKey)
                    {
                        return this[temp.CloneOf];
                    }
                    if(temp.DeriveFromParent)
                    {
                        return null;
                    }

                    return temp.Value;
                }
                catch(KeyNotFoundException)
                {
                    throw new StringNotFoundException(key);
                }
            }
            set
            {
                if (StringsTable.ContainsKey(key))
                {
                    StringsTable[key] = new StringTranslation(key, value);
                }
                else
                {
                    StringsTable[key].Value = value;
                }
            }
        }
コード例 #3
0
        public void Load(string xmlPath)
        {
            Key = Path.GetFileNameWithoutExtension(xmlPath);
            string localeKey = Path.GetFileName(Path.GetDirectoryName(xmlPath));

            var xmlDocument = new XmlDocument();

            xmlDocument.Load(xmlPath);

            var node = xmlDocument.SelectSingleNode(@"/localization/strings");

            if (node == null)
            {
                throw new IncompleteLocaleException("The required locale element 'strings' was not found.");
            }

            var strings = xmlDocument.SelectNodes(@"/localization/strings/string");

            if (strings != null)
            {
                foreach (XmlNode text in strings)
                {
                    if (text.Attributes == null)
                    {
                        throw new MalformedStringException("Invalid translation string found. Attribute 'key' is required.");
                    }

                    var temp = text.Attributes["key"];

                    if (temp == null || string.IsNullOrEmpty(temp.InnerText))
                    {
                        throw new MalformedStringException("Invalid translation string found. Attribute 'key' is required.");
                    }

                    var key = temp.InnerText;

                    var value  = text.Attributes["value"] != null ? text.Attributes["value"].InnerText : null;
                    var clone  = text.Attributes["clone"] != null ? text.Attributes["clone"].InnerText : null;
                    var derive = text.Attributes["derive"] != null && text.Attributes["derive"].InnerText == @"true";

                    uint version        = 0;
                    var  versionElement = text.Attributes["version"];
                    if (versionElement != null)
                    {
                        version = uint.Parse(versionElement.InnerText);
                    }

                    if (_strings.ContainsKey(key))
                    {
                        throw new DuplicateKeyException(string.Format("Key {0} in {1}\\{2} was defined more than once.", key, localeKey, Key));
                    }

                    var newString = new StringTranslation(key);

                    if (value != null)
                    {
                        newString.Value = value;
                    }
                    else if (!string.IsNullOrEmpty(clone))
                    {
                        newString.CloneOf = clone;
                    }
                    else if (derive)
                    {
                        newString.DeriveFromParent = true;
                    }
                    else
                    {
                        //throw new MalformedStringException(string.Format("Invalid translation string {0} found. One of attributes 'value', 'clone', or 'derive' is required.", key));
                        continue;
                    }

                    _strings[newString.Key] = newString;
                    newString.Version       = version;
                }
            }
        }
コード例 #4
0
        public void Load(string xmlPath)
        {
            Key = Path.GetFileNameWithoutExtension(xmlPath);
            string localeKey = Path.GetFileName(Path.GetDirectoryName(xmlPath));

            var xmlDocument = new XmlDocument();
            xmlDocument.Load(xmlPath);

            var node = xmlDocument.SelectSingleNode(@"/localization/strings");
            if (node == null)
                throw new IncompleteLocaleException("The required locale element 'strings' was not found.");

            var strings = xmlDocument.SelectNodes(@"/localization/strings/string");
            if (strings != null)
            {
                foreach (XmlNode text in strings)
                {
                    if (text.Attributes == null)
                        throw new MalformedStringException("Invalid translation string found. Attribute 'key' is required.");

                    var temp = text.Attributes["key"];

                    if (temp == null || string.IsNullOrEmpty(temp.InnerText))
                        throw new MalformedStringException("Invalid translation string found. Attribute 'key' is required.");

                    var key = temp.InnerText;

                    var value = text.Attributes["value"] != null ? text.Attributes["value"].InnerText : null;
                    var clone = text.Attributes["clone"] != null ? text.Attributes["clone"].InnerText : null;
                    var derive = text.Attributes["derive"] != null && text.Attributes["derive"].InnerText == @"true";

                    uint version = 0;
                    var versionElement = text.Attributes["version"];
                    if (versionElement != null)
                    {
                        version = uint.Parse(versionElement.InnerText);
                    }

                    if (_strings.ContainsKey(key))
                        throw new DuplicateKeyException(string.Format("Key {0} in {1}\\{2} was defined more than once.", key, localeKey, Key));

                    var newString = new StringTranslation(key);

                    if(value != null)
                    {
                        newString.Value = value;
                    }
                    else if(!string.IsNullOrEmpty(clone))
                    {
                        newString.CloneOf = clone;
                    }
                    else if(derive)
                    {
                        newString.DeriveFromParent = true;
                    }
                    else
                    {
                        //throw new MalformedStringException(string.Format("Invalid translation string {0} found. One of attributes 'value', 'clone', or 'derive' is required.", key));
                        continue;
                    }

                    _strings[newString.Key] = newString;
                    newString.Version = version;
                }
            }
        }