001/* 
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     <a href="mailto:stan@nuxeo.com">Sun Seng David TAN</a>
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.rendering.fm.i18n;
023
024import java.util.Enumeration;
025import java.util.HashMap;
026import java.util.Locale;
027import java.util.Map;
028import java.util.ResourceBundle;
029
030/**
031 * A resource bundle for Nuxeo Rendering that holds a map of locals, allows developers to change it from its api
032 * (setLocale) and that will delegate its method to the correct resource bundle according to the local chosen.
033 *
034 * @author <a href="mailto:stan@nuxeo.com">Sun Seng David TAN</a>
035 */
036public class ResourceComposite extends ResourceBundle {
037
038    final Map<Locale, ResourceBundle> map = new HashMap<Locale, ResourceBundle>();
039
040    final ClassLoader cl;
041
042    ResourceBundle current;
043
044    public ResourceComposite() {
045        cl = null;
046    }
047
048    public ResourceComposite(ClassLoader cl) {
049        this.cl = cl;
050    }
051
052    /**
053     * Set the locale to be used.
054     *
055     * @param locale
056     */
057    public void setLocale(Locale locale) {
058        current = map.get(locale);
059        if (current == null) {
060            if (cl == null) {
061                current = ResourceBundle.getBundle("messages", locale);
062            } else {
063                current = ResourceBundle.getBundle("messages", locale, cl);
064            }
065            map.put(locale, current);
066        }
067    }
068
069    @Override
070    public Enumeration<String> getKeys() {
071        if (current == null) {
072            setLocale(Locale.getDefault());
073        }
074        return current.getKeys();
075    }
076
077    @Override
078    protected Object handleGetObject(String key) {
079        if (current == null) {
080            setLocale(Locale.getDefault());
081        }
082        return current.getObject(key);
083    }
084
085    /**
086     * Delegates getString using the resource bundle corresponding to the local (create one if it doesn't exist).
087     *
088     * @param key
089     * @param locale
090     * @return
091     */
092    public String getString(String key, Locale locale) {
093        ResourceBundle bundle = map.get(locale);
094        if (bundle == null) {
095            if (cl == null) {
096                bundle = ResourceBundle.getBundle("messages", locale);
097            } else {
098                bundle = ResourceBundle.getBundle("messages", locale, cl);
099            }
100            map.put(locale, bundle);
101        }
102        return bundle.getString(key);
103    }
104
105}