ConsoleLocalizator.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.tools.ngl;
  16. import java.io.PrintStream;
  17. import java.io.UnsupportedEncodingException;
  18. import java.util.Formatter;
  19. import java.util.Locale;
  20. import java.util.Scanner;
  21. import com.l2jserver.util.osnative.CodePage;
  22. import com.l2jserver.util.osnative.WinConsole;
  23. import com.sun.jna.Platform;
  24. import com.sun.jna.Pointer;
  25. import com.sun.jna.ptr.IntByReference;
  26. /**
  27. * @author mrTJO
  28. */
  29. public class ConsoleLocalizator extends LocalizationParser
  30. {
  31. public static final String EOL = System.getProperty("line.separator");
  32. private WinConsole _wcon;
  33. private Pointer _stdout;
  34. private static PrintStream _out;
  35. Scanner _scn = new Scanner(System.in);
  36. String _baseName = "NGLConsole";
  37. /**
  38. * Load ConsoleLocalizator by using Default Locale
  39. * @param dir
  40. * @param baseName
  41. */
  42. public ConsoleLocalizator(String dir, String baseName)
  43. {
  44. this(dir, baseName, Locale.getDefault());
  45. }
  46. /**
  47. * Load ConsoleLocalizator by using a specified Locale
  48. * @param dir
  49. * @param baseName
  50. * @param locale
  51. */
  52. public ConsoleLocalizator(String dir, String baseName, Locale locale)
  53. {
  54. super(dir, baseName, locale);
  55. loadConsole();
  56. }
  57. /**
  58. * Load ConsoleLocalizator by using a custom xml file ../languages/<dir>/<baseName>_<locale>.xml
  59. * @param dir
  60. * @param baseName
  61. * @param locale
  62. */
  63. public ConsoleLocalizator(String dir, String baseName, String locale)
  64. {
  65. super(dir, baseName, locale);
  66. loadConsole();
  67. }
  68. /**
  69. * Choose the appropriate output stream for console
  70. */
  71. private void loadConsole()
  72. {
  73. if (Platform.isWindows())
  74. {
  75. try
  76. {
  77. _wcon = WinConsole.INSTANCE;
  78. if (_wcon.GetConsoleOutputCP() != 0)
  79. {
  80. // Set Console Output to UTF8
  81. _wcon.SetConsoleOutputCP(CodePage.CP_UTF8);
  82. // Set Output to STDOUT
  83. _stdout = _wcon.GetStdHandle(-11);
  84. }
  85. else
  86. {
  87. // Not running from windows console
  88. _wcon = null;
  89. }
  90. }
  91. catch (Exception e)
  92. {
  93. // Missing function in Kernel32
  94. _wcon = null;
  95. }
  96. }
  97. if (_wcon == null) // Not running windows console
  98. {
  99. try
  100. {
  101. // UTF-8 Print Stream
  102. _out = new PrintStream(System.out, true, "UTF-8");
  103. }
  104. catch (UnsupportedEncodingException e)
  105. {
  106. // UTF-8 Not Supported
  107. _out = new PrintStream(System.out, true);
  108. directPrint("Your system doesn't support UTF-8 encoding" + EOL);
  109. }
  110. }
  111. }
  112. /**
  113. * Get string from translation, add arguments and write it to console.
  114. * @param id
  115. * @param args
  116. */
  117. public void print(String id, Object... args)
  118. {
  119. String msg = getStringFromId(id);
  120. if (msg == null)
  121. {
  122. msg = formatText("Untranslated id: %s", id);
  123. }
  124. else
  125. {
  126. msg = formatText(msg, args);
  127. }
  128. directPrint(msg);
  129. }
  130. /**
  131. * Write a new line
  132. */
  133. public void println()
  134. {
  135. directPrint(EOL);
  136. }
  137. /**
  138. * Get string from translation, add arguments and write it to console with a newline at the end of string.
  139. * @param id
  140. * @param args
  141. */
  142. public void println(String id, Object... args)
  143. {
  144. String msg = getStringFromId(id);
  145. if (msg == null)
  146. {
  147. msg = formatText("Untranslated id: %s" + EOL, id);
  148. }
  149. else
  150. {
  151. msg = formatText(msg + EOL, args);
  152. }
  153. directPrint(msg);
  154. }
  155. /**
  156. * Get string from translation, add arguments and write it to console. Wait for an input and return in form of string.
  157. * @param id
  158. * @param args
  159. * @return Input String
  160. */
  161. public String inputString(String id, Object... args)
  162. {
  163. print(id, args);
  164. directPrint(": ");
  165. String ret = _scn.next();
  166. return ret;
  167. }
  168. /**
  169. * Read string from translation file and append arguments.
  170. * @param id
  171. * @param args
  172. * @return
  173. */
  174. public String getString(String id, Object... args)
  175. {
  176. String msg = getStringFromId(id);
  177. if (msg == null)
  178. {
  179. return formatText("Untranslated id: %s", id);
  180. }
  181. return formatText(msg, args);
  182. }
  183. /**
  184. * Append arguments to specified string.
  185. * @param text
  186. * @param args
  187. * @return
  188. */
  189. private String formatText(String text, Object... args)
  190. {
  191. String formattedText = null;
  192. try (Formatter form = new Formatter())
  193. {
  194. formattedText = form.format(text, args).toString();
  195. }
  196. return formattedText;
  197. }
  198. /**
  199. * Write the text into console by using UTF-8 PrintStream under UNIX environment, and Kernel32.dll under Windows.
  200. * @param message
  201. */
  202. private void directPrint(String message)
  203. {
  204. if (_wcon == null)
  205. {
  206. _out.print(message);
  207. }
  208. else
  209. {
  210. _wcon.WriteConsoleW(_stdout, message.toCharArray(), message.length(), new IntByReference(), null);
  211. }
  212. }
  213. }