001package org.hl7.fhir.dstu2.utils; 002 003/*- 004 * #%L 005 * org.hl7.fhir.dstu2 006 * %% 007 * Copyright (C) 2014 - 2019 Health Level 7 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023 024import java.io.FileNotFoundException; 025import java.io.IOException; 026import java.util.HashMap; 027import java.util.Map; 028 029import javax.xml.parsers.DocumentBuilder; 030import javax.xml.parsers.DocumentBuilderFactory; 031import javax.xml.parsers.ParserConfigurationException; 032 033import org.hl7.fhir.utilities.CSFileInputStream; 034import org.hl7.fhir.utilities.xml.XMLUtil; 035import org.w3c.dom.Document; 036import org.w3c.dom.Element; 037import org.xml.sax.SAXException; 038 039public class Translations { 040 041 private String[] lang; 042 private Map<String, Element> messages = new HashMap<String, Element>(); 043 044 /** 045 * Set a default language to use 046 * 047 * @param lang 048 */ 049 public void setLang(String lang) { 050 this.lang = lang.split("[.;]"); 051 } 052 053 /** 054 * Load from the XML translations file maintained by the FHIR project 055 * 056 * @param filename 057 * @throws IOException 058 * @throws SAXException 059 * @throws FileNotFoundException 060 * @throws ParserConfigurationException 061 * @throws Exception 062 */ 063 public void load(String filename) throws FileNotFoundException, SAXException, IOException, ParserConfigurationException { 064 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 065 DocumentBuilder builder = factory.newDocumentBuilder(); 066 loadMessages(builder.parse(new CSFileInputStream(filename))); 067 } 068 069 private void loadMessages(Document doc) { 070 // TODO Auto-generated method stub 071 Element element = XMLUtil.getFirstChild(doc.getDocumentElement()); 072 while (element != null) { 073 messages.put(element.getAttribute("id"), element); 074 element = XMLUtil.getNextSibling(element); 075 } 076 } 077 078 public boolean hasTranslation(String id) { 079 return messages.containsKey(id); 080 } 081 082 /** 083 * use configured language 084 * 085 * @param id - the id of the message to retrieve 086 * @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) 087 * @return the message 088 */ 089 public String getMessage(String id, String defaultMsg) { 090 return getMessage(id, lang, defaultMsg); 091 } 092 093 /** 094 * return the message in a specified language 095 * 096 * @param id - the id of the message to retrieve 097 * @param lang - a language string from a browser 098 * @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) 099 * @return the message 100 */ 101 public String getMessage(String id, String lang, String defaultMsg) { 102 return getMessage(id, lang.split("[.;]"), defaultMsg); 103 } 104 105 private String getMessage(String id, String[] lang, String defaultMsg) { 106 Element msg = messages.get(id); 107 if (msg == null) 108 return defaultMsg; 109 for (String l : lang) { 110 String res = getByLang(msg, l); 111 if (res != null) 112 return res; 113 } 114 if (defaultMsg == null) { 115 String res = getByLang(msg, "en"); 116 if (res != null) 117 return res; 118 } 119 return defaultMsg; 120 } 121 122 private String getByLang(Element msg, String lang) { 123 Element c = XMLUtil.getFirstChild(msg); 124 while (c != null) { 125 if (c.getAttribute("lang").equals(lang)) 126 return c.getTextContent(); 127 c = XMLUtil.getNextSibling(c); 128 } 129 return null; 130 } 131 132 // http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes 133 public String getLangDesc(String s) { 134 if (s.equals("en")) 135 return "English"; 136 if (s.equals("nl")) 137 return "Nederlands (Dutch)"; 138 if (s.equals("de")) 139 return "Deutsch (German)"; 140 if (s.equals("du")) 141 return "\u0440\u0443\u0301\u0441\u0441\u043a\u0438\u0439 (Russian)"; 142 return "\"" + s + "\""; 143 } 144 145 146}