/// <summary> /// Removes the resource. /// </summary> /// <param name="element">The element.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public bool RemoveResource(Element element) { Locker.EnterWriteLock(); try { CreateDir(); string filePath = Path.Combine(_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(); } }
/// <summary> /// Gets the element. /// </summary> /// <param name="name">The name.</param> /// <param name="category">The category.</param> /// <param name="culture">The culture.</param> /// <returns>Element.</returns> public Element GetElement(string name, string category, string culture) { Locker.EnterReadLock(); try { var filePath = Path.Combine(_path, ResXResourceFileHelper.GetFileName(category, culture)); var 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(); } }
/// <summary> /// Adds the resource. /// </summary> /// <param name="element">The element.</param> public void AddResource(Element element) { Locker.EnterWriteLock(); try { CreateDir(); string filePath = Path.Combine(_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(); } }
/// <summary> /// Adds the category. /// </summary> /// <param name="category">The category.</param> /// <param name="culture">The culture.</param> public void AddCategory(string category, string culture) { var filePath = Path.Combine(_path, ResXResourceFileHelper.GetFileName(category, culture)); var document = GetResxDocument(filePath) ?? CreateResXDocument(); Locker.EnterWriteLock(); try { document.Save(filePath); } finally { Locker.ExitWriteLock(); } }
/// <summary> /// Gets the cultures. /// </summary> /// <returns>IEnumerable{CultureInfo}.</returns> public IEnumerable <CultureInfo> GetCultures() { if (Directory.Exists(_path)) { string[] files = Directory.GetFiles(_path); foreach (var file in files) { string culture; string category; ResXResourceFileHelper.Parse(file, out culture, out category); if (!string.IsNullOrEmpty(culture)) { yield return(new CultureInfo(culture)); } } } }
/// <summary> /// Gets the elements. /// </summary> /// <returns>IQueryable{Element}.</returns> public IQueryable <Element> GetElements() { Locker.EnterReadLock(); try { IEnumerable <Element> elements = Enumerable.Empty <Element>(); if (Directory.Exists(_path)) { string[] files = Directory.GetFiles(_path); foreach (var fileName in files) { if (!fileName.EndsWith(".resx")) { continue; } var doc = XDocument.Load(fileName); string culture; string category; ResXResourceFileHelper.Parse(fileName, out culture, out category); if (doc.Root == null) { continue; } var newElements = doc.Root.Elements("data").Select(x => new Element { Category = category, Culture = culture, Name = x.Attribute("name").Value, Value = x.Element("value") != null ? x.Element("value").Value : null }); elements = elements.Union(newElements); } } return(elements.AsQueryable()); } finally { Locker.ExitReadLock(); } }
/// <summary> /// Gets the categories. /// </summary> /// <returns>IEnumerable{ElementCategory}.</returns> public IEnumerable <ElementCategory> GetCategories() { if (Directory.Exists(_path)) { string[] files = Directory.GetFiles(_path); foreach (var file in files) { string culture; string category; ResXResourceFileHelper.Parse(file, out culture, out category); if (!string.IsNullOrEmpty(category)) { yield return(new ElementCategory { Category = category, Culture = culture }); } } } }
/// <summary> /// Removes the category. /// </summary> /// <param name="category">The category.</param> /// <param name="culture">The culture.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public bool RemoveCategory(string category, string culture) { Locker.EnterWriteLock(); try { string[] files = Directory.GetFiles(_path); foreach (var file in files) { string culture1; string category1; ResXResourceFileHelper.Parse(file, out culture1, out category1); if (category.EqualsOrNullEmpty(category1, StringComparison.CurrentCultureIgnoreCase) && culture1.EqualsOrNullEmpty(culture, StringComparison.CurrentCultureIgnoreCase)) { File.Delete(file); } } return(true); } finally { Locker.ExitWriteLock(); } }