001 package com.khubla.pragmatach.plugin.i8n; 002 003 import java.io.InputStream; 004 import java.util.Date; 005 import java.util.Hashtable; 006 import java.util.Properties; 007 008 import com.khubla.pragmatach.framework.api.I8NProvider; 009 import com.khubla.pragmatach.framework.api.PragmatachException; 010 import com.khubla.pragmatach.plugin.i8n.plugin.PluginImpl; 011 012 /** 013 * @author tome 014 */ 015 public class I8NImpl implements I8NProvider { 016 /** 017 * props file 018 */ 019 private final static String I8N_FILE = "i8n.properties_"; 020 /** 021 * properties 022 */ 023 private final Hashtable<String, Properties> localeData = new Hashtable<String, Properties>(); 024 /** 025 * the owning plugin 026 */ 027 private final PluginImpl plugin; 028 029 /** 030 * ctor 031 */ 032 public I8NImpl(PluginImpl plugin) { 033 this.plugin = plugin; 034 } 035 036 @Override 037 public String getDate(String locale, Date date) throws PragmatachException { 038 return date.toString(); 039 } 040 041 /** 042 * get the props for a certain locale 043 */ 044 private Properties getLocaleData(String locale) throws PragmatachException { 045 try { 046 /* 047 * check locally 048 */ 049 Properties properties = localeData.get(locale); 050 if (null != properties) { 051 return properties; 052 } else { 053 /* 054 * get resource 055 */ 056 final InputStream inputStream = plugin.getPluginContext().getResourceLoader().getResource(getLocalfileResourceName(locale)); 057 if (null != inputStream) { 058 properties = new Properties(); 059 properties.load(inputStream); 060 localeData.put(locale, properties); 061 return properties; 062 } else { 063 return null; 064 } 065 } 066 } catch (final Exception e) { 067 throw new PragmatachException("Exception in getLocaleData", e); 068 } 069 } 070 071 private String getLocalfileResourceName(String locale) { 072 return I8N_FILE + locale; 073 } 074 075 @Override 076 public String getName() { 077 return "Pragmatach file-based i8n provider"; 078 } 079 080 @Override 081 public String getString(String locale, String name) throws PragmatachException { 082 final Properties properties = getLocaleData(locale); 083 if (null != properties) { 084 return properties.getProperty(name); 085 } 086 return null; 087 } 088 }