Author: adrianc
Date: Thu Jan 3 09:45:43 2008 New Revision: 608571 URL: http://svn.apache.org/viewvc?rev=608571&view=rev Log: More refinements to properties file resolving. Many thanks to Scott Gray for his feedback. Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilProperties.java Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilProperties.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilProperties.java?rev=608571&r1=608570&r2=608571&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilProperties.java (original) +++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilProperties.java Thu Jan 3 09:45:43 2008 @@ -477,20 +477,13 @@ ResourceBundle bundle = null; try { bundle = UtilResourceBundle.getBundle(resource, locale, (ClassLoader) null); - } catch (MissingResourceException e) {} - if (bundle == null && !resource.endsWith(".xml")) { - try { - // Try custom XML format - bundle = UtilResourceBundle.getBundle(resource + ".xml", locale, (ClassLoader) null); - } catch (MissingResourceException e) {} - } - if (bundle == null) { - String resourceCacheKey = resource + "_" + locale.toString(); + } catch (MissingResourceException e) { + String resourceCacheKey = createResourceName(resource, locale, false); if (!resourceNotFoundMessagesShown.contains(resourceCacheKey)) { resourceNotFoundMessagesShown.add(resourceCacheKey); - Debug.log("[UtilProperties.getPropertyValue] could not find resource: " + resource + " for locale " + locale.toString(), module); + Debug.log("[UtilProperties.getPropertyValue] could not find resource: " + resource + " for locale " + locale, module); } - throw new IllegalArgumentException("Could not find resource bundle [" + resource + "] in the locale [" + locale.toString() + "]"); + throw new IllegalArgumentException("Could not find resource bundle [" + resource + "] in the locale [" + locale + "]"); } return bundle; } @@ -539,6 +532,19 @@ properties = null; } } + if (UtilValidate.isEmpty(properties) && !resource.endsWith(".xml")) { + url = resolvePropertiesUrl(resource + ".xml", locale); + if (url != null) { + try { + properties = new ExtendedProperties(url, locale); + } catch (Exception e) { + if (UtilValidate.isNotEmpty(e.getMessage())) { + Debug.log(e.getMessage(), module); + } + properties = null; + } + } + } if (UtilValidate.isNotEmpty(properties)) { Debug.logInfo("Loaded " + properties.size() + " properties for: " + resource + " (" + locale + ")", module); } @@ -637,15 +643,22 @@ * a locale. * @param resource The desired resource * @param locale The desired locale + * @param removeExtension Remove file extension from resource String * @return Localized resource name */ - public static String createResourceName(String resource, Locale locale) { - if (locale == null) { - return resource; - } + public static String createResourceName(String resource, Locale locale, boolean removeExtension) { String resourceName = resource; - if (UtilValidate.isNotEmpty(locale.toString())) { - resourceName = resourceName + "_" + locale; + if (removeExtension) { + if (resourceName.endsWith(".xml")) { + resourceName = resourceName.replace(".xml", ""); + } else if (resourceName.endsWith(".properties")) { + resourceName = resourceName.replace(".properties", ""); + } + } + if (locale != null) { + if (UtilValidate.isNotEmpty(locale.toString())) { + resourceName = resourceName + "_" + locale; + } } return resourceName; } @@ -677,7 +690,7 @@ if (UtilValidate.isEmpty(resource)) { throw new IllegalArgumentException("resource cannot be null or empty"); } - String resourceName = createResourceName(resource, locale); + String resourceName = createResourceName(resource, locale, false); if (propertiesNotFound.contains(resourceName)) { return null; } @@ -803,7 +816,7 @@ } public static ResourceBundle getBundle(String resource, Locale locale, ClassLoader loader) throws MissingResourceException { - String resourceName = createResourceName(resource, locale); + String resourceName = createResourceName(resource, locale, true); ResourceBundle bundle = bundleCache.get(resourceName); if (bundle == null) { synchronized (bundleCache) { @@ -816,7 +829,7 @@ while (candidateLocales.size() > 0) { Locale candidateLocale = candidateLocales.removeLast(); // ResourceBundles are connected together as a singly-linked list - String parentName = createResourceName(resource, candidateLocale); + String parentName = createResourceName(resource, candidateLocale, true); ResourceBundle lookupBundle = bundleCache.get(parentName); if (lookupBundle == null) { Properties newProps = getProperties(resource, candidateLocale); |
Free forum by Nabble | Edit this page |