LocalizationParser.java 4.0 KB

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