protected void Op1_Click(object sender, EventArgs e) { List<string> files = new List<string>(); foreach (LocPageGroup grp in groups().Where(g => g != LocPageGroup.newEA)) { GenResxContext ctx = new GenResxContext(grp); foreach (string s in TradosLib.GenResx(ctx)) files.Add(s); LocCfgPageGroupFilter group = LocCfg.Instance().findPageGroup(grp); //CSharp resources string globalRes = group.GlobalResourcePath; if (ctx.toTrans.Count > 0) { LowUtils.AdjustFileDir(globalRes); using (ResXResourceWriter wr = new ResXResourceWriter(globalRes)) foreach (TradosLib.resxNameValue nv in ctx.toTrans) wr.AddResource(nv.Name, nv.Value); files.Add(globalRes); } else if (File.Exists(globalRes)) File.Delete(globalRes); //JS resources string globalResJS = group.GlobalResourcePathJS; if (ctx.toTransJS.Count > 0) { LowUtils.AdjustFileDir(globalResJS); using (ResXResourceWriter wr = new ResXResourceWriter(globalResJS)) foreach (TradosLib.resxNameValue nv in ctx.toTransJS) wr.AddResource(nv.Name, nv.Value); files.Add(globalResJS); } else if (File.Exists(globalResJS)) File.Delete(globalResJS); } string[] filesArr = (from LocPageGroup grp in groups() from string s in TradosLib.oper1(grp) select s).ToArray(); CountLab.Text = filesArr.Length.ToString(); LogRep.DataSource = files; LogRep.DataBind(); }
/// <summary> /// Vygeneruje RESX z jednoho souboru /// </summary> public static XElement GenResx(string fn, GenResxContext ctx) { LocFileType fileType = getFileType(fn); if (fileType == LocFileType.aspx && (ctx.grp == LocPageGroup.rew_school || ctx.grp == LocPageGroup.rew_rewise)) fileType = LocFileType.js; XElement root = null; //Vytazeni konstant z CSharp kodu if (fileType == LocFileType.aspx || fileType == LocFileType.cs) { //if (fileType == LocFileType.cs && !File.Exists(fn)) //fn = fn.Replace(".cs",null); string content = File.ReadAllText(fn); bool modified; content = CSParser.Parse(content, ctx, fileType, out modified); if (modified) File.WriteAllText(fn, content); } //Vytazeni konstant z JS kodu if (fileType == LocFileType.js) { string content = File.ReadAllText(fn); bool modified; content = JSParser.Parse(content, ctx, out modified); if (modified) File.WriteAllText(fn, content); } StringBuilder sb = new StringBuilder(); //Pres XML if (fileType == LocFileType.aspx || fileType == LocFileType.lmap || fileType == LocFileType.lmdata || fileType == LocFileType.sitemap /*|| fileType == LocFileType.appdata*/ || fileType == LocFileType.downloadXml) { root = /*fileType == LocFileType.appdata ? null : */fileToXml(fn); /*if (ctx.grp == LocPageGroup.CPV) { //Pro CPV se jmeno prvku k prekladu sklada z parent chainu XElement body = root.Element(html + "body"); foreach (XElement el in body.Descendants().Where(e => e.Attribute("force_trans") != null || (!e.HasElements && !e.Parents(false).Any(p => p.Attribute("force_trans") != null)))) { if (el.Value.StartsWith("@")) continue; sb.Length = 0; XElement e = el; while (e != body) { sb.Insert(0, "." + e.Name.LocalName); e = e.Parent; } sb.Remove(0, 1); Anot.SetAnotName(el, sb.ToString()); } }*/ //generace RESX string outFn = resxFileName(fn); resxNameValue[] items = resxItems(root, fileType).ToArray(); if (items.Length == 0) { //vymazani RESX (i jazykovych) if (File.Exists(outFn)) File.Delete(outFn); foreach (Langs lng in Enum.GetValues(typeof(Langs))) { string transFn = resxFileName(outFn, lng); if (File.Exists(transFn)) File.Delete(transFn); } //return null; } if (items.Length > 0) { LowUtils.AdjustFileDir(outFn); using (ResXResourceWriter wr = new ResXResourceWriter(outFn)) foreach (resxNameValue nv in items) wr.AddResource(nv.Name, nv.Value.Replace("$nbsp;", " ")); } //Modifikace ASPX stranky if (fileType == LocFileType.aspx) { //New: title atribut v lmdata XML u LMDataControl kontrolky se lokalizuje pres CSLocalize foreach (XAttribute attr in root.Descendants().Where(el => el.Name.Namespace == lm).Select(el => el.Attribute("title")).Where(a => a != null && a.Parent.Attribute("id") != null)) ctx.toTrans.Add(new TradosLib.resxNameValue(LMDataControlResId(fn, attr), attr.Value, (XAttribute)attr)); foreach (resxNameValue nv in items) if (nv.Attr != null) AspxParser.modifyAspxPage(nv.Attr); else AspxParser.modifyAspxPage(nv.Nodes, nv.Name, nv.Value); //XCData dt = (XCData) root.DescendantNodes().Where(nd => nd.NodeType == XmlNodeType.CDATA && ((XCData)nd).Value.IndexOf("/*LMDataControl.Localize*/null") > 0).FirstOrDefault(); //if (dt!=null) root.Save(fn + ".xml", SaveOptions.DisableFormatting); string content = AspxParser.ToString(root); if (!File.Exists(fn + ".transsrc")) StringUtils.StringToFileUtf8Signature(content, fn + ".trans"); //File.WriteAllText(fn + ".trans", content); } } return root; }
/// <summary> /// Vygeneruje RESX z grupy souboru /// </summary> public static IEnumerable<string> GenResx(GenResxContext ctx) { //LocCfgPageGroupFilter group = LocCfg.Instance().findPageGroup(ctx.grp); //Stranky pro existujici RESX soubory foreach (string fn in getFiles(new LocPageGroup[] { ctx.grp })) if (GenResx(fn, ctx) != null) yield return fn; }
public static string Parse(string content, GenResxContext ctx, out bool modified) { modified = false; StringBuilder sb = new StringBuilder(); foreach (regExItem item in regExItem.Parse(content, rxJSLocalize)) { if (item.IsMatch) { string val = item.Value.Substring(11, item.Value.Length - 12); string[] parts = val.Split(new char[] { ',' }, 2); for (int i = 0; i < parts.Length; i++) parts[i] = parts[i].Trim(); //prvni parametr: GUID nebo null string id; if (parts[0] == "id") { //definice funkce CSLocalize sb.Append(item.Value); continue; } else if (parts[0] == "null") { id = Guid.NewGuid().ToString().Replace("-", null); parts[0] = id; ctx.ids.Add(id, true); modified = true; } else { id = parts[0].Substring(1, 32); if (ctx.ids.ContainsKey(id)) { id = Guid.NewGuid().ToString().Replace("-", null); parts[0] = id; modified = true; } ctx.ids.Add(id, true); } //Modifikovane volani procedury sb.Append(@"CSLocalize('"); sb.Append(id); sb.Append("', "); sb.Append(parts[1]); sb.Append(")"); //Vystup hodnoty do resource: val = parts[1].Substring(1, parts[1].Length - 2); ctx.toTransJS.Add(new TradosLib.resxNameValue(id, val, (XAttribute)null)); } else sb.Append(item.Value); } content = sb.ToString(); return content; }
public static string Parse(string content, GenResxContext ctx, LocFileType fileType, out bool modified) { modified = false; StringBuilder sb = new StringBuilder(); foreach (regExItem item in regExItem.Parse(content, rxCSLocalize)) { if (item.IsMatch) { string val = item.Value.Substring(20, item.Value.Length - 21); string[] parts = val.Split(new char[] { ',' }, 3); for (int i = 0; i < parts.Length; i++) parts[i] = parts[i].Trim(); //prvni parametr: GUID nebo null string id; if (parts[0] == "null") { id = Guid.NewGuid().ToString().Replace("-", null); parts[0] = id; ctx.ids.Add(id, true); modified = true; } else { id = parts[0].Substring(1, 32); if (ctx.ids.ContainsKey(id)) { id = Guid.NewGuid().ToString().Replace("-", null); modified = true; } ctx.ids.Add(id, true); } //Druhy parametr: grupa if (parts[1] != "LocPageGroup." + ctx.grp.ToString()) { parts[1] = "LocPageGroup." + ctx.grp.ToString(); modified = true; } //Modifikovane volani procedury sb.Append(@"CSLocalize."); //rozdeleno aby se nestalo predmetem lokalizace sb.Append(@"localize("""); sb.Append(id); sb.Append("\", "); sb.Append(parts[1]); sb.Append(", "); sb.Append(parts[2]); sb.Append(")"); //Vystup hodnoty do resource: val = parts[2]; if (val[0] == '@') { val = val.Substring(2, val.Length - 3); val = val.Replace("\"\"", "\""); } else { val = val.Substring(1, val.Length - 2); val = Regex.Unescape(val); } ctx.toTrans.Add(new TradosLib.resxNameValue(id, val, (XAttribute)null)); } else sb.Append(item.Value); } content = sb.ToString(); return content; }