Index: lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundle.java diff -u lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundle.java:1.1 lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundle.java:1.2 --- lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundle.java:1.1 Mon Mar 3 13:14:40 2008 +++ lutinutil/src/java/org/codelutin/i18n/bundle/I18nBundle.java Sun Mar 23 06:57:41 2008 @@ -53,16 +53,90 @@ public I18nBundleEntry[] getEntries(Locale locale) { I18nBundleScope scope = I18nBundleScope.valueOf(locale); + List entiresByScopes = getEntries(scope); + if (entiresByScopes.isEmpty()) { + // found no bundle + } + List result = new ArrayList(); for (I18nBundleEntry entry : entries) { I18nBundleScope i18nBundleScope = entry.getScope(); + // load from general to the max scope and always if there is only one bundle entry found if ((i18nBundleScope == scope || i18nBundleScope.ordinal() < scope.ordinal()) && entry.matchLocale(locale, scope)) { result.add(entry); } } + if (result.isEmpty()) { + log.warn("did not find matchin entry for locale "+locale+". Try to detect best entries..."); + // must try to promute and deal with limit cases + switch (scope) { + + case GENERAL: + // try with a higher locale ? + promuteGeneralScope(result); + break; + case LANGUAGE: + // try with a higher locale ? + promuteLanguageScope(locale, result); + break; + case FULL: + promuteFull(locale, result); + break; + } + + } return result.toArray(new I18nBundleEntry[result.size()]); } + private void promuteFull(Locale locale, List result) { + // try with a another FULL matching locale ? + for (I18nBundleEntry entry : entries) { + I18nBundleScope i18nBundleScope = entry.getScope(); + // load from general to the max scope and always if there is only one bundle entry found + if (i18nBundleScope == I18nBundleScope.FULL && + !entry.getLocale().getCountry().equals(locale.getCountry()) && + entry.getLocale().getLanguage().equals(locale.getLanguage())) { + result.add(entry); + // we take the first one, this is a resuce!!! + break; + } + } + } + + private void promuteGeneralScope(List result) { + if (size() == 1) { + // there is one entry take it + result.add(entries.get(0)); + //return; + } + //TODO Should try to load default en_GB from I18nLoader ? + //I18n.DEFAULT_LOCALE.getCountry() + } + + private void promuteLanguageScope(Locale locale, List result) { + for (I18nBundleEntry entry : entries) { + I18nBundleScope i18nBundleScope = entry.getScope(); + // load from general to the max scope and always if there is only one bundle entry found + if (i18nBundleScope == I18nBundleScope.FULL && entry.getLocale().getLanguage().equals(locale.getLanguage())) { + result.add(entry); + // we take the first one, this is a resuce!!! + break; + } + } + } + + public List getEntries(I18nBundleScope scope) { + List result = new ArrayList(); + for (I18nBundleEntry entry : entries) { + I18nBundleScope i18nBundleScope = entry.getScope(); + // load from general to the max scope and always if there is only one bundle entry found + if (i18nBundleScope == scope || i18nBundleScope.ordinal() < scope.ordinal()) { + result.add(entry); + } + } + return result; + } + public int size() { return entries == null ? 0 : entries.size(); }