public static string ExportJson(string project, string module, List <string> languages, string exportPath) { using (var fastZip = new ZipFile()) { var filter = new ResCurrent { Project = new ResProject { Name = project }, Module = new ResModule { Name = module } }; var zipDirectory = Directory.CreateDirectory(exportPath + module); foreach (var language in languages) { filter.Language = new ResCulture { Title = language }; var words = ResourceData.GetListResWords(filter, string.Empty).GroupBy(x => x.ResFile.FileID).ToList(); if (!words.Any()) { Console.WriteLine("Error!!! Can't find appropriate project and module. Possibly wrong names!"); return(null); } foreach (var fileWords in words) { var wordsDictionary = new Dictionary <string, string>(); foreach (var word in fileWords.OrderBy(x => x.Title).Where(word => !wordsDictionary.ContainsKey(word.Title))) { wordsDictionary[word.Title] = word.ValueTo ?? word.ValueFrom; } var firstWord = fileWords.FirstOrDefault(); var fileName = firstWord == null ? module : Path.GetFileNameWithoutExtension(firstWord.ResFile.FileName); var zipFileName = zipDirectory.FullName + "\\" + fileName + (language == "Neutral" ? string.Empty : "." + language) + ".json"; using (TextWriter writer = new StreamWriter(zipFileName)) { var obj = JsonConvert.SerializeObject(wordsDictionary, Formatting.Indented); writer.Write(obj); } } } var zipPath = exportPath + "\\" + module + ".zip"; fastZip.AddDirectory(zipDirectory.FullName); fastZip.Save(zipPath); zipDirectory.Delete(true); return(zipPath); } }
public static string ExportJson(string project, string module, List <string> languages, string exportPath, bool withDefaultValue = true, bool withStructurJson = true) { var filter = new ResCurrent { Project = new ResProject { Name = project }, Module = new ResModule { Name = module } }; var zipDirectory = Directory.CreateDirectory(exportPath + module); foreach (var language in languages) { filter.Language = new ResCulture { Title = language }; var words = ResourceData.GetListResWords(filter, string.Empty).GroupBy(x => x.ResFile.FileID).ToList(); if (!words.Any()) { Console.WriteLine("Error!!! Can't find appropriate project and module. Possibly wrong names!"); return(null); } foreach (var fileWords in words) { var wordsDictionary = new Dictionary <string, string>(); foreach ( var word in fileWords.OrderBy(x => x.Title).Where(word => !wordsDictionary.ContainsKey(word.Title))) { if (string.IsNullOrEmpty(word.ValueTo) && !withDefaultValue) { continue; } wordsDictionary[word.Title] = word.ValueTo ?? word.ValueFrom; if (!string.IsNullOrEmpty(wordsDictionary[word.Title])) { wordsDictionary[word.Title] = wordsDictionary[word.Title].TrimEnd('\n').TrimEnd('\r'); } } if (!wordsDictionary.Any()) { continue; } var firstWord = fileWords.FirstOrDefault(); var fileName = firstWord == null ? module : Path.GetFileNameWithoutExtension(firstWord.ResFile.FileName); var ext = Path.GetExtension(firstWord.ResFile.FileName); var zipFileName = zipDirectory.FullName + "\\" + fileName + (language == "Neutral" ? string.Empty : "." + language) + ext; using (TextWriter writer = new StreamWriter(zipFileName)) { if (ext == ".json") { if (withStructurJson) { var collectionNames = new List <string>(); var wrOrder = 0; JObject jObject = null; var writeJtoken = new JTokenWriter(); writeJtoken.WriteStartObject(); foreach (var vordsKV in wordsDictionary) { var strNameSplit = vordsKV.Key.Split('.'); for (var a = 0; a < strNameSplit.Length; a++) { while (collectionNames.Count < a + 1) { collectionNames.Add(""); } if (collectionNames[a] != null && collectionNames[a] == strNameSplit[a]) { continue; } if (wrOrder > a) { for (var b = a; b < collectionNames.Count; b++) { collectionNames[b] = null; } while (wrOrder > a) { writeJtoken.WriteEndObject(); wrOrder--; } } writeJtoken.WritePropertyName(strNameSplit[a]); if (a < strNameSplit.Length - 1) { writeJtoken.WriteStartObject(); wrOrder++; collectionNames[a] = strNameSplit[a]; } else { writeJtoken.WriteValue(vordsKV.Value); } } } jObject = (JObject)writeJtoken.Token; writer.Write(jObject); } else { var obj = JsonConvert.SerializeObject(wordsDictionary, Formatting.Indented); writer.Write(obj); } } else { var data = new XmlDocument(); var resources = data.CreateElement("resources"); foreach (var ind in wordsDictionary) { var stringAttr = data.CreateAttribute("name"); stringAttr.Value = ind.Key; var child = data.CreateElement("string"); child.Attributes.Append(stringAttr); child.InnerText = ind.Value; resources.AppendChild(child); } data.AppendChild(resources); var settings = new XmlWriterSettings { Indent = true, IndentChars = " ", NewLineChars = Environment.NewLine, NewLineHandling = NewLineHandling.Replace, OmitXmlDeclaration = false, ConformanceLevel = ConformanceLevel.Fragment }; using (var xmlTextWriter = XmlWriter.Create(writer, settings)) { data.WriteTo(xmlTextWriter); xmlTextWriter.Flush(); } } } } } var zipPath = zipDirectory.FullName + ".zip"; var fastZip = new FastZip(); fastZip.CreateEmptyDirectories = true; fastZip.CreateZip(zipPath, zipDirectory.FullName, true, null); zipDirectory.Delete(true); return(zipPath); }
public static string ExportJson(string project, string module, List <string> languages, string exportPath, bool withDefaultValue = true) { using (var fastZip = new ZipFile()) { var filter = new ResCurrent { Project = new ResProject { Name = project }, Module = new ResModule { Name = module } }; var zipDirectory = Directory.CreateDirectory(exportPath + module); foreach (var language in languages) { filter.Language = new ResCulture { Title = language }; var words = ResourceData.GetListResWords(filter, string.Empty).GroupBy(x => x.ResFile.FileID).ToList(); if (!words.Any()) { Console.WriteLine("Error!!! Can't find appropriate project and module. Possibly wrong names!"); return(null); } foreach (var fileWords in words) { var wordsDictionary = new Dictionary <string, string>(); foreach ( var word in fileWords.OrderBy(x => x.Title).Where(word => !wordsDictionary.ContainsKey(word.Title))) { if (string.IsNullOrEmpty(word.ValueTo) && !withDefaultValue) { continue; } wordsDictionary[word.Title] = word.ValueTo ?? word.ValueFrom; if (!string.IsNullOrEmpty(wordsDictionary[word.Title])) { wordsDictionary[word.Title] = wordsDictionary[word.Title].TrimEnd('\n').TrimEnd('\r'); } } var firstWord = fileWords.FirstOrDefault(); var fileName = firstWord == null ? module : Path.GetFileNameWithoutExtension(firstWord.ResFile.FileName); var ext = Path.GetExtension(firstWord.ResFile.FileName); var zipFileName = zipDirectory.FullName + "\\" + fileName + (language == "Neutral" ? string.Empty : "." + language) + ext; using (TextWriter writer = new StreamWriter(zipFileName)) { if (ext == ".json") { var obj = JsonConvert.SerializeObject(wordsDictionary, Formatting.Indented); writer.Write(obj); } else { var data = new XmlDocument(); var resources = data.CreateElement("resources"); foreach (var ind in wordsDictionary) { var stringAttr = data.CreateAttribute("name"); stringAttr.Value = ind.Key; var child = data.CreateElement("string"); child.Attributes.Append(stringAttr); child.InnerText = ind.Value; resources.AppendChild(child); } data.AppendChild(resources); var settings = new XmlWriterSettings { Indent = true, IndentChars = " ", NewLineChars = Environment.NewLine, NewLineHandling = NewLineHandling.Replace, OmitXmlDeclaration = false, ConformanceLevel = ConformanceLevel.Fragment }; using (var xmlTextWriter = XmlWriter.Create(writer, settings)) { data.WriteTo(xmlTextWriter); xmlTextWriter.Flush(); } } } } } var zipPath = exportPath + "\\" + module + ".zip"; fastZip.AddDirectory(zipDirectory.FullName); fastZip.Save(zipPath); zipDirectory.Delete(true); return(zipPath); } }