public static AXmlResource[] ReadAllFiles(string aProjPath, string sourceLan, out AXmlResource sourceFile) { DirectoryInfo aProjDir = new DirectoryInfo(Path.Combine(aProjPath, AXmlHelper.AXML_PATH)); FileInfo[] aFiles = aProjDir.GetFiles("strings.xml", SearchOption.AllDirectories); List <AXmlResource> aResources = new List <AXmlResource>(); sourceFile = null; foreach (FileInfo aFile in aFiles) { AXmlResource resource = AXmlHelper.ReadAXml(aFile.FullName); if (resource != null) { Console.WriteLine($"{aFile.Directory.Name}/{aFile.Name} successfully read"); } else { Console.WriteLine($"Error reading file {aFile.Directory.Name}/{aFile.Name}"); } if (resource == null) { continue; } if (!string.IsNullOrEmpty(sourceLan)) { if (String.Compare(resource.Language, sourceLan, true) == 0) { sourceFile = resource; } } else { if (aFile.Directory.Name == "values") { sourceFile = resource; } } aResources.Add(resource); } return(aResources.ToArray()); }
public void Extract() { AXmlResource sourceRes; AXmlResource[] projResources = AXmlHelper.ReadAllFiles(this.Options.ProjectPath, this.Options.SourceLanguage, out sourceRes); if (sourceRes == null) { throw new Exception("Default resource not found"); } for (int i = 0; i < projResources.Length; i++) { AXmlResource projResource = projResources[i]; Console.WriteLine($"Processing {projResource.Language}"); var targetResource = new AXmlResource() { FolderParts = projResource.FolderParts }; foreach (var resString in sourceRes) { if (!projResource.Any(r => r.Name == resString.Name)) { targetResource.Add(resString.Clone()); } } if (targetResource.Count == 0) { continue; } Console.WriteLine($"Saving {projResource.Language}"); AXmlHelper.SaveAXml(targetResource, Path.Combine(this.Options.TargetPath, String.Join("-", targetResource.FolderParts), "strings.xml")); } Console.WriteLine("Saving initial file (source.xml)"); AXmlHelper.SaveAXml(sourceRes, Path.Combine(this.Options.TargetPath, "source.xml")); }
public static AXmlResource ReadAXml(string path) { AXmlResource res = null; DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(path)); string[] folderParts = dir.Name.Split('-'); using (XmlReader reader = XmlReader.Create(path, new XmlReaderSettings() { IgnoreComments = false })) { res = new AXmlResource(); AXmlPlural xmlPlural = null; bool resourceContent = false; List <string> comments = new List <string>(); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Comment) { comments.Add(reader.Value.Trim()); continue; } if (reader.IsStartElement()) { if (reader.Name == N_RESOURCE) { resourceContent = true; continue; } } if (!resourceContent) { continue; } if (reader.NodeType == XmlNodeType.EndElement) { if (reader.Name == N_PLURALS) { xmlPlural = null; continue; } else if (reader.Name == N_RESOURCE) { resourceContent = false; continue; } } if (reader.IsStartElement()) { if (reader.Name == N_STRING) { AXmlString xmlString = new AXmlString(); xmlString.Name = reader.GetAttribute(A_NAME); if (comments.Count > 0) { xmlString.Comments.AddRange(comments); comments.Clear(); } string trans = reader.GetAttribute(A_TRANSLATABLE); if (!string.IsNullOrEmpty(trans)) { xmlString.IsTranslatable = Convert.ToBoolean(trans); } xmlString.Value = reader.ReadElementContentAsString(); res.Add(xmlString); continue; } else if (reader.Name == N_PLURALS) { xmlPlural = new AXmlPlural(); xmlPlural.Name = reader.GetAttribute(A_NAME); if (comments.Count > 0) { xmlPlural.Comments.AddRange(comments); comments.Clear(); } res.Add(xmlPlural); continue; } else if (reader.Name == N_ITEM) { if (xmlPlural == null) { continue; } AXmlPluralItem item = new AXmlPluralItem(); item.Quantity = (QuantityType)Enum.Parse(typeof(QuantityType), reader.GetAttribute(A_QUANTITY)); item.Value = reader.ReadElementContentAsString(); xmlPlural.Add(item); } } } } if (res != null) { res.FolderParts = folderParts; } return(res); }
public static void SaveAXml(AXmlResource resource, string path) { string dir = Path.GetDirectoryName(path); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } using (XmlWriter writer = XmlWriter.Create(path, new XmlWriterSettings() { Indent = true })) { writer.WriteStartDocument(); writer.WriteStartElement(N_RESOURCE); foreach (AXmlResourceItem xmlItem in resource) { if (xmlItem.Comments != null && xmlItem.Comments.Count > 0) { foreach (string comment in xmlItem.Comments) { writer.WriteComment(comment); } } if (xmlItem is AXmlString) { AXmlString aString = (AXmlString)xmlItem; writer.WriteStartElement(N_STRING); writer.WriteAttributeString(A_NAME, aString.Name); if (!aString.IsTranslatable) { writer.WriteAttributeString(A_TRANSLATABLE, Convert.ToString(aString.IsTranslatable)); } writer.WriteValue(aString.Value); writer.WriteEndElement(); } else if (xmlItem is AXmlPlural) { writer.WriteStartElement(N_PLURALS); writer.WriteAttributeString(A_NAME, xmlItem.Name); foreach (AXmlPluralItem item in ((AXmlPlural)xmlItem).Items.Values) { if (string.IsNullOrEmpty(item.Value)) { continue; } writer.WriteStartElement(N_ITEM); writer.WriteAttributeString(A_QUANTITY, Enum.GetName(typeof(QuantityType), item.Quantity)); writer.WriteValue(item.Value); writer.WriteEndElement(); } writer.WriteEndElement(); } } writer.WriteEndElement(); writer.WriteEndDocument(); } }