You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by dl...@apache.org on 2002/02/27 18:17:25 UTC
cvs commit: jakarta-turbine-2/src/java/org/apache/turbine/services/localization Localization.java LocalizationService.java LocalizationTool.java TurbineLocalizationService.java
dlr 02/02/27 09:17:24
Modified: src/java/org/apache/turbine/services/localization
Localization.java LocalizationService.java
LocalizationTool.java
TurbineLocalizationService.java
Log:
Backport of functionality from Fulcrum's LocalizationService.
PR: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=6241
Submitted by: Chris Kimpton <ki...@yahoo.com>
Revision Changes Path
1.5 +14 -2 jakarta-turbine-2/src/java/org/apache/turbine/services/localization/Localization.java
Index: Localization.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/localization/Localization.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -u -r1.4 -r1.5
--- Localization.java 24 Dec 2001 15:41:49 -0000 1.4
+++ Localization.java 27 Feb 2002 17:17:23 -0000 1.5
@@ -86,11 +86,23 @@
*
* @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
- * @version $Id: Localization.java,v 1.4 2001/12/24 15:41:49 mpoeschl Exp $
+ * @version $Id: Localization.java,v 1.5 2002/02/27 17:17:23 dlr Exp $
*/
public abstract class Localization
{
/**
+ * Fetches the localized text from the specified bundle, ignoring
+ * any default bundles.
+ *
+ * @see LocalizationService#getString(String, Locale, String)
+ */
+ public static String getString(String bundleName, Locale locale,
+ String key)
+ {
+ return getService().getString(bundleName, locale, key);
+ }
+
+ /**
* Pulls a string out of the LocalizationService with the default
* locale values of what is defined in the
* TurbineResources.properties file for the
@@ -113,7 +125,7 @@
*/
public static String getString(String key, Locale locale)
{
- return getService().getBundle(null, locale).getString(key);
+ return getService().getString(null, locale, key);
}
/**
1.4 +17 -2 jakarta-turbine-2/src/java/org/apache/turbine/services/localization/LocalizationService.java
Index: LocalizationService.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/localization/LocalizationService.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -u -r1.3 -r1.4
--- LocalizationService.java 15 Oct 2001 20:04:04 -0000 1.3
+++ LocalizationService.java 27 Feb 2002 17:17:23 -0000 1.4
@@ -66,7 +66,7 @@
*
* @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
- * @version $Id: LocalizationService.java,v 1.3 2001/10/15 20:04:04 dlr Exp $
+ * @version $Id: LocalizationService.java,v 1.4 2002/02/27 17:17:23 dlr Exp $
*/
public interface LocalizationService
extends Service
@@ -83,7 +83,8 @@
/**
* Retrieves the name of the default bundle (as specified in the
- * config file).
+ * config file), or the first in the list if there are more than
+ * one.
*/
public String getDefaultBundleName();
@@ -171,4 +172,18 @@
* @param defaultBundle Name of default bundle.
*/
public void setBundle(String defaultBundle);
+
+
+ /**
+ * Tries very hard to return a value, looking first in the
+ * specified bundle, then searching list of default bundles
+ * (giving precedence to earlier bundles over later bundles).
+ *
+ * @param bundleName Name of the bundle to look in first.
+ * @param locale Locale to get text for.
+ * @param key Name of the text to retrieve.
+ * @return Localized text.
+ */
+ public String getString(String bundleName, Locale locale, String key);
+
}
1.2 +1 -0 jakarta-turbine-2/src/java/org/apache/turbine/services/localization/LocalizationTool.java
Index: LocalizationTool.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/localization/LocalizationTool.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -u -r1.1 -r1.2
--- LocalizationTool.java 13 Oct 2001 23:22:52 -0000 1.1
+++ LocalizationTool.java 27 Feb 2002 17:17:23 -0000 1.2
@@ -61,6 +61,7 @@
import org.apache.turbine.services.localization.Localization;
import org.apache.turbine.services.pull.ApplicationTool;
import org.apache.turbine.util.RunData;
+import org.apache.turbine.util.Log;
/**
* A pull tool which provides lookups for localized text by delegating
1.3 +140 -11 jakarta-turbine-2/src/java/org/apache/turbine/services/localization/TurbineLocalizationService.java
Index: TurbineLocalizationService.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/localization/TurbineLocalizationService.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -u -r1.2 -r1.3
--- TurbineLocalizationService.java 13 Oct 2001 23:17:04 -0000 1.2
+++ TurbineLocalizationService.java 27 Feb 2002 17:17:23 -0000 1.3
@@ -57,12 +57,14 @@
import java.util.Hashtable;
import java.util.Locale;
import java.util.ResourceBundle;
+import java.util.MissingResourceException;
import javax.servlet.http.HttpServletRequest;
import org.apache.turbine.services.InitializationException;
import org.apache.turbine.services.TurbineBaseService;
import org.apache.turbine.services.resources.TurbineResources;
import org.apache.turbine.util.RunData;
+import org.apache.turbine.util.Log;
import org.apache.turbine.util.StringUtils;
/**
@@ -103,8 +105,10 @@
*/
private Hashtable bundles = null;
- /** The name of the default bundle to use. */
- private String defaultBundle = null;
+ /**
+ * The list of default bundles to search.
+ */
+ private String[] bundleNames = null;
/**
* The name of the default locale to use (includes language and
@@ -123,6 +127,7 @@
*/
public TurbineLocalizationService()
{
+ bundles = new Hashtable();
}
/**
@@ -131,8 +136,8 @@
public void init()
throws InitializationException
{
- bundles = new Hashtable();
- defaultBundle = TurbineResources.getString("locale.default.bundle");
+ initBundleNames(null);
+
Locale jvmDefault = Locale.getDefault();
defaultLanguage = TurbineResources
.getString("locale.default.language",
@@ -144,13 +149,48 @@
setInit(true);
}
+
+ /**
+ * Initialize list of default bundle names.
+ *
+ * @param names Ignored.
+ */
+ protected void initBundleNames(String[] ignored)
+ {
+ bundleNames =
+ TurbineResources.getStringArray("locale.default.bundles");
+ String name = TurbineResources.getString("locale.default.bundle");
+
+ if (name != null && name.length() > 0)
+ {
+ // Using old-style single bundle name property.
+ if (bundleNames == null || bundleNames.length <= 0)
+ {
+ bundleNames = new String[] { name };
+ }
+ else
+ {
+ // Prepend "default" bundle name.
+ String[] array = new String[bundleNames.length + 1];
+ array[0] = name;
+ System.arraycopy(bundleNames, 0, array, 1, bundleNames.length);
+ bundleNames = array;
+ }
+ }
+ if (bundleNames == null)
+ {
+ bundleNames = new String[0];
+ }
+ }
+
/**
* Retrieves the name of the default bundle (as specified in the
* config file).
+ * @see org.apache.turbine.services.localization.LocalizationService#getDefaultBundleName()
*/
public String getDefaultBundleName()
{
- return defaultBundle;
+ return (bundleNames.length > 0 ? bundleNames[0] : "");
}
/**
@@ -162,7 +202,7 @@
*/
public ResourceBundle getBundle()
{
- return getBundle(defaultBundle, (Locale) null);
+ return getBundle(getDefaultBundleName(), (Locale) null);
}
/**
@@ -201,7 +241,7 @@
*/
public ResourceBundle getBundle(HttpServletRequest req)
{
- return getBundle(defaultBundle, getLocale(req));
+ return getBundle(getDefaultBundleName(), getLocale(req));
}
/**
@@ -229,7 +269,7 @@
*/
public ResourceBundle getBundle(RunData data)
{
- return getBundle(defaultBundle, getLocale(data.getRequest()));
+ return getBundle(getDefaultBundleName(), getLocale(data.getRequest()));
}
/**
@@ -257,7 +297,7 @@
public ResourceBundle getBundle(String bundleName, Locale locale)
{
// Assure usable inputs.
- bundleName = (bundleName == null ? defaultBundle : bundleName.trim());
+ bundleName = (bundleName == null ? getDefaultBundleName() : bundleName.trim());
if (locale == null)
{
locale = getLocale((String) null);
@@ -305,13 +345,27 @@
}
/**
- * This method sets the name of the defaultBundle.
+ * This method sets the name of the first bundle in the search
+ * list (the "default" bundle).
*
* @param defaultBundle Name of default bundle.
*/
public void setBundle(String defaultBundle)
{
- this.defaultBundle = defaultBundle;
+ if (bundleNames.length > 0)
+ {
+ bundleNames[0] = defaultBundle;
+ }
+ else
+ {
+ synchronized (this)
+ {
+ if (bundleNames.length <= 0)
+ {
+ bundleNames = new String[] { defaultBundle };
+ }
+ }
+ }
}
/**
@@ -339,4 +393,79 @@
// Couldn't parse locale.
return defaultLocale;
}
+
+
+ /**
+ * @exception MissingResourceException Specified key cannot be matched.
+ * @see org.apache.turbine.services.localization.LocalizationService#getString(String, Locale, String)
+ */
+ public String getString(String bundleName, Locale locale, String key)
+ {
+ String value = null;
+
+ if (locale == null)
+ {
+ locale = getLocale((String) null);
+ }
+
+ // Look for text in requested bundle.
+ ResourceBundle rb = getBundle(bundleName, locale);
+ value = getStringOrNull(rb, key);
+
+ // Look for text in list of default bundles.
+ if (value == null && bundleNames.length > 0)
+ {
+ String name;
+ for (int i = 0; i < bundleNames.length; i++)
+ {
+ name = bundleNames[i];
+ //System.out.println("getString(): name=" + name +
+ // ", locale=" + locale + ", i=" + i);
+ if (!name.equals(bundleName))
+ {
+ rb = getBundle(name, locale);
+ value = getStringOrNull(rb, key);
+ if (value != null)
+ {
+ locale = rb.getLocale();
+ break;
+ }
+ }
+ }
+ }
+
+ if (value == null)
+ {
+ String loc = locale.toString();
+ Log.debug(LocalizationService.SERVICE_NAME +
+ " noticed missing resource: " +
+ "bundleName=" + bundleName + ", locale=" + loc +
+ ", key=" + key);
+ // Text not found in requested or default bundles.
+ throw new MissingResourceException(bundleName, loc, key);
+ }
+
+ return value;
+ }
+
+ /**
+ * Gets localized text from a bundle if it's there. Otherwise,
+ * returns <code>null</code> (ignoring a possible
+ * <code>MissingResourceException</code>).
+ */
+ protected final String getStringOrNull(ResourceBundle rb, String key)
+ {
+ if (rb != null)
+ {
+ try
+ {
+ return rb.getString(key);
+ }
+ catch (MissingResourceException ignored)
+ {
+ }
+ }
+ return null;
+ }
+
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: cvs commit: jakarta-turbine-2/src/java/org/apache/turbine/services/localization Localization.java LocalizationService.java LocalizationTool.java TurbineLocalizationService.java
Posted by Chris Kimpton <ki...@yahoo.com>.
Hi Dan,
>
jakarta-turbine-2/src/java/org/apache/turbine/services/localization/LocalizationTool.java
>
> Index: LocalizationTool.java
>
> ===================================================================
> RCS file:
>
/home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/localization/LocalizationTool.java,v
> retrieving revision 1.1
> retrieving revision 1.2
> diff -u -u -r1.1 -r1.2
> --- LocalizationTool.java 13 Oct 2001 23:22:52 -0000 1.1
> +++ LocalizationTool.java 27 Feb 2002 17:17:23 -0000 1.2
> @@ -61,6 +61,7 @@
> import org.apache.turbine.services.localization.Localization;
> import org.apache.turbine.services.pull.ApplicationTool;
> import org.apache.turbine.util.RunData;
> +import org.apache.turbine.util.Log;
>
> /**
> * A pull tool which provides lookups for localized text by
> delegating
>
>
>
There was more to my patch in this file - the get() method was
changed to use the getString method on Localization.
Any reason for not applying that bit? I need it to be done as it
then allows the new localization feature to be used transparently in
the velocity templates. I didn't think it had any adverse effects.
Thanks,
Chris
=====
Need somewhere to Live in London? - Then go to http://freeflats.com
__________________________________________________
Do You Yahoo!?
Yahoo! Greetings - Send FREE e-cards for every occasion!
http://greetings.yahoo.com
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>