public object GetAllResourcesForResourceGrid(string resourceSet) { var items = Manager.GetAllResources(resourceSet: resourceSet); if (items == null) { throw new ApplicationException(Manager.ErrorMessage); } // reorder and reshape the data var itemList = items .OrderBy(it => it.ResourceId + "_" + it.LocaleId) .Select(it => new BasicResourceItem() { ResourceId = it.ResourceId, LocaleId = it.LocaleId, ResourceSet = it.ResourceSet, Value = it.Value as string }).ToList(); var totalLocales = itemList.GroupBy(it => it.LocaleId).Select(it => it.Key).ToList(); foreach (var item in itemList.GroupBy(it => it.ResourceId)) { string resid = item.Key; var resItems = itemList.Where(it => it.ResourceId == resid).ToList(); if (resItems.Count < totalLocales.Count) { foreach (string locale in totalLocales) { if (!resItems.Any(ri => ri.LocaleId == locale)) { itemList.Add(new BasicResourceItem { ResourceId = resid, LocaleId = locale, ResourceSet = resourceSet }); } } } } itemList = itemList.OrderBy(it => it.ResourceId + "_" + it.LocaleId).ToList(); var resultList = new List <object>(); foreach (var item in itemList.GroupBy(it => it.ResourceId)) { var resId = item.Key; var newItem = new { ResourceId = resId, Resources = itemList .Where(it => it.ResourceId == resId) .OrderBy(it => it.LocaleId) }; resultList.Add(newItem); } // final projection var result = new { ResourceSet = resourceSet, Locales = totalLocales, Resources = resultList }; return(result); }
/// <summary> /// Generates Resx Files for standard non-Web Resource files /// based on the BasePhysicalPath /// </summary> /// <returns></returns> public bool GenerateResXFiles() { DbResourceDataManager Data = new DbResourceDataManager(); // Retrieve all resources for a ResourceSet for all cultures // The data is ordered by ResourceSet, LocaleId and resource ID as each // ResourceSet or Locale changes a new file is written DataTable dtResources = Data.GetAllResources(); if (dtResources == null) { return(false); } string lastSet = ""; string lastLocale = "@!"; // Load the document schema XmlDocument doc = new XmlDocument(); doc.LoadXml(ResXDocumentTemplate); XmlWriter xWriter = null; var xmlSettings = new XmlWriterSettings(); //// Make sure we use fragment syntax so there's no validation //// otherwise loading the original string will fail xmlSettings.ConformanceLevel = ConformanceLevel.Document; xmlSettings.IndentChars = " "; xmlSettings.Indent = true; foreach (DataRow dr in dtResources.Rows) { // Read into vars for easier usage below string resourceId = dr["ResourceId"] as string; string value = dr["Value"] as string; string comment = dr["Comment"] as string; string type = dr["Type"] as string; string textFile = dr["TextFile"] as string; byte[] binFile = dr["BinFile"] as byte[]; string fileName = dr["FileName"] as string; string resourceSet = dr["ResourceSet"] as string; //ResourceSet = ResourceSet.ToLower(); string localeId = dr["LocaleId"] as string; localeId = localeId.ToLower(); // Create a new output file if the resource set or locale changes if (resourceSet != lastSet || localeId != lastLocale) { if (xWriter != null) { xWriter.WriteEndElement(); xWriter.Close(); } string localizedExtension = ".resx"; if (localeId != "") { localizedExtension = "." + localeId + ".resx"; } var fullFileName = this.FormatResourceSetPath(resourceSet) + localizedExtension; XmlTextWriter writer = new XmlTextWriter(fullFileName, Encoding.UTF8); writer.Indentation = 3; writer.IndentChar = ' '; writer.Formatting = Formatting.Indented; xWriter = writer as XmlWriter; xWriter.WriteStartElement("root"); // Write out the schema doc.DocumentElement.ChildNodes[0].WriteTo(xWriter); // Write out the leading resheader elements XmlNodeList Nodes = doc.DocumentElement.SelectNodes("resheader"); foreach (XmlNode Node in Nodes) { Node.WriteTo(xWriter); } lastSet = resourceSet; lastLocale = localeId; } if (type == "") // plain string value { //<data name="LinkButton1Resource1.Text" xml:space="preserve"> // <value>LinkButton</value> //</data> xWriter.WriteStartElement("data"); xWriter.WriteAttributeString("name", resourceId); xWriter.WriteAttributeString("xml", "space", null, "preserve"); xWriter.WriteElementString("value", value); if (!string.IsNullOrEmpty(comment)) { xWriter.WriteElementString("comment", comment); } xWriter.WriteEndElement(); // data } // File Resources get written to disk else if (type == "FileResource") { string ResourceFilePath = this.FormatResourceSetPath(resourceSet); string ResourcePath = new FileInfo(ResourceFilePath).DirectoryName; if (value.IndexOf("System.String") > -1) { string[] Tokens = value.Split(';'); Encoding Encode = Encoding.Default; try { if (Tokens.Length == 3) { Encode = Encoding.GetEncoding(Tokens[2]); } // Write out the file to disk File.WriteAllText(ResourcePath + "\\" + fileName, textFile, Encode); } catch { } } else { File.WriteAllBytes(ResourcePath + "\\" + fileName, binFile); } //<data name="Scratch" type="System.Resources.ResXFileRef, System.Windows.Forms"> // <value>Scratch.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value> //</data> xWriter.WriteStartElement("data"); xWriter.WriteAttributeString("name", resourceId); xWriter.WriteAttributeString("type", "System.Resources.ResXFileRef, System.Windows.Forms"); // values are already formatted in the database xWriter.WriteElementString("value", value); if (!string.IsNullOrEmpty(comment)) { xWriter.WriteElementString("comment", comment); } xWriter.WriteEndElement(); // data } } // foreach dr if (xWriter != null) { xWriter.WriteEndElement(); //xWriter.WriteRaw("\r\n</root>"); xWriter.Close(); } return(true); }