/NLocalizer/NLocalizer/LangReader.cs

http://nlocalizer.codeplex.com · C# · 203 lines · 144 code · 8 blank · 51 comment · 56 complexity · 43bd519eb9c84e9eab2583ca9277326e MD5 · raw file

  1. /*******************************************************************************
  2. NLocalizer (C) 2010-2012 Chris Prusik (KAP1 Ltd www.kap1.net)
  3. The fast, simple solution for localizing .NET applications, by text files.
  4. Latest version: http://NLocalizer.codeplex.com/
  5. $Id$
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. *******************************************************************************/
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Text;
  18. using System.IO;
  19. using System.Windows.Forms;
  20. namespace NLocalizer
  21. {
  22. /// <summary>
  23. /// Helper which read language files from application directory
  24. /// </summary>
  25. public static class LangReader
  26. {
  27. /// <summary>
  28. /// Reads language files from application directory into the specified translation.
  29. /// </summary>
  30. /// <param name="translation">The translation.</param>
  31. public static void Read(Translation translation)
  32. {
  33. Read(Path.GetDirectoryName(Application.ExecutablePath), translation);
  34. }
  35. /// <summary>
  36. /// Reads language files from specified directory into specified translation.
  37. /// </summary>
  38. /// <param name="directoryName">Name of the directory.</param>
  39. /// <param name="translation">The translation.</param>
  40. public static void Read(string directoryName, Translation translation)
  41. {
  42. foreach (string fileName in Directory.GetFiles(directoryName, "*.lang", SearchOption.AllDirectories))
  43. Read(Path.GetFileNameWithoutExtension(fileName),
  44. File.ReadAllLines(fileName), translation);
  45. }
  46. /// <summary>
  47. /// Reads language from lines into the specified translation.
  48. /// </summary>
  49. /// <param name="language">The language.</param>
  50. /// <param name="lines">The lines.</param>
  51. /// <param name="translation">The translation.</param>
  52. public static void Read(string language, string[] lines, Translation translation)
  53. {
  54. for (int pos = 0; pos < lines.Length; pos++)
  55. {
  56. string line = lines[pos].Trim();
  57. if (line.ToLower().StartsWith("debug"))
  58. {
  59. if (line.Length > "debug".Length)
  60. translation.DebugFileName = line.Substring("debug ".Length).Trim();
  61. else
  62. translation.DebugFileName = "NLocalizer.cs";
  63. }
  64. else if (line.ToLower().StartsWith("log"))
  65. {
  66. if (line.Length > "log".Length)
  67. translation.LogFileName = line.Substring("log ".Length).Trim();
  68. else
  69. translation.LogFileName = "NLocalizer.log";
  70. }
  71. else if (line.ToLower().StartsWith("template"))
  72. {
  73. string fileName = "";
  74. if (line.Length > "template".Length)
  75. fileName = line.Substring("template ".Length).Trim();
  76. else
  77. fileName = "NLocalizer.tpl";
  78. if (File.Exists(fileName))
  79. RuntimeCompiler.CodeTemplatesFileName = fileName;
  80. }
  81. else if (line.ToLower().StartsWith("framework "))
  82. {
  83. translation.FrameworkVersion = line.Substring("framework ".Length).Trim();
  84. }
  85. else if (line.ToLower().StartsWith("locale "))
  86. {
  87. line = line.Substring("locale ".Length).Trim();
  88. string[] items = line.Split(',');
  89. foreach (string item in items)
  90. if (item.Trim() != "")
  91. translation.Locales[item.Trim()] = language;
  92. }
  93. else if (line.ToLower().StartsWith("module "))
  94. {
  95. line = line.Substring("module ".Length).Trim();
  96. string[] items = line.Split(',');
  97. foreach (string item in items)
  98. if (item.Trim() != "" && translation.CodeDlls.Contains(item.Trim()) == false)
  99. translation.CodeDlls.Add(item.Trim());
  100. }
  101. else if (line.ToLower().StartsWith("dll "))
  102. {
  103. line = line.Substring("dll ".Length).Trim();
  104. string[] items = line.Split(',');
  105. foreach (string item in items)
  106. if (item.Trim() != "" && translation.CodeDlls.Contains(item.Trim()) == false)
  107. translation.CodeDlls.Add(item.Trim());
  108. }
  109. else if (line.ToLower().StartsWith("using "))
  110. {
  111. line = line.Substring("using ".Length).Trim();
  112. string[] items = line.Split(',');
  113. foreach (string item in items)
  114. if (item.Trim() != "" && translation.CodeUsings.Contains(item.Trim()) == false)
  115. translation.CodeUsings.Add(item.Trim());
  116. }
  117. else if (line.ToLower().StartsWith("imports "))
  118. {
  119. line = line.Substring("imports ".Length).Trim();
  120. string[] items = line.Split(',');
  121. foreach (string item in items)
  122. if (item.Trim() != "" && translation.CodeUsings.Contains(item.Trim()) == false)
  123. translation.CodeUsings.Add(item.Trim());
  124. }
  125. else if (line.ToLower().StartsWith("static "))
  126. {
  127. line = line.Substring("static ".Length).Trim();
  128. string[] items = line.Split(',');
  129. foreach (string item in items)
  130. if (item.Trim() != "" && translation.StaticClasses.Contains(item.Trim()) == false)
  131. translation.StaticClasses.Add(item.Trim());
  132. }
  133. else if (line.Trim() != "" && line.StartsWith(";") == false)
  134. {
  135. string className = "";
  136. string propertyName = "";
  137. string valueText = "";
  138. bool isStatic = false;
  139. Decode(language, line, ref className, ref propertyName, ref valueText, ref isStatic);
  140. translation.SetProperty(language, className, propertyName, valueText, isStatic);
  141. }
  142. }
  143. }
  144. /// <summary>
  145. /// Replace special chars \r \n \t \\ \"
  146. /// </summary>
  147. /// <param name="message">input text</param>
  148. /// <returns></returns>
  149. public static string ReplaceSpecialChars(string message)
  150. {
  151. List<string> macros = Macro.GetMacros(message);
  152. message = Macro.NamesToNumbers(message, macros);
  153. message = message.Replace(@"\r", "\r").
  154. Replace(@"\n", "\n").
  155. Replace(@"\t", "\t").
  156. Replace("\\\"", "\"").
  157. Replace(@"\\", "\\");
  158. message = Macro.NumbersToNames(message, macros);
  159. return message;
  160. }
  161. /// <summary>
  162. /// Decode the specified line of language.
  163. /// </summary>
  164. /// <param name="language">The language.</param>
  165. /// <param name="line">The line.</param>
  166. /// <param name="className">Name of the class.</param>
  167. /// <param name="propertyName">Name of the property.</param>
  168. /// <param name="valueText">The value text.</param>
  169. /// <param name="isStatic">Is the property static?</param>
  170. public static void Decode(string language, string line,
  171. ref string className, ref string propertyName, ref string valueText, ref bool isStatic)
  172. {
  173. isStatic = line.StartsWith("!");
  174. if (isStatic)
  175. line = line.Substring(1).Trim();
  176. if (line.StartsWith("(") == false)
  177. throw new Exception(String.Format("Expected ( in line: {0}", line));
  178. line = line.Substring(1).Trim();
  179. if (line.IndexOf(")") < 0)
  180. throw new Exception(String.Format("Expected ) in line: {0}", line));
  181. className = line.Substring(0, line.IndexOf(")")).Trim();
  182. line = line.Substring(line.IndexOf(")") + 1).Trim();
  183. if (line.IndexOf("=") < 0)
  184. throw new Exception(String.Format("Expected = in line: {0}", line));
  185. propertyName = line.Substring(0, line.IndexOf("=")).Trim();
  186. line = line.Substring(line.IndexOf("=") + 1).Trim();
  187. valueText = ReplaceSpecialChars(line);
  188. }
  189. }
  190. }