ConsoleLocalizator.java 5.2 KB

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