예제 #1
0
        /// <summary>
        /// Removes the resource.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public bool RemoveResource(Element element)
        {
            locker.EnterWriteLock();
            try
            {
                CreateDir();
                string filePath = Path.Combine(this.path, ResXResourceFileHelper.GetFileName(element.Category, element.Culture));

                XDocument document = GetResxDocument(filePath);

                if (document == null)
                {
                    return(false);
                }

                var newElement = document.Root.Elements("data")
                                 .FirstOrDefault(d => d.Attribute("name").Value.EqualsOrNullEmpty(element.Name, StringComparison.OrdinalIgnoreCase));

                if (newElement == null)
                {
                    return(false);
                }

                newElement.Remove();
                document.Save(filePath);

                return(true);
            }
            finally
            {
                locker.ExitWriteLock();
            }
        }
예제 #2
0
        /// <summary>
        /// Gets the element.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="category">The category.</param>
        /// <param name="culture">The culture.</param>
        /// <returns></returns>
        public Element GetElement(string name, string category, string culture)
        {
            locker.EnterReadLock();
            try
            {
                string filePath = Path.Combine(this.path, ResXResourceFileHelper.GetFileName(category, culture));

                XDocument document = GetResxDocument(filePath);

                if (document == null)
                {
                    return(null);
                }

                return(document.Root.Elements("data")
                       .Where(it => it.Attribute("name").Value.EqualsOrNullEmpty(name, StringComparison.OrdinalIgnoreCase))
                       .Select(it => new Element()
                {
                    Name = it.Attribute("name").Value,
                    Value = it.Element("value").Value,
                    Category = category,
                    Culture = culture
                }).FirstOrDefault());
            }
            finally
            {
                locker.ExitReadLock();
            }
        }
예제 #3
0
        /// <summary>
        /// Adds the resource.
        /// </summary>
        /// <param name="element">The element.</param>
        public void AddResource(Element element)
        {
            locker.EnterWriteLock();
            try
            {
                CreateDir();
                string    filePath = Path.Combine(this.path, ResXResourceFileHelper.GetFileName(element.Category, element.Culture));
                XDocument document = GetResxDocument(filePath);

                if (document == null)
                {
                    document = CreateResXDocument();
                }
                var exists = document.Root.Elements("data")
                             .FirstOrDefault(d => d.Attribute("name").Value == element.Name);
                if (exists == null)
                {
                    document.Root.Add(
                        new XElement("data",
                                     new XAttribute("name", element.Name),
                                     new XAttribute(XNamespace.Xml + "space", "preserve"),
                                     new XElement("value", element.Value)));
                    document.Save(filePath);
                }
            }
            finally
            {
                locker.ExitWriteLock();
            }
        }
예제 #4
0
        /// <summary>
        /// Adds the category.
        /// </summary>
        /// <param name="category">The category.</param>
        /// <param name="culture">The culture.</param>
        public void AddCategory(string category, string culture)
        {
            string filePath = Path.Combine(this.path, ResXResourceFileHelper.GetFileName(category, culture));

            XDocument document = GetResxDocument(filePath);

            if (document == null)
            {
                document = CreateResXDocument();
            }
            locker.EnterWriteLock();
            try
            {
                document.Save(filePath);
            }
            finally
            {
                locker.ExitWriteLock();
            }
        }