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         //set VM parameters
00104         Locale.setDefault(this.getManagerLocale());
00105         TimeZone.setDefault(TimeZone.getTimeZone("ECT"));
00106   }
00107   
00108   public String getSystemDefaultLocale()    {
00109     String locale = PersistentPropertiesManager.getManager().getProperty("intlmanager", "local");
00110     if (locale ==null) {
00111       locale = PropertiesManager.getManager().getProperty("serve.intl.defaultinitlocal", IntlResourceManager.DEFAULT);
00112     }
00113     if (locale == null) {
00114       locale = IntlResourceManager.DEFAULT;
00115     }
00116     return locale;
00117   }
00118   
00119   public static IntlResourceManager getManager()  {
00120     if (manager == null)  {
00121       synchronized(IntlResourceManager.class) {
00122         if (manager == null)  {
00123           manager = new IntlResourceManager();
00124                   ApplicationContextManager.getManager().addManager(manager);
00125         }
00126       }
00127     }
00128     return manager;
00129   }
00130   
00131   public String getUserLocale() {
00132     SessionContext context = SessionContextManager.getManager().getSessionContext();
00133     String userlocale =  context.getUserLocale();
00134     if (userlocale == null) {
00135       userlocale = DEFAULT;
00136     }
00137     return userlocale;
00138   }
00139  
00150   public Properties loadLocalizedProperties(String fileName, String local)  throws java.io.FileNotFoundException, java.io.IOException {
00151     Properties props = new Properties();
00152     this.loadLocalizedPropertiesInProps(props, fileName, local);
00153     return props;
00154   }
00155   
00156   private void loadLocalizedPropertiesInProps(Properties props, String fileName, String local)  throws java.io.FileNotFoundException, java.io.IOException {
00157     String locfilename = this.getLocalizedFileName(fileName, true);
00158     InputStream in = new BufferedInputStream(new FileInputStream(locfilename));
00159     try {
00160         props.load(in);
00161     } finally   {
00162         in.close();
00163     }
00164   }
00165   
00171   public String getLocalizedFileName(String fileName, boolean force){
00172     //add .<local> before last point if exist.
00173     int index = fileName.lastIndexOf('.');
00174     String localized = null;
00175     if (index != -1)    {
00176       StringBuffer buff = new StringBuffer(fileName.substring(0, index));
00177       buff.append('.').append(this.getUserLocale().toLowerCase());
00178       buff.append(fileName.substring(index, fileName.length()));
00179       localized = buff.toString();
00180     } else  {
00181       StringBuffer buff = new StringBuffer(fileName);
00182       buff.append('.').append(this.getUserLocale());
00183       localized = buff.toString();
00184     }
00185     File file = new File(localized);
00186     if (force)  {
00187       if (!file.exists()) {
00188            localized = fileName;
00189       }
00190     }
00191     return localized;
00192 
00193   }
00194 
00205   public void addLocalizedProperties(String propertiesFile, String local) throws java.io.FileNotFoundException, java.io.IOException{
00206     this.loadLocalizedPropertiesInProps(localizedProperties, propertiesFile, local.toLowerCase());
00207   }
00208   
00209   public String getLocalizedProperty(String propKey)  {
00210     return localizedProperties.getProperty(propKey);
00211   }
00212   
00213   public void setLocalizedProperty(String key, String value)  {
00214     localizedProperties.put(key, value);
00215   }
00216   
00217   public String getLocalizedProperty(String propKey, String defaultValue)  {
00218     String val = localizedProperties.getProperty(propKey);
00219     if (val == null)  {
00220       val = defaultValue;
00221     }
00222     return val;
00223   }
00224   
00229   public String getManagerLocaleString()  {
00230     return systemLocal;
00231   }
00232   
00233   public void setManagerLocal(String local)     {
00234         systemLocal=local.toLowerCase();
00235         PersistentPropertiesManager.getManager().saveProperty("intlmanager", "local", systemLocal);
00236         //set VM parameters
00237         Locale.setDefault(this.getManagerLocale());
00238   }
00239   
00240   public Locale getManagerLocale()  {
00241     if (systemLocal.equals(FR)) {
00242       return Locale.FRANCE;
00243     } else if (systemLocal.equals(EN))  {
00244       return Locale.UK;
00245     } else if (systemLocal.equals(US))  {
00246       return Locale.US;
00247     } else if (systemLocal.equals(BE))  {
00248       return Locale.FRANCE;
00249     } else if (systemLocal.equals(IT))  {
00250       return Locale.ITALY;
00251     } else if (systemLocal.equals(DE))  {
00252       return Locale.GERMANY;
00253     }
00254     return Locale.US;
00255   }
00256  
00257   
00258 }

Generated on Mon Dec 4 11:03:27 2006 for OpenMobileIS by  doxygen 1.5.1-p1