001: /**********************************************************************************
002: *
003: * $Id: LocaleUtil.java 21053 2007-02-06 18:03:57Z ray@media.berkeley.edu $
004: *
005: ***********************************************************************************
006: *
007: * Copyright (c) 2007 The Regents of the University of California
008: *
009: * Licensed under the Educational Community License, Version 1.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.opensource.org/licenses/ecl1.php
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: *
021: **********************************************************************************/package org.sakaiproject.jsf.util;
022:
023: import java.lang.reflect.Constructor;
024: import java.lang.reflect.InvocationTargetException;
025: import java.lang.reflect.Method;
026: import java.util.Locale;
027: import java.util.ResourceBundle;
028:
029: import javax.faces.context.FacesContext;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: /**
035: * Instead of using standard methods of deciding which locale should be used
036: * when loading a resource, Sakai tools are requested to go through the Sakai-specific
037: * ResourceLoader class, which lets Sakai-specific settings such as site
038: * preferences be taken into account.
039: *
040: * Currently the ResourceLoader functionality is not behind an
041: * interface, and so direct references to the class will drag in dependencies
042: * on framework features. To keep the Sakai JSF tag library self-sufficient,
043: * this utility class uses JavaBean-style introspection to load and use
044: * the ResourceLoader class if it's available, or to fall back to the standard
045: * UIViewRoot "getLocale()" method.
046: *
047: * Note that we only use the "getLocale()" method in ResourceLoader.
048: * For details, see the comments to SAK-6886.
049: */
050: public class LocaleUtil {
051: private static final Log log = LogFactory.getLog(LocaleUtil.class);
052:
053: private static Object sakaiResourceLoader = null;
054: private static Method sakaiResourceLoaderGetLocale;
055: private static boolean isInitialized = false;
056:
057: private static void init() {
058: if (!isInitialized) {
059: // Try to load the Sakai localization class.
060: try {
061: Class sakaiResourceLoaderClass = Class
062: .forName("org.sakaiproject.util.ResourceLoader");
063: if (log.isDebugEnabled())
064: log.debug("Found Sakai ResourceLoader class");
065: Constructor sakaiResourceLoaderConstructor = sakaiResourceLoaderClass
066: .getConstructor();
067: sakaiResourceLoaderGetLocale = sakaiResourceLoaderClass
068: .getMethod("getLocale");
069: sakaiResourceLoader = sakaiResourceLoaderConstructor
070: .newInstance();
071: } catch (ClassNotFoundException e) {
072: if (log.isDebugEnabled())
073: log
074: .debug("Did not find Sakai ResourceLoader class; will use standard JSF localization");
075: } catch (SecurityException e) {
076: if (log.isErrorEnabled())
077: log.error("Will use standard JSF localization", e);
078: } catch (NoSuchMethodException e) {
079: if (log.isErrorEnabled())
080: log.error("Will use standard JSF localization", e);
081: } catch (IllegalArgumentException e) {
082: if (log.isErrorEnabled())
083: log.error("Will use standard JSF localization", e);
084: } catch (InstantiationException e) {
085: if (log.isErrorEnabled())
086: log.error("Will use standard JSF localization", e);
087: } catch (IllegalAccessException e) {
088: if (log.isErrorEnabled())
089: log.error("Will use standard JSF localization", e);
090: } catch (InvocationTargetException e) {
091: if (log.isErrorEnabled())
092: log.error("Will use standard JSF localization", e);
093: }
094: isInitialized = true;
095: }
096: }
097:
098: public static Locale getLocale(FacesContext context) {
099: Locale locale = null;
100: init();
101: if (sakaiResourceLoader != null) {
102: try {
103: locale = (Locale) sakaiResourceLoaderGetLocale
104: .invoke(sakaiResourceLoader);
105: } catch (IllegalArgumentException e) {
106: if (log.isErrorEnabled())
107: log.error(e);
108: } catch (IllegalAccessException e) {
109: if (log.isErrorEnabled())
110: log.error(e);
111: } catch (InvocationTargetException e) {
112: if (log.isErrorEnabled())
113: log.error(e);
114: }
115: } else {
116: // Use standard JSF approach.
117: locale = context.getViewRoot().getLocale();
118: }
119: return locale;
120: }
121:
122: public static String getLocalizedString(FacesContext context,
123: String bundleName, String key) {
124: String localized = null;
125: Locale locale = getLocale(context);
126: ResourceBundle rb = ResourceBundle
127: .getBundle(bundleName, locale);
128: if (log.isDebugEnabled())
129: log.debug("getLocalizedString; locale="
130: + locale.getDisplayName() + ", bundleName="
131: + bundleName + ", rb=" + rb.getLocale()
132: + ", rb getCountry()="
133: + rb.getLocale().getCountry());
134: localized = rb.getString(key);
135: return localized;
136: }
137:
138: }
|