ConsoleLocalizator.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. private WinConsole _wcon;
  32. private Pointer _stdout;
  33. private static PrintStream _out;
  34. Scanner _scn = new Scanner(System.in);
  35. String _baseName = "NGLConsole";
  36. /**
  37. * Load ConsoleLocalizator by using Default Locale
  38. *
  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. *
  49. * @param dir
  50. * @param baseName
  51. * @param locale
  52. */
  53. public ConsoleLocalizator(String dir, String baseName, Locale locale)
  54. {
  55. super(dir, baseName, locale);
  56. loadConsole();
  57. }
  58. /**
  59. * Load ConsoleLocalizator by using a custom xml file
  60. * ../languages/<dir>/<baseName>_<locale>.xml
  61. *
  62. * @param dir
  63. * @param baseName
  64. * @param locale
  65. */
  66. public ConsoleLocalizator(String dir, String baseName, String locale)
  67. {
  68. super(dir, baseName, locale);
  69. loadConsole();
  70. }
  71. /**
  72. * Choose the appropriate output stream for console
  73. */
  74. private void loadConsole()
  75. {
  76. if (Platform.isWindows())
  77. {
  78. try
  79. {
  80. _wcon = WinConsole.INSTANCE;
  81. if (_wcon.GetConsoleOutputCP() != 0)
  82. {
  83. // Set Console Output to UTF8
  84. _wcon.SetConsoleOutputCP(CodePage.CP_UTF8);
  85. // Set Output to STDOUT
  86. _stdout = _wcon.GetStdHandle(-11);
  87. }
  88. else
  89. {
  90. // Not running from windows console
  91. _wcon = null;
  92. }
  93. }
  94. catch (Exception e)
  95. {
  96. // Missing function in Kernel32
  97. _wcon = null;
  98. }
  99. }
  100. if (_wcon == null) // Not running windows console
  101. {
  102. try
  103. {
  104. // UTF-8 Print Stream
  105. _out = new PrintStream(System.out, true, "UTF-8");
  106. }
  107. catch (UnsupportedEncodingException e)
  108. {
  109. // UTF-8 Not Supported
  110. _out = new PrintStream(System.out, true);
  111. directPrint("Your system doesn't support UTF-8 encoding\n");
  112. }
  113. }
  114. }
  115. /**
  116. * Get string from translation, add arguments
  117. * and write it to console.
  118. *
  119. * @param id
  120. * @param args
  121. */
  122. public void print(String id, Object... args)
  123. {
  124. String msg = getStringFromId(id);
  125. if (msg == null)
  126. msg = formatText("Untranslated id: %s", id);
  127. else
  128. msg = formatText(msg, args);
  129. directPrint(msg);
  130. }
  131. /**
  132. * Write a new line
  133. */
  134. public void println()
  135. {
  136. directPrint("\n");
  137. }
  138. /**
  139. * Get string from translation, add arguments
  140. * and write it to console with a newline at the
  141. * end of string.
  142. *
  143. * @param id
  144. * @param args
  145. */
  146. public void println(String id, Object... args)
  147. {
  148. String msg = getStringFromId(id);
  149. if (msg == null)
  150. msg = formatText("Untranslated id: %s\n", id);
  151. else
  152. msg = formatText(msg+"\n", args);
  153. directPrint(msg);
  154. }
  155. /**
  156. * Get string from translation, add arguments
  157. * and write it to console.
  158. * Wait for an input and return in form of string.
  159. *
  160. * @param id
  161. * @param args
  162. * @return Input String
  163. */
  164. public String inputString(String id, Object... args)
  165. {
  166. print(id, args);
  167. directPrint(": ");
  168. String ret = _scn.next();
  169. return ret;
  170. }
  171. /**
  172. * Read string from translation file and append
  173. * arguments.
  174. *
  175. * @param id
  176. * @param args
  177. * @return
  178. */
  179. public String getString(String id, Object... args)
  180. {
  181. String msg = getStringFromId(id);
  182. if (msg == null)
  183. return formatText("Untranslated id: %s", id);
  184. return formatText(msg, args);
  185. }
  186. /**
  187. * Append arguments to specified string.
  188. *
  189. * @param text
  190. * @param args
  191. * @return
  192. */
  193. private String formatText(String text, Object... args)
  194. {
  195. Formatter form = new Formatter();
  196. return form.format(text, args).toString();
  197. }
  198. /**
  199. * Write the text into console by using UTF-8
  200. * PrintStream under UNIX environment, and
  201. * Kernel32.dll under Windows.
  202. *
  203. * @param message
  204. */
  205. private void directPrint(String message)
  206. {
  207. if (_wcon == null)
  208. _out.print(message);
  209. else
  210. _wcon.WriteConsoleW(_stdout, message.toCharArray(),
  211. message.length(), new IntByReference(), null);
  212. }
  213. }