Index: lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleScope.java diff -u lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleScope.java:1.1 lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleScope.java:1.2 --- lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleScope.java:1.1 Mon Mar 3 13:14:40 2008 +++ lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundleScope.java Sat Mar 22 23:29:24 2008 @@ -17,10 +17,13 @@ * ##% */ package org.codelutin.i18n.bundle; -import java.util.Locale; +import org.codelutin.i18n.I18nLoader; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.List; +import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -48,48 +51,64 @@ * @author chemit */ public enum I18nBundleScope { - /** none */ + /** default scope (with no clanguage and country information) */ GENERAL("(.*18n/.+)\\.properties") { public Locale getLocale(Matcher matcher) { // no locale for general bundle return null; } }, - /** language */ + /** language scope (no country information) */ LANGUAGE("(.*18n/.+)-(\\w\\w)\\.properties") { public Locale getLocale(Matcher matcher) { Locale result = null; if (matcher.matches()) { - String language = matcher.group(2).toLowerCase(); - result = new Locale(language); + result = I18nLoader.newLocale(matcher.group(2)); + //String language = matcher.group(2).toLowerCase(); + //result = new Locale(language); } return result; } }, - /** language + country */ - FULL("(.*18n/.+)-(\\w\\w)_(\\w\\w)\\.properties") { + /** full scope : language + country */ + FULL("(.*18n/.+)-(\\w\\w_\\w\\w)\\.properties") { public Locale getLocale(Matcher matcher) { Locale result = null; if (matcher.matches()) { - String language = matcher.group(2).toLowerCase(); - String country = matcher.group(3).toUpperCase(); - result = new Locale(language, country); + result = I18nLoader.newLocale(matcher.group(2) + "_" + matcher.group(3)); + //String language = matcher.group(2).toLowerCase(); + //String country = matcher.group(3).toUpperCase(); + //result = new Locale(language, country); } return result; } }; - private Pattern patternAll; + /** pattern to use for a scope to detect bundle entry */ + private final Pattern patternAll; + + /** cache of scope in reverse order */ + static I18nBundleScope[] reverseValues; I18nBundleScope(String patternAll) { this.patternAll = Pattern.compile(patternAll); } + /** + * get a matcher fro the given path for this scope + * + * @param path the path to treate + * @return the bunle detect matcher + */ public Matcher getMatcher(String path) { return patternAll.matcher(path); } + /** + * @param matcher the scope matcher to use + * @return the prefix of the bundle + */ public String getBundlePrefix(Matcher matcher) { String result = null; if (matcher.matches()) { @@ -98,8 +117,13 @@ return result; } + /** + * @param matcher the scope matcher to use + * @return the locale + */ public abstract Locale getLocale(Matcher matcher); + public static I18nBundleScope valueOf(Locale locale) { if (locale == null) { return GENERAL; @@ -110,16 +134,12 @@ return FULL; } - - static I18nBundleScope[] reverseValues; - - public static I18nBundleScope[] reverseValues() { - if (reverseValues==null) { - I18nBundleScope[] result = values(); - ArrayList list = new ArrayList(Arrays.asList(values())); + public static synchronized I18nBundleScope[] reverseValues() { + if (reverseValues == null) { + List list = new ArrayList(Arrays.asList(values())); Collections.sort(list); Collections.reverse(list); - reverseValues =list.toArray(new I18nBundleScope[list.size()]); + reverseValues = list.toArray(new I18nBundleScope[list.size()]); } return reverseValues; }