public static ResourceArea Build(IEnumerable <XElement> data, string rootArea) { var root = new ResourceArea { Name = rootArea, Prefix = "" }; foreach (var element in data) { // Splits: ([resouce area]_)*[resouce name] var nameAttribute = element.Attribute("name").Value; var valueElement = element.Element("value").Value; var areaParts = nameAttribute.Split(new[] { "_" }, StringSplitOptions.RemoveEmptyEntries); if (areaParts.Length <= 1) { root.Values.Add(GetValue(nameAttribute, valueElement)); } else { var area = GetArea(root, areaParts.Take(areaParts.Length - 1)); var value = GetValue(areaParts.Skip(areaParts.Length - 1).First(), valueElement); area.Values.Add(value); } } SortArea(root); return(root); }
private static ResourceArea GetArea(ResourceArea area, IEnumerable <string> areaPath) { var currentArea = area; foreach (var areaName in areaPath) { var existing = currentArea.NestedAreas.FirstOrDefault(a => a.Name == areaName); if (existing == null) { if (currentArea.Values.Any(v => v.Name == areaName)) { throw new ArgumentException(string.Format( "Area name '{0}' is already in use as a value name under area '{1}'.", areaName, currentArea.Name)); } existing = new ResourceArea { Name = areaName, Prefix = currentArea.Prefix + areaName + "_" }; currentArea.NestedAreas.Add(existing); } currentArea = existing; } return(currentArea); }
private static void SortArea (ResourceArea area) { area.Values.Sort ((left, right) => left.Name.CompareTo (right.Name)); foreach (var nested in area.NestedAreas) { SortArea (nested); } }
private static void SortArea(ResourceArea area) { area.Values.Sort((left, right) => left.Name.CompareTo(right.Name)); foreach (var nested in area.NestedAreas) { SortArea(nested); } }
private static ResourceArea GetArea (ResourceArea area, IEnumerable<string> areaPath) { var currentArea = area; foreach (var areaName in areaPath) { var existing = currentArea.NestedAreas.FirstOrDefault (a => a.Name == areaName); if (existing == null) { if (currentArea.Values.Any (v => v.Name == areaName)) throw new ArgumentException (string.Format ( "Area name '{0}' is already in use as a value name under area '{1}'.", areaName, currentArea.Name)); existing = new ResourceArea { Name = areaName, Prefix = currentArea.Prefix + areaName + "_" }; currentArea.NestedAreas.Add (existing); } currentArea = existing; } return currentArea; }
public static ResourceArea Build (IEnumerable<XElement> data, string rootArea) { var root = new ResourceArea { Name = rootArea, Prefix = "" }; foreach (var element in data) { // Splits: ([resouce area]_)*[resouce name] var nameAttribute = element.Attribute ("name").Value; var valueElement = element.Element ("value").Value; var areaParts = nameAttribute.Split (new[] { "_" }, StringSplitOptions.RemoveEmptyEntries); if (areaParts.Length <= 1) { root.Values.Add (GetValue (nameAttribute, valueElement)); } else { var area = GetArea (root, areaParts.Take (areaParts.Length - 1)); var value = GetValue (areaParts.Skip (areaParts.Length - 1).First (), valueElement); area.Values.Add (value); } } SortArea (root); return root; }
public static IGenerator Create (string language, string targetNamespace, string resourcesTypeName, string targetClassName, bool makePublic, ResourceArea rootArea) { var session = new Dictionary<string, object> { { "ResourcesTypeName", resourcesTypeName }, { "TargetNamespace", targetNamespace }, { "TargetClassName", targetClassName }, { "MakePublic", makePublic }, { "RootArea", rootArea } }; if ("C#".Equals (language, StringComparison.OrdinalIgnoreCase)) { var generator = new CsTypedResx { Session = session }; generator.Initialize(); return generator; } throw new NotSupportedException(string.Format("Language {0} is not supported yet.", language)); }
private void RenderArea(ResourceArea area, string visibility) { PushIndent("\t"); #line default #line hidden #line 48 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("\r\n/// <summary>\r\n/// Provides access to string resources.\r\n/// </summary>\r\n[globa" + "l::System.CodeDom.Compiler.GeneratedCodeAttribute(\"TypedResx\", \"1.0.0.0\")]\r\n[glo" + "bal::System.Diagnostics.DebuggerNonUserCodeAttribute()]\r\n"); #line default #line hidden #line 55 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(visibility)); #line default #line hidden #line 55 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(" static partial class "); #line default #line hidden #line 55 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(area.Name)); #line default #line hidden #line 55 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("\r\n{"); #line default #line hidden #line 56 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" foreach (var value in area.Values) { if (!value.HasFormat) { #line default #line hidden #line 61 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("\r\n\t/// <summary>\r\n\t/// Looks up a localized string similar to: \r\n"); #line default #line hidden #line 65 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" RenderMessageHint(value.Value); #line default #line hidden #line 65 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("\t/// </summary>\r\n\tpublic static string "); #line default #line hidden #line 67 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(value.Name)); #line default #line hidden #line 67 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(" { get { return "); #line default #line hidden #line 67 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ResourcesTypeName)); #line default #line hidden #line 67 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("."); #line default #line hidden #line 67 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(area.Prefix)); #line default #line hidden #line 67 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(value.Name)); #line default #line hidden #line 67 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("; } }\r\n"); #line default #line hidden #line 68 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" } else if (value.IsIndexed) { #line default #line hidden #line 72 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("\r\n\t/// <summary>\r\n\t/// Looks up a localized string similar to: \r\n"); #line default #line hidden #line 76 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" RenderMessageHint(value.Value); #line default #line hidden #line 76 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("\t/// </summary>\r\n\tpublic static string "); #line default #line hidden #line 78 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(value.Name)); #line default #line hidden #line 78 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("("); #line default #line hidden #line 78 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(string.Join(", ", value.Format.Select((s, i) => "object arg" + i)))); #line default #line hidden #line 78 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(")\r\n\t{\r\n var args = new [] { "); #line default #line hidden #line 80 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(string.Join(", ", value.Format.Select((s, i) => "arg" + i)))); #line default #line hidden #line 80 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(" };\r\n return string.Format(System.Globalization.CultureInfo.CurrentCulture" + ", "); #line default #line hidden #line 81 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ResourcesTypeName)); #line default #line hidden #line 81 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("."); #line default #line hidden #line 81 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(area.Prefix)); #line default #line hidden #line 81 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(value.Name)); #line default #line hidden #line 81 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(", args);\r\n\t}\r\n"); #line default #line hidden #line 83 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" } else { #line default #line hidden #line 87 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("\r\n\t/// <summary>\r\n\t/// Looks up a localized string similar to: \r\n"); #line default #line hidden #line 91 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" RenderMessageHint(value.Value); #line default #line hidden #line 91 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("\t/// </summary>\r\n\tpublic static string "); #line default #line hidden #line 93 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(value.Name)); #line default #line hidden #line 93 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("("); #line default #line hidden #line 93 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(string.Join(", ", value.Format.Select(s => "object " + s)))); #line default #line hidden #line 93 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(")\r\n\t{\r\n\t\t// NOTE: if you use named format strings, you need to manually install t" + "he package netfx-System.StringFormatWith\r\n\t\treturn "); #line default #line hidden #line 96 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ResourcesTypeName)); #line default #line hidden #line 96 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("."); #line default #line hidden #line 96 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(area.Prefix)); #line default #line hidden #line 96 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(value.Name)); #line default #line hidden #line 96 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(".FormatWith(new \r\n\t\t{\r\n"); #line default #line hidden #line 98 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" for (int i = 0; i < value.Format.Count; i++) { #line default #line hidden #line 101 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("\t\t\t"); #line default #line hidden #line 102 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(value.Format[i])); #line default #line hidden #line 102 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(" = "); #line default #line hidden #line 102 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(this.ToStringHelper.ToStringWithCulture(value.Format[i])); #line default #line hidden #line 102 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write(",\r\n"); #line default #line hidden #line 103 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" } #line default #line hidden #line 105 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("\t\t});\r\n\t}\r\n"); #line default #line hidden #line 108 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" } } foreach (var nestedArea in area.NestedAreas) { RenderArea(nestedArea, "public"); } #line default #line hidden #line 116 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" this.Write("}\r\n"); #line default #line hidden #line 118 "C:\Code\Personal\netfx\Extensions\System\StringResources\Source\CsTypedResx.tt" PopIndent(); }
public static IGenerator Create(string language, string targetNamespace, string resourcesTypeName, string targetClassName, bool makePublic, ResourceArea rootArea) { var session = new Dictionary <string, object> { { "ResourcesTypeName", resourcesTypeName }, { "TargetNamespace", targetNamespace }, { "TargetClassName", targetClassName }, { "MakePublic", makePublic }, { "RootArea", rootArea } }; if ("C#".Equals(language, StringComparison.OrdinalIgnoreCase)) { var generator = new CsTypedResx { Session = session }; generator.Initialize(); return(generator); } throw new NotSupportedException(string.Format("Language {0} is not supported yet.", language)); }