2
0

LocalizationParser.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.tools.ngl;
  20. import java.io.File;
  21. import java.util.HashMap;
  22. import java.util.Locale;
  23. import java.util.Map;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javax.xml.parsers.DocumentBuilderFactory;
  27. import org.w3c.dom.Document;
  28. import org.w3c.dom.NamedNodeMap;
  29. import org.w3c.dom.Node;
  30. /**
  31. * @author mrTJO
  32. */
  33. public class LocalizationParser
  34. {
  35. private String LANGUAGES_DIRECTORY = "../languages/";
  36. private final Map<String, String> _msgMap = new HashMap<>();
  37. private static final Logger _log = Logger.getLogger(LocalizationParser.class.getName());
  38. private final String _baseName;
  39. public LocalizationParser(String dir, String baseName, Locale locale)
  40. {
  41. LANGUAGES_DIRECTORY += dir + "/";
  42. _baseName = baseName;
  43. String language = locale.getLanguage();
  44. // String script = locale.getScript();
  45. String country = locale.getCountry();
  46. String variant = locale.getVariant();
  47. StringBuilder sb = new StringBuilder();
  48. sb.append(language);
  49. if (!country.isEmpty())
  50. {
  51. sb.append(country);
  52. }
  53. if (!variant.isEmpty())
  54. {
  55. sb.append('_' + variant);
  56. // Java 7 Function
  57. /*
  58. * if (script != "") sb.append('_'+script);
  59. */
  60. }
  61. File xml = getTranslationFile(sb.toString());
  62. parseXml(xml);
  63. }
  64. public LocalizationParser(String dir, String baseName, String locale)
  65. {
  66. LANGUAGES_DIRECTORY += dir + "/";
  67. _baseName = baseName;
  68. File xml = getTranslationFile(locale);
  69. parseXml(xml);
  70. }
  71. /**
  72. * Parse translation xml
  73. * @param xml
  74. */
  75. private void parseXml(File xml)
  76. {
  77. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  78. factory.setValidating(false);
  79. factory.setIgnoringComments(true);
  80. Document doc = null;
  81. if (xml.exists())
  82. {
  83. try
  84. {
  85. doc = factory.newDocumentBuilder().parse(xml);
  86. }
  87. catch (Exception e)
  88. {
  89. _log.log(Level.WARNING, "Could not load localization file");
  90. return;
  91. }
  92. Node n = doc.getFirstChild();
  93. NamedNodeMap docAttr = n.getAttributes();
  94. if (docAttr.getNamedItem("extends") != null)
  95. {
  96. String baseLang = docAttr.getNamedItem("extends").getNodeValue();
  97. parseXml(getTranslationFile(baseLang));
  98. }
  99. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  100. {
  101. if (d.getNodeName().equals("message"))
  102. {
  103. NamedNodeMap attrs = d.getAttributes();
  104. String id = attrs.getNamedItem("id").getNodeValue();
  105. String text = attrs.getNamedItem("text").getNodeValue();
  106. _msgMap.put(id, text);
  107. }
  108. }
  109. }
  110. }
  111. /**
  112. * Search the translation file
  113. * @param language
  114. * @return
  115. */
  116. private File getTranslationFile(String language)
  117. {
  118. File xml = null;
  119. if (language.length() > 0)
  120. {
  121. xml = new File(LANGUAGES_DIRECTORY + _baseName + '_' + language + ".xml");
  122. }
  123. if ((language.length() > 2) && ((xml == null) || !xml.exists()))
  124. {
  125. xml = new File(LANGUAGES_DIRECTORY + _baseName + '_' + language.substring(0, 2) + ".xml");
  126. }
  127. if ((xml == null) || !xml.exists())
  128. {
  129. xml = new File(LANGUAGES_DIRECTORY + _baseName + ".xml");
  130. }
  131. return xml;
  132. }
  133. /**
  134. * Return string from specified id
  135. * @param id
  136. * @return
  137. */
  138. protected String getStringFromId(String id)
  139. {
  140. return _msgMap.get(id);
  141. }
  142. }