From 74b65e284b2efc2d9f4fbc93bc050f9b6a7803c5 Mon Sep 17 00:00:00 2001 From: Kenneth Skovhede Date: Tue, 27 Sep 2016 20:40:18 +0200 Subject: [PATCH] Various fixes for generating the localization files --- BuildTools/LocalizationTool2/Program.cs | 30 +++++++++++++++---------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/BuildTools/LocalizationTool2/Program.cs b/BuildTools/LocalizationTool2/Program.cs index c443dc242..ba911af69 100644 --- a/BuildTools/LocalizationTool2/Program.cs +++ b/BuildTools/LocalizationTool2/Program.cs @@ -16,6 +16,10 @@ namespace LocalizationTool2 sourcefolder = Path.GetFullPath(sourcefolder.Replace("~", Environment.GetEnvironmentVariable("HOME"))); + var targetfolder = sourcefolder; + if (args == null || args.Length > 1) + targetfolder = Path.GetFullPath(args[1].Replace("~", Environment.GetEnvironmentVariable("HOME"))); + if (!Directory.Exists(sourcefolder)) { @@ -30,7 +34,7 @@ namespace LocalizationTool2 var searchlist = new Dictionary(); searchlist.Add("html", new Regex("((\\{\\{)|(ng-bind-html\\s*=\\s*\"))\\s*\\'(?(\\\\\\'|[^\\'])+)\\'\\s*\\|\\s*localize(\\s|\\:|\\})", RegexOptions.Multiline | RegexOptions.IgnoreCase)); searchlist.Add("js", new Regex("Localization\\.localize\\(\\s*((\\'(?(\\\\\\'|[^\\'])+)\\')|(\\\"(?(\\\\\\\"|[^\\\"])+)\\\"))", RegexOptions.Multiline | RegexOptions.IgnoreCase)); - searchlist.Add("cs", new Regex("LC.L\\s*\\(((@\\s*\"(?(\"\"|[^\"])+))|(\"(?(\\\\\"|[^\"])+)))\"\\s*\\)", RegexOptions.Multiline | RegexOptions.IgnoreCase)); + searchlist.Add("cs", new Regex("LC.L\\s*\\(((@\\s*\"(?(\"\"|[^\"])+))|(\"(?(\\\\\"|[^\"])+)))\"\\s*(\\)|,)", RegexOptions.Multiline | RegexOptions.IgnoreCase)); var map = new Dictionary(); @@ -46,7 +50,7 @@ namespace LocalizationTool2 foreach (Match match in re.Matches(txt)) { var linepos = txt.Substring(match.Index).Count(x => x == '\n'); - var str = match.Groups["sourcestring"].Value; + var str = match.Groups["sourcestring"].Value.Replace("\n", "\\n").Replace("\r", "\\r"); LocalizationEntry le; if (!map.TryGetValue(str, out le)) map[str] = new LocalizationEntry(str, f.Substring(sourcefolder.Length), linepos); @@ -56,30 +60,32 @@ namespace LocalizationTool2 } } - File.WriteAllText(Path.Combine(sourcefolder, "translations.json"), Newtonsoft.Json.JsonConvert.SerializeObject(map.Values.OrderBy(x => x.SourceLocations.FirstOrDefault()).ToArray(), Newtonsoft.Json.Formatting.Indented)); - File.WriteAllText(Path.Combine(sourcefolder, "translations-list.json"), Newtonsoft.Json.JsonConvert.SerializeObject(map.Select(x => x.Key).OrderBy(x => x).ToArray(), Newtonsoft.Json.Formatting.Indented)); + //File.WriteAllText(Path.Combine(sourcefolder, "translations.json"), Newtonsoft.Json.JsonConvert.SerializeObject(map.Values.OrderBy(x => x.SourceLocations.FirstOrDefault()).ToArray(), Newtonsoft.Json.Formatting.Indented)); + //File.WriteAllText(Path.Combine(sourcefolder, "translations-list.json"), Newtonsoft.Json.JsonConvert.SerializeObject(map.Select(x => x.Key).OrderBy(x => x).ToArray(), Newtonsoft.Json.Formatting.Indented)); File.WriteAllLines( - Path.Combine(sourcefolder, "translations.po"), + Path.Combine(targetfolder, "localization.po"), map.OrderBy(x => x.Key).Select(x => x.Value).SelectMany(x => new string[] { "#: " + x.SourceLocations.FirstOrDefault(), - string.Format("msgid: \"{0}\"", (x.SourceString ?? "")), - string.Format("msgstr: \"{0}\"", (x.SourceString ?? "")), + string.Format("msgid \"{0}\"", (x.SourceString ?? "")), + string.Format("msgstr \"{0}\"", (x.SourceString ?? "")), "" }), System.Text.Encoding.UTF8 ); - File.WriteAllLines( - Path.Combine(sourcefolder, "translations-by-file.po"), + Console.WriteLine("Wrote {0} strings to {1}", map.Count, Path.Combine(targetfolder, "localization.po")); + + /*File.WriteAllLines( + Path.Combine(sourcefolder, "localization-by-file.po"), map.OrderBy(x => x.Value.SourceLocations.FirstOrDefault()).Select(x => x.Value).SelectMany(x => new string[] { "#: " + x.SourceLocations.FirstOrDefault(), - string.Format("msgid: \"{0}\"", (x.SourceString ?? "")), - string.Format("msgstr: \"{0}\"", (x.SourceString ?? "")), + string.Format("msgid \"{0}\"", (x.SourceString ?? "")), + string.Format("msgstr \"{0}\"", (x.SourceString ?? "")), "" }), System.Text.Encoding.UTF8 - ); + );*/ return 0;