Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
$Id: LocaleUtil.java 105077 2012-02-24 22:54:29Z ottenhoff@longsight.com $ Copyright (c) 2007, 2008 The Sakai Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.opensource.org/licenses/ECL-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 package org.sakaiproject.jsf.util;
 
 
 
Instead of using standard methods of deciding which locale should be used when loading a resource, Sakai tools are requested to go through the Sakai-specific ResourceLoader class, which lets Sakai-specific settings such as site preferences be taken into account. Currently the ResourceLoader functionality is not behind an interface, and so direct references to the class will drag in dependencies on framework features. To keep the Sakai JSF tag library self-sufficient, this utility class uses JavaBean-style introspection to load and use the ResourceLoader class if it's available, or to fall back to the standard UIViewRoot "getLocale()" method. Note that we only use the "getLocale()" method in ResourceLoader. For details, see the comments to SAK-6886.
 
 public class LocaleUtil {
 	private static final Log log = LogFactory.getLog(LocaleUtil.class);
 	
 	private static Object sakaiResourceLoader = null;
 	private static boolean isInitialized = false;
 	
 	private static void init() {
 		if (!) {
 			// Try to load the Sakai localization class.
 			try {
 				Class sakaiResourceLoaderClass = Class.forName("org.sakaiproject.util.ResourceLoader");
 				if (.isDebugEnabled()) .debug("Found Sakai ResourceLoader class");
 				Constructor sakaiResourceLoaderConstructor = sakaiResourceLoaderClass.getConstructor();
 				 = sakaiResourceLoaderClass.getMethod("getLocale");
 				 = sakaiResourceLoaderConstructor.newInstance();
 			} catch (ClassNotFoundException e) {
 				if (.isDebugEnabled()) .debug("Did not find Sakai ResourceLoader class; will use standard JSF localization");
 			} catch (SecurityException e) {
 				if (.isErrorEnabled()) .error("Will use standard JSF localization"e);
 			} catch (NoSuchMethodException e) {
 				if (.isErrorEnabled()) .error("Will use standard JSF localization"e);
 			} catch (IllegalArgumentException e) {
 				if (.isErrorEnabled()) .error("Will use standard JSF localization"e);
 			} catch (InstantiationException e) {
 				if (.isErrorEnabled()) .error("Will use standard JSF localization"e);
 			} catch (IllegalAccessException e) {
 				if (.isErrorEnabled()) .error("Will use standard JSF localization"e);
 			} catch (InvocationTargetException e) {
 				if (.isErrorEnabled()) .error("Will use standard JSF localization"e);
 			}
 			 = true;
 		}
 	}
 	
 	public static Locale getLocale(FacesContext context) {
 		Locale locale = null;
 		init();	
 		if ( != null) {
 			try {
 			} catch (IllegalArgumentException e) {
 				if (.isErrorEnabled()) .error(e);
 			} catch (IllegalAccessException e) {
 				if (.isErrorEnabled()) .error(e);
 			} catch (InvocationTargetException e) {
 				if (.isErrorEnabled()) .error(e);
 			}
else {
			// Use standard JSF approach.
			locale = context.getViewRoot().getLocale();
		}
		return locale;
	}
	public static String getLocalizedString(FacesContext contextString bundleNameString key) {
		String localized = null;
		Locale locale = getLocale(context);
		ResourceBundle rb = ResourceBundle.getBundle(bundleNamelocale);
		if (.isDebugEnabled()) .debug("getLocalizedString; locale=" + locale.getDisplayName() + ", bundleName=" + bundleName + ", rb=" + rb.getLocale() + ", rb getCountry()=" + rb.getLocale().getCountry());
		localized = rb.getString(key);
		return localized;
	}
New to GrepCode? Check out our FAQ X