001package org.hl7.fhir.r4.utils; 002 003import java.io.FileNotFoundException; 004import java.io.IOException; 005import java.util.HashMap; 006import java.util.Map; 007 008import javax.xml.parsers.DocumentBuilder; 009import javax.xml.parsers.DocumentBuilderFactory; 010import javax.xml.parsers.ParserConfigurationException; 011 012import org.hl7.fhir.utilities.CSFileInputStream; 013import org.hl7.fhir.utilities.xml.XMLUtil; 014import org.w3c.dom.Document; 015import org.w3c.dom.Element; 016import org.xml.sax.SAXException; 017 018public class Translations { 019 020 private String[] lang; 021 private Map<String, Element> messages = new HashMap<String, Element>(); 022 023 /** 024 * Set a default language to use 025 * 026 * @param lang 027 */ 028 public void setLang(String lang) { 029 this.lang = lang.split("[.;]"); 030 } 031 032 /** 033 * Load from the XML translations file maintained by the FHIR project 034 * 035 * @param filename 036 * @throws IOException 037 * @throws SAXException 038 * @throws FileNotFoundException 039 * @throws ParserConfigurationException 040 * @throws Exception 041 */ 042 public void load(String filename) throws FileNotFoundException, SAXException, IOException, ParserConfigurationException { 043 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 044 DocumentBuilder builder = factory.newDocumentBuilder(); 045 loadMessages(builder.parse(new CSFileInputStream(filename))); 046 } 047 048 private void loadMessages(Document doc) { 049 // TODO Auto-generated method stub 050 Element element = XMLUtil.getFirstChild(doc.getDocumentElement()); 051 while (element != null) { 052 messages.put(element.getAttribute("id"), element); 053 element = XMLUtil.getNextSibling(element); 054 } 055 } 056 057 public boolean hasTranslation(String id) { 058 return messages.containsKey(id); 059 } 060 061 /** 062 * use configured language 063 * 064 * @param id - the id of the message to retrieve 065 * @param defaultMsg - string to use if the message is not defined or a language match is not found (if null, then will default to english) 066 * @return the message 067 */ 068 public String getMessage(String id, String defaultMsg) { 069 return getMessage(id, lang, defaultMsg); 070 } 071 072 /** 073 * return the message in a specified language 074 * 075 * @param id - the id of the message to retrieve 076 * @param lang - a language string from a browser 077 * @param defaultMsg - string to use if the message is not defined or a language match is not found (if null, then will default to the english message) 078 * @return the message 079 */ 080 public String getMessage(String id, String lang, String defaultMsg) { 081 return getMessage(id, lang.split("[.;]"), defaultMsg); 082 } 083 084 private String getMessage(String id, String[] lang, String defaultMsg) { 085 Element msg = messages.get(id); 086 if (msg == null) 087 return defaultMsg; 088 for (String l : lang) { 089 String res = getByLang(msg, l); 090 if (res != null) 091 return res; 092 } 093 if (defaultMsg == null) { 094 String res = getByLang(msg, "en"); 095 if (res != null) 096 return res; 097 } 098 return defaultMsg; 099 } 100 101 private String getByLang(Element msg, String lang) { 102 Element c = XMLUtil.getFirstChild(msg); 103 while (c != null) { 104 if (c.getAttribute("lang").equals(lang)) 105 return c.getTextContent(); 106 c = XMLUtil.getNextSibling(c); 107 } 108 return null; 109 } 110 111 // http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes 112 public String getLangDesc(String s) { 113 if (s.equals("en")) 114 return "English"; 115 if (s.equals("nl")) 116 return "Nederlands (Dutch)"; 117 if (s.equals("de")) 118 return "Deutsch (German)"; 119 if (s.equals("ru")) 120 return "\u0440\u0443\u0301\u0441\u0441\u043a\u0438\u0439 (Russian)"; 121 return "\"" + s + "\""; 122 } 123 124 125}