ConsoleLocalizator.java 5.2 KB

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