001: /*
002: * Copyright 2007 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.i18n.impl;
017:
018: import org.outerj.daisy.i18n.DResourceBundles;
019: import org.outerj.daisy.i18n.DResourceBundle;
020: import org.outerj.daisy.i18n.I18nMessage;
021:
022: import java.util.*;
023:
024: /**
025: * Manages a set of resource bundles with fallback between languages and different bundles.
026: */
027: public class AggregateResourceBundle implements DResourceBundles {
028: private String orderedBundleNames[];
029: private Map<String, Map<String, DResourceBundle>> bundlesByName = new HashMap<String, Map<String, DResourceBundle>>();
030:
031: /**
032: * @param names the names of the resource bundles, in the order in which they should be searched
033: */
034: public AggregateResourceBundle(String[] names) {
035: if (names == null)
036: throw new IllegalArgumentException("Null argument: names");
037:
038: this .orderedBundleNames = names;
039: }
040:
041: public void addBundle(String name, Locale locale,
042: DResourceBundle bundle) {
043: Map<String, DResourceBundle> bundlesByLocale = bundlesByName
044: .get(name);
045: if (bundlesByLocale == null) {
046: bundlesByLocale = new HashMap<String, DResourceBundle>();
047: bundlesByName.put(name, bundlesByLocale);
048: }
049:
050: bundlesByLocale.put(getString(locale), bundle);
051: }
052:
053: public static String getString(Locale locale) {
054: boolean hasLanguage = !locale.getLanguage().equals("");
055: boolean hasCountry = !locale.getCountry().equals("");
056: boolean hasVariant = !locale.getVariant().equals("");
057:
058: if (hasLanguage && hasCountry && hasVariant)
059: return locale.getLanguage() + '-' + locale.getCountry()
060: + '-' + locale.getVariant();
061: else if (hasLanguage && hasCountry)
062: return locale.getLanguage() + '-' + locale.getCountry();
063: else if (hasLanguage)
064: return locale.getLanguage();
065: else
066: return "";
067: }
068:
069: public I18nMessage get(Locale locale, String key) {
070: for (String name : orderedBundleNames) {
071: Map<String, DResourceBundle> bundlesByLocale = bundlesByName
072: .get(name);
073: if (bundlesByLocale == null)
074: continue;
075:
076: String[] localeNames = getSearchLocaleNames(locale);
077: for (String localeName : localeNames) {
078: DResourceBundle bundle = bundlesByLocale
079: .get(localeName);
080: if (bundle != null) {
081: I18nMessage message = bundle.get(key);
082: if (message != null)
083: return message;
084: }
085: }
086: }
087:
088: return null;
089: }
090:
091: private String[] getSearchLocaleNames(Locale locale) {
092: boolean hasLanguage = !locale.getLanguage().equals("");
093: boolean hasCountry = !locale.getCountry().equals("");
094: boolean hasVariant = !locale.getVariant().equals("");
095:
096: List<String> localeNames = new ArrayList<String>(4);
097: if (hasLanguage && hasCountry && hasVariant)
098: localeNames.add(locale.getLanguage() + '-'
099: + locale.getCountry() + '-' + locale.getVariant());
100:
101: if (hasLanguage && hasCountry)
102: localeNames.add(locale.getLanguage() + '-'
103: + locale.getCountry());
104:
105: if (hasLanguage)
106: localeNames.add(locale.getLanguage());
107:
108: localeNames.add("");
109:
110: return localeNames.toArray(new String[0]);
111: }
112: }
|