1 5 6 package org.exoplatform.services.resources.impl; 7 8 import java.util.HashMap ; 9 import java.util.Locale ; 10 import java.util.Map ; 11 import java.util.ResourceBundle ; 12 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 16 import org.exoplatform.container.PortalContainer; 17 import org.exoplatform.services.resources.LocaleConfig; 18 import org.exoplatform.services.resources.ResourceBundleService; 19 20 24 public class LocaleConfigImpl implements LocaleConfig{ 25 26 static private Map predefinedLocaleMap_ = null ; 27 static { 28 predefinedLocaleMap_ = new HashMap (10) ; 29 predefinedLocaleMap_.put("us" , Locale.US) ; 30 predefinedLocaleMap_.put("en" , Locale.ENGLISH) ; 31 predefinedLocaleMap_.put("fr" , Locale.FRANCE) ; 32 predefinedLocaleMap_.put("zh" , Locale.SIMPLIFIED_CHINESE) ; 33 } 34 35 private Locale locale_; 36 private String outputEncoding_; 37 private String inputEncoding_; 38 private String description_ ; 39 private String localeName_ ; 40 41 public LocaleConfigImpl() { 42 43 } 44 45 public final String getDescription() { return description_; } 46 public final void setDescription(String desc) { description_ = desc; } 47 48 public final String getOutputEncoding() { return outputEncoding_; } 49 public final void setOutputEncoding(String enc) { outputEncoding_ = enc; } 50 51 public final String getInputEncoding() { return inputEncoding_ ; } 52 public final void setInputEncoding(String enc) { inputEncoding_ = enc;} 53 54 public final Locale getLocale() { return locale_ ; } 55 public final void setLocale(Locale locale) { locale_ = locale; } 56 public final void setLocale(String localeName) { 57 localeName_ = localeName ; 58 locale_ = (Locale ) predefinedLocaleMap_.get(localeName) ; 59 if(locale_ == null) locale_ = new Locale (localeName) ; 60 } 61 62 public final String getLanguage() { return locale_.getLanguage() ; } 63 64 public final String getLocaleName() { return localeName_ ; } 65 66 public ResourceBundle getResourceBundle(String name) { 67 ResourceBundleService service = 68 (ResourceBundleService)PortalContainer.getComponent(ResourceBundleService.class) ; 69 ResourceBundle res = service.getResourceBundle(name, locale_) ; 70 return res ; 71 } 72 73 public ResourceBundle getMergeResourceBundle(String [] names) { 74 ResourceBundleService service = 75 (ResourceBundleService)PortalContainer.getComponent(ResourceBundleService.class) ; 76 ResourceBundle res = service.getResourceBundle(names, locale_) ; 77 return res ; 78 } 79 80 public ResourceBundle getOwnerResourceBundle(String owner) { 81 PortalContainer manager = PortalContainer.getInstance() ; 82 ResourceBundleService service = 83 (ResourceBundleService)manager.getComponentInstanceOfType(ResourceBundleService.class) ; 84 try { 85 ResourceBundle res = service.getResourceBundle("locale.users." + owner, locale_) ; 86 if(res == null ) { 87 res = service.getResourceBundle("locale.users.default", locale_) ; 88 } 89 return res ; 90 } catch (Exception ex) { 91 return service.getResourceBundle("locale.users.default", locale_) ; 92 } 93 } 94 95 public void setInput(HttpServletRequest req) throws java.io.UnsupportedEncodingException { 96 req.setCharacterEncoding(inputEncoding_) ; 97 } 98 99 public void setOutput(HttpServletResponse res) { 100 res.setContentType("text/html; charset=" + outputEncoding_) ; 101 res.setLocale(locale_) ; 102 } 103 } | Popular Tags |