LocalizationParser.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.File;
  17. import java.util.HashMap;
  18. import java.util.Locale;
  19. import java.util.Map;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. /**
  27. * @author mrTJO
  28. */
  29. public class LocalizationParser
  30. {
  31. private String LANGUAGES_DIRECTORY = "../languages/";
  32. private final Map<String, String> _msgMap = new HashMap<String, String>();
  33. private static final Logger _log = Logger.getLogger(LocalizationParser.class.getName());
  34. private final String _baseName;
  35. public LocalizationParser(String dir, String baseName, Locale locale)
  36. {
  37. LANGUAGES_DIRECTORY += dir + "/";
  38. _baseName = baseName;
  39. String language = locale.getLanguage();
  40. // String script = locale.getScript();
  41. String country = locale.getCountry();
  42. String variant = locale.getVariant();
  43. StringBuilder sb = new StringBuilder();
  44. sb.append(language);
  45. if (!country.isEmpty())
  46. {
  47. sb.append(country);
  48. }
  49. if (!variant.isEmpty())
  50. {
  51. sb.append('_' + variant);
  52. // Java 7 Function
  53. /*
  54. * if (script != "") sb.append('_'+script);
  55. */
  56. }
  57. File xml = getTranslationFile(sb.toString());
  58. parseXml(xml);
  59. }
  60. public LocalizationParser(String dir, String baseName, String locale)
  61. {
  62. LANGUAGES_DIRECTORY += dir + "/";
  63. _baseName = baseName;
  64. File xml = getTranslationFile(locale);
  65. parseXml(xml);
  66. }
  67. /**
  68. * Parse translation xml
  69. * @param xml
  70. */
  71. private void parseXml(File xml)
  72. {
  73. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  74. factory.setValidating(false);
  75. factory.setIgnoringComments(true);
  76. Document doc = null;
  77. if (xml.exists())
  78. {
  79. try
  80. {
  81. doc = factory.newDocumentBuilder().parse(xml);
  82. }
  83. catch (Exception e)
  84. {
  85. _log.log(Level.WARNING, "Could not load localization file");
  86. return;
  87. }
  88. Node n = doc.getFirstChild();
  89. NamedNodeMap docAttr = n.getAttributes();
  90. if (docAttr.getNamedItem("extends") != null)
  91. {
  92. String baseLang = docAttr.getNamedItem("extends").getNodeValue();
  93. parseXml(getTranslationFile(baseLang));
  94. }
  95. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  96. {
  97. if (d.getNodeName().equals("message"))
  98. {
  99. NamedNodeMap attrs = d.getAttributes();
  100. String id = attrs.getNamedItem("id").getNodeValue();
  101. String text = attrs.getNamedItem("text").getNodeValue();
  102. _msgMap.put(id, text);
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. * Search the translation file
  109. * @param language
  110. * @return
  111. */
  112. private File getTranslationFile(String language)
  113. {
  114. File xml = null;
  115. if (language.length() > 0)
  116. {
  117. xml = new File(LANGUAGES_DIRECTORY + _baseName + '_' + language + ".xml");
  118. }
  119. if ((language.length() > 2) && ((xml == null) || !xml.exists()))
  120. {
  121. xml = new File(LANGUAGES_DIRECTORY + _baseName + '_' + language.substring(0, 2) + ".xml");
  122. }
  123. if ((xml == null) || !xml.exists())
  124. {
  125. xml = new File(LANGUAGES_DIRECTORY + _baseName + ".xml");
  126. }
  127. return xml;
  128. }
  129. /**
  130. * Return string from specified id
  131. * @param id
  132. * @return
  133. */
  134. protected String getStringFromId(String id)
  135. {
  136. return _msgMap.get(id);
  137. }
  138. }