Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

IntlResourceManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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.util.PropertiesManager;
00041 import org.openmobileis.embedded.util.ServicePropertiesManager;
00042 
00043 import java.util.Locale;
00044 
00045 
00053 public final class IntlResourceManager {
00057   public final static String DEFAULT="fr"; 
00061   public final static String FR="fr"; 
00065   public final static String US="us";
00069   public final static String EN="en";
00073   public final static String BE="be";
00077   public final static String SW="sw";
00081   public final static String DE="de";
00085   public final static String IT="it";
00089   public final static String ES="es";
00090 
00091   private static IntlResourceManager manager;
00092   
00093   private Properties localizedProperties;
00094   
00095   private String systemLocal;
00096   
00098   private IntlResourceManager() {
00099     localizedProperties = new Properties();
00100     systemLocal = ServicePropertiesManager.getManager().getProperty("intlmanager", "local");
00101     if (systemLocal ==null) {
00102         systemLocal = PropertiesManager.getManager().getProperty("serve.intl.defaultinitlocal", IntlResourceManager.DEFAULT);
00103     }
00104                 //set VM parameters
00105                 Locale.setDefault(this.getManagerLocale());
00106                 TimeZone.setDefault(TimeZone.getTimeZone("ECT"));
00107   }
00108   
00109   public static IntlResourceManager getManager()  {
00110     if (manager == null)  {
00111       synchronized(IntlResourceManager.class) {
00112         if (manager == null)  {
00113           manager = new IntlResourceManager();
00114                                         ApplicationContextManager.getManager().addManager(manager);
00115         }
00116       }
00117     }
00118     return manager;
00119   }
00120   
00131   public Properties loadLocalizedProperties(String fileName, String local)  throws java.io.FileNotFoundException, java.io.IOException {
00132     Properties props = new Properties();
00133     this.loadLocalizedPropertiesInProps(props, fileName, local);
00134     return props;
00135   }
00136   
00137   private void loadLocalizedPropertiesInProps(Properties props, String fileName, String local)  throws java.io.FileNotFoundException, java.io.IOException {
00138     File file = new File(fileName+"."+local.toLowerCase());
00139     if (!file.exists()) {
00140       file = new File(fileName); 
00141     }
00142         InputStream in = new BufferedInputStream(new FileInputStream(file));
00143     try {
00144         props.load(in);
00145     } finally   {
00146         in.close();
00147     }
00148   }
00149   
00154   public String getLocalizedFileName(String fileName){
00155     String localized = fileName+"."+systemLocal;
00156     File file = new File(localized);
00157     if (!file.exists()) {
00158       localized = fileName;
00159     }
00160     return localized;
00161 
00162   }
00163   
00168   public String removeLocaleFromFileName(String fileName){
00169     if (fileName.endsWith(systemLocal))  {
00170       int index = fileName.lastIndexOf( '.' );
00171       if (index != -1)  {
00172         fileName = fileName.substring(0, index);
00173       }
00174     }
00175     return fileName;
00176 
00177   }
00188   public void addLocalizedProperties(String propertiesFile, String local) throws java.io.FileNotFoundException, java.io.IOException{
00189     this.loadLocalizedPropertiesInProps(localizedProperties, propertiesFile, local);
00190   }
00191   
00192   public String getLocalizedProperty(String propKey)  {
00193     return localizedProperties.getProperty(propKey);
00194   }
00195   
00196   public void setLocalizedProperty(String key, String value)  {
00197     localizedProperties.put(key, value);
00198   }
00199   
00200   public String getLocalizedProperty(String propKey, String defaultValue)  {
00201     String val = localizedProperties.getProperty(propKey);
00202     if (val == null)  {
00203       val = defaultValue;
00204     }
00205     return val;
00206   }
00207   
00212   public String getManagerLocaleString()  {
00213     return systemLocal;
00214   }
00215   
00216   public void setManagerLocal(String local)     {
00217         systemLocal=local;
00218                 ServicePropertiesManager.getManager().saveProperty("intlmanager", "local", systemLocal);
00219                 //set VM parameters
00220                 Locale.setDefault(this.getManagerLocale());
00221   }
00222   
00223   public Locale getManagerLocale()  {
00224     if (systemLocal.equals(FR)) {
00225       return Locale.FRANCE;
00226     } else if (systemLocal.equals(EN))  {
00227       return Locale.UK;
00228     } else if (systemLocal.equals(US))  {
00229       return Locale.US;
00230     } else if (systemLocal.equals(BE))  {
00231       return Locale.FRANCE;
00232     } else if (systemLocal.equals(IT))  {
00233       return Locale.ITALY;
00234     } else if (systemLocal.equals(DE))  {
00235       return Locale.GERMANY;
00236     }
00237     return Locale.US;
00238   }
00239  
00240   
00241 }

Generated on Wed Dec 14 21:05:33 2005 for OpenMobileIS by  doxygen 1.4.4