/*
 * $Id: I18nUtil.java,v 1.3 2012-07-25 09:56:35 tlipkis Exp $
 */

/*

Copyright (c) 2012 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.

*/

package org.lockss.util;

import java.util.MissingResourceException;
import org.lockss.util.Logger;
import org.xnap.commons.i18n.I18n;
import org.xnap.commons.i18n.I18nFactory;

/**
 * A utility class for I18N features.
 *
 * @author Neil Mayo
 */
public class I18nUtil {

  // Avoid circular loading dependencies.
  protected static Logger log =
    Logger.getLoggerWithInitialLevel("I18nUtil",
				     Logger.getInitialDefaultLevel());

  /** The name of the default backup bundle. This should always be in the build. */
  private static final String defaultBundle = "DefaultBundle";

  /**
   * Get a gettext-commons I18n object suitable for performing
   * localisation of strings. If the gettext bundles are not available, 
   * it will use the defaultBundle, which does no translation but does
   * perform formatting for strings with arguments.
   * <p>
   * The gettext-commons library internally caches the I18n objects.
   * 
   * @param clazz the class of the caller
   * @return an I18n object
   */
  public static I18n getI18n(Class clazz) {
    I18n i18n;
    try {
      i18n = I18nFactory.getI18n(clazz);
    } catch (MissingResourceException ex) {
      log.error("Cannot initialize i18n for " + clazz + ex.toString());
      // Try again, specifying the default bundle
      try {
        i18n = I18nFactory.getI18n(clazz, defaultBundle);
        log.warning("i18n is disabled: using "+defaultBundle);
      } catch (MissingResourceException e) {
        log.critical("Cannot initialize "+defaultBundle+" for "+clazz, e);
        i18n = null;
        // This shouldn't happen if the DefaultBundle is incorporated into the 
        // build. Is the omission a serious enough build error to stop the daemon?
      }
    }
    return i18n;
  }
  
}

@@ -26,37 +26,43 @@
 v 1.
-3
+4
  2012-0
-7-25 09:56:35 tlipkis
+8-01 15:05:32 easyonthemayo
  Exp
@@ -1488,16 +1488,97 @@
 ception;
+%0A%0Aimport org.lockss.config.Configuration;%0Aimport org.lockss.config.CurrentConfig;
 %0Aimport 
@@ -2089,16 +2089,385 @@
 ndle%22;%0A%0A
+  // -------------------------- LOCKSS CONFIG PARAMS --------------------------%0A  static final String PREFIX = Configuration.PREFIX + %22i18n.%22;%0A  /** Enable i18n using the gettext-commons-generated packages.%0A   * Daemon restart required. */%0A  public static final String PARAM_ENABLE_I18N = PREFIX + %22enabled%22;%0A  public static final boolean DEFAULT_ENABLE_I18N = false;%0A%0A
   /**%0A  
@@ -2598,17 +2598,58 @@
 ailable,
- 
+%0A   * or the config param is set to false,
 %0A   * it
@@ -2976,22 +2976,193 @@
     
-I18n i18n;%0A   
+boolean enableI18n = CurrentConfig.getBooleanParam(PARAM_ENABLE_I18N,%0A        DEFAULT_ENABLE_I18N);%0A    I18n i18n = null;%0A    // Try loading the full i18n if enabled%0A    if (enableI18n)
  try
@@ -3331,32 +3331,94 @@
     
-// Try again, specifying
+enableI18n = false;%0A    %7D%0A%0A    // If i18n disabled (manually or through failure), load
  the
@@ -3437,17 +3437,32 @@
 dle%0A    
- 
+if (!enableI18n)
  try %7B%0A 
@@ -3462,26 +3462,24 @@
 try %7B%0A      
-  
 i18n = I18nF
@@ -3512,26 +3512,24 @@
 ultBundle);%0A
-  
       log.wa
@@ -3573,26 +3573,24 @@
 ultBundle);%0A
-  
     %7D catch 
@@ -3622,26 +3622,24 @@
  e) %7B%0A      
-  
 log.critical
@@ -3695,26 +3695,24 @@
 , e);%0A      
-  
 i18n = null;
@@ -3718,18 +3718,16 @@
 ;%0A      
- 
- 
 // This 
@@ -3788,20 +3788,17 @@
 into the
- %0A  
+%0A
       //
@@ -3878,17 +3878,10 @@
     
-  %7D%0A    
 %7D
+%0A
 %0A   

Add Configuration parameter to I18nUtil to enable i18n; disabled by default.