1 13 14 package org.ejbca.ui.web.admin.configuration; 15 16 import java.io.FileInputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 20 import javax.servlet.ServletContext ; 21 22 import org.ejbca.core.model.InternalResources; 23 import org.ejbca.core.model.ra.raadmin.GlobalConfiguration; 24 25 26 33 public class WebLanguages implements java.io.Serializable { 34 35 36 private static final InternalResources intres = InternalResources.getInstance(); 37 38 40 41 private void init(ServletContext servletContext, GlobalConfiguration globalconfiguration) throws IOException { 42 if(languages == null){ 43 availablelanguages=null; 45 46 String availablelanguagesstring = globalconfiguration.getAvailableLanguagesAsString(); 47 availablelanguages = availablelanguagesstring.split(","); 48 for(int i=0; i < availablelanguages.length;i++){ 49 availablelanguages[i] = availablelanguages[i].trim().toUpperCase(); 50 } 51 languages = new LanguageProperties[availablelanguages.length]; 53 for(int i = 0; i < availablelanguages.length; i++){ 54 languages[i] = new LanguageProperties(); 55 String propsfile = "/" + globalconfiguration.getLanguagePath() + "/" 56 + globalconfiguration.getLanguageFilename() + "." 57 + availablelanguages[i].toLowerCase() +".properties"; 58 59 InputStream is = null; 60 if (servletContext != null) { 61 is = servletContext.getResourceAsStream(propsfile); 62 } else { 63 is = this.getClass().getResourceAsStream(propsfile); 64 } 65 if(is==null) { 66 is = new FileInputStream ("/tmp"+propsfile); 68 } 69 languages[i].load(is); 70 } 71 } 72 } 73 74 public WebLanguages(ServletContext servletContext, GlobalConfiguration globalconfiguration, int preferedlang, int secondarylang) throws IOException { 75 init(servletContext, globalconfiguration); 76 this.userspreferedlanguage=preferedlang; 77 this.userssecondarylanguage=secondarylang; 78 } 79 80 81 82 public String getText(String template){ 83 String returnvalue = null; 84 try{ 85 returnvalue= languages[userspreferedlanguage].getProperty(template); 86 if(returnvalue == null){ 87 returnvalue= languages[userssecondarylanguage].getProperty(template); 88 } 89 if(returnvalue == null){ 90 returnvalue= intres.getLocalizedMessage(template); 91 } 92 }catch(java.lang.NullPointerException e){} 93 if(returnvalue == null) 94 returnvalue= "No text available"; 95 return returnvalue; 96 } 97 98 99 public String [] getAvailableLanguages(){ 100 return availablelanguages; 101 } 102 103 104 private int userspreferedlanguage; 106 private int userssecondarylanguage; 107 108 private String [] availablelanguages; 109 private LanguageProperties[] languages = null; 110 111 } 112 | Popular Tags |