Index: lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleManager.java diff -u lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleManager.java:1.2 lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleManager.java:1.3 --- lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleManager.java:1.2 Thu Mar 20 01:29:42 2008 +++ lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleManager.java Sun Mar 23 06:02:24 2008 @@ -19,8 +19,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.codelutin.i18n.I18n; import org.codelutin.i18n.Language; -import org.codelutin.i18n.LanguageManager; import org.codelutin.util.Resource; import org.codelutin.util.StringUtil; @@ -42,7 +42,7 @@ public class I18nBundleManager { /** to use log facility, just put in your code: log.info(\"...\"); */ - private static final Log log = LogFactory.getLog(LanguageManager.class); + private static final Log log = LogFactory.getLog(I18nBundleManager.class); /** pattern to find all i18n bundles in classloader class path */ public static final String SEARCH_BUNDLE_PATTERN = ".*18n/.+\\.properties"; @@ -50,9 +50,12 @@ /** shared instance */ protected static I18nBundleManager instance; - /** le cache de languages deja charges indexees par leur encoding */ + /** le cache de bundles deja charges */ protected List cache; + /** le cache des urls de recheche des bundles */ + protected static URL[] urls; + public static synchronized I18nBundleManager getInstance() { if (instance == null) { instance = new I18nBundleManager(); @@ -60,7 +63,7 @@ return instance; } - public static void init(URL... extraUrl) { + public static void init() { I18nBundleManager manager = getInstance(); if (manager.isInit()) { // already init @@ -68,7 +71,10 @@ } // get all bundles urls - URL[] urls = manager.getURLs(Language.getLoader(), extraUrl); + if (urls == null || urls.length == 0) { + // cache this expensive search + urls = manager.getURLs(Language.getLoader(), I18n.getExtraURL()); + } long t0 = System.nanoTime(); @@ -87,6 +93,12 @@ log.info(bundles.size() + " bundle(s) found, [" + manager.getBundleEntries().length + " file(s)] in " + StringUtil.convertTime(System.nanoTime() - t0)); } + public static void resetURL() { + if (urls != null) { + urls = null; + } + } + public I18nBundle[] getBundles() { checkInit(); return cache.toArray(new I18nBundle[cache.size()]); @@ -136,7 +148,7 @@ log.info(language + ", nbEntries: " + entries.length + ", nbSentences: " + language.size() + " in " + StringUtil.convertTime(System.nanoTime() - t0)); } - public void reset() { + public void close() { if (cache != null) { cache = null; } @@ -163,6 +175,11 @@ urls.addAll(Resource.getURLs(SEARCH_BUNDLE_PATTERN, loader)); } urls.addAll(Arrays.asList(extraUrl)); + if (log.isDebugEnabled()) { + for (URL url : urls) { + log.debug(url.toString()); + } + } } catch (Exception eee) { log.warn("Unable to find urls for loader : " + loader); return new URL[0]; Index: lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleScope.java diff -u lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleScope.java:1.3 lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleScope.java:1.4 --- lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleScope.java:1.3 Sun Mar 23 00:23:23 2008 +++ lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleScope.java Sun Mar 23 06:02:24 2008 @@ -51,13 +51,15 @@ * @author chemit */ public enum I18nBundleScope { - /** default scope (with no clanguage and country information) */ + + /** default scope (with no language, nor country information) */ GENERAL("(.*18n/.+)\\.properties") { public Locale getLocale(Matcher matcher) { // no locale for general bundle return null; } }, + /** language scope (no country information) */ LANGUAGE("(.*18n/.+)-(\\w\\w)\\.properties") { @@ -65,21 +67,17 @@ Locale result = null; if (matcher.matches()) { result = I18nLoader.newLocale(matcher.group(2)); - //String language = matcher.group(2).toLowerCase(); - //result = new Locale(language); } return result; } }, + /** full scope : language + country */ FULL("(.*18n/.+)-(\\w\\w_\\w\\w)\\.properties") { public Locale getLocale(Matcher matcher) { Locale result = null; if (matcher.matches()) { result = I18nLoader.newLocale(matcher.group(2)); - //String language = matcher.group(2).toLowerCase(); - //String country = matcher.group(3).toUpperCase(); - //result = new Locale(language, country); } return result; } @@ -91,7 +89,7 @@ /** cache of scope in reverse order */ static I18nBundleScope[] reverseValues; - I18nBundleScope(String patternAll) { + private I18nBundleScope(String patternAll) { this.patternAll = Pattern.compile(patternAll); } @@ -123,7 +121,14 @@ */ public abstract Locale getLocale(Matcher matcher); - + /** + * Obtain the scope of a locale. + *

+ * The given locale can be null, which means {@link I18nBundleScope#GENERAL} scope + * + * @param locale locale used + * @return the scope of given locale + */ public static I18nBundleScope valueOf(Locale locale) { if (locale == null) { return GENERAL; @@ -134,6 +139,7 @@ return FULL; } + /** @return the arrayof scope from more specialized scope to less specialized scope */ public static synchronized I18nBundleScope[] reverseValues() { if (reverseValues == null) { List list = new ArrayList(Arrays.asList(values()));