IntlResourceManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *
00024  *  Modifications :
00025  *  2005 Creation P.Delrieu
00026  *
00027  */
00028 
00029 
00030 package org.openmobileis.common.intl;
00031 
00032 import java.util.Properties;
00033 import java.util.TimeZone;
00034 import java.io.BufferedInputStream;
00035 import java.io.File;
00036 import java.io.InputStream;
00037 import java.io.FileInputStream;
00038 
00039 import org.openmobileis.common.context.ApplicationContextManager;
00040 import org.openmobileis.common.context.SessionContext;
00041 import org.openmobileis.common.context.SessionContextManager;
00042 import org.openmobileis.common.util.PropertiesManager;
00043 import org.openmobileis.common.util.PersistentPropertiesManager;
00044 
00045 import java.util.Locale;
00046 
00047 
00055 public final class IntlResourceManager {
00059   public final static String DEFAULT="en";
00063   public final static String FR="fr";
00067   public final static String US="us";
00071   public final static String EN="en";
00075   public final static String BE="be";
00079   public final static String SW="sw";
00083   public final static String DE="de";
00087   public final static String IT="it";
00091   public final static String ES="es";
00092 
00093   private static IntlResourceManager manager;
00094 
00095   private Properties localizedProperties;
00096 
00097   private String systemLocal;
00098 
00100   private IntlResourceManager() {
00101     localizedProperties = new Properties();
00102     systemLocal = this.getSystemDefaultLocale();
00103     SessionContext context = SessionContextManager.getManager().getSessionContext();
00104     context.setUserLocale(systemLocal);
00105         //set VM parameters
00106         Locale.setDefault(this.getManagerLocale());
00107         TimeZone.setDefault(TimeZone.getTimeZone("ECT"));
00108   }
00109 
00110   public String getSystemDefaultLocale()    {
00111     String locale = PersistentPropertiesManager.getManager().getProperty("intlmanager", "local");
00112     if (locale ==null) {
00113       locale = PropertiesManager.getManager().getProperty("serve.intl.defaultinitlocal", IntlResourceManager.DEFAULT);
00114     }
00115     if (locale == null) {
00116       locale = IntlResourceManager.DEFAULT;
00117     }
00118     return locale;
00119   }
00120 
00121   public static IntlResourceManager getManager()  {
00122     if (manager == null)  {
00123       synchronized(IntlResourceManager.class) {
00124         if (manager == null)  {
00125           manager = new IntlResourceManager();
00126                   ApplicationContextManager.getManager().addManager(manager);
00127         }
00128       }
00129     }
00130     return manager;
00131   }
00132 
00133   public String getUserLocale() {
00134     SessionContext context = SessionContextManager.getManager().getSessionContext();
00135     String userlocale =  context.getUserLocale();
00136     if (userlocale == null) {
00137       userlocale = DEFAULT;
00138     }
00139     return userlocale;
00140   }
00141 
00152   public Properties loadLocalizedProperties(String fileName, String local)  throws java.io.FileNotFoundException, java.io.IOException {
00153     Properties props = new Properties();
00154     this.loadLocalizedPropertiesInProps(props, fileName, local);
00155     return props;
00156   }
00157 
00158   private void loadLocalizedPropertiesInProps(Properties props, String fileName, String local)  throws java.io.FileNotFoundException, java.io.IOException {
00159     String locfilename = this.getLocalizedFileName(fileName, true);
00160     InputStream in = new BufferedInputStream(new FileInputStream(locfilename));
00161     try {
00162         props.load(in);
00163     } finally   {
00164         in.close();
00165     }
00166   }
00167 
00173   public String getLocalizedFileName(String fileName, boolean force){
00174     //add .<local> before last point if exist.
00175     int index = fileName.lastIndexOf('.');
00176     String localized = null;
00177     if (index != -1)    {
00178       StringBuffer buff = new StringBuffer(fileName.substring(0, index));
00179       buff.append('.').append(this.getUserLocale().toLowerCase());
00180       buff.append(fileName.substring(index, fileName.length()));
00181       localized = buff.toString();
00182     } else  {
00183       StringBuffer buff = new StringBuffer(fileName);
00184       buff.append('.').append(this.getUserLocale());
00185       localized = buff.toString();
00186     }
00187     File file = new File(localized);
00188     if (force)  {
00189       if (!file.exists()) {
00190            localized = fileName;
00191       }
00192     }
00193     return localized;
00194 
00195   }
00196 
00207   public void addLocalizedProperties(String propertiesFile, String local) throws java.io.FileNotFoundException, java.io.IOException{
00208     this.loadLocalizedPropertiesInProps(localizedProperties, propertiesFile, local.toLowerCase());
00209   }
00210 
00211   public String getLocalizedProperty(String propKey)  {
00212     return localizedProperties.getProperty(propKey);
00213   }
00214 
00215   public void setLocalizedProperty(String key, String value)  {
00216     localizedProperties.put(key, value);
00217   }
00218 
00219   public String getLocalizedProperty(String propKey, String defaultValue)  {
00220     String val = localizedProperties.getProperty(propKey);
00221     if (val == null)  {
00222       val = defaultValue;
00223     }
00224     return val;
00225   }
00226 
00231   public String getManagerLocaleString()  {
00232     return systemLocal;
00233   }
00234 
00235   public void setManagerLocal(String local)     {
00236         systemLocal=local.toLowerCase();
00237           PersistentPropertiesManager.getManager().saveProperty("intlmanager", "local", systemLocal);
00238           //set user locale
00239     SessionContext context = SessionContextManager.getManager().getSessionContext();
00240     context.setUserLocale(systemLocal);
00241           //set VM parameters
00242           Locale.setDefault(this.getManagerLocale());
00243   }
00244 
00245   public Locale getManagerLocale()  {
00246     if (systemLocal.equals(FR)) {
00247       return Locale.FRANCE;
00248     } else if (systemLocal.equals(EN))  {
00249       return Locale.UK;
00250     } else if (systemLocal.equals(US))  {
00251       return Locale.US;
00252     } else if (systemLocal.equals(BE))  {
00253       return Locale.FRANCE;
00254     } else if (systemLocal.equals(IT))  {
00255       return Locale.ITALY;
00256     } else if (systemLocal.equals(DE))  {
00257       return Locale.GERMANY;
00258     }
00259     return Locale.US;
00260   }
00261 
00262 
00263 }

Generated on Mon Jan 14 17:29:47 2008 for OpenMobileIS by  doxygen 1.5.4