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<>();
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    public void setLocale(Locale locale) {
056        current = map.get(locale);
057        if (current == null) {
058            if (cl == null) {
059                current = ResourceBundle.getBundle("messages", locale);
060            } else {
061                current = ResourceBundle.getBundle("messages", locale, cl);
062            }
063            map.put(locale, current);
064        }
065    }
066
067    @Override
068    public Enumeration<String> getKeys() {
069        if (current == null) {
070            setLocale(Locale.getDefault());
071        }
072        return current.getKeys();
073    }
074
075    @Override
076    protected Object handleGetObject(String key) {
077        if (current == null) {
078            setLocale(Locale.getDefault());
079        }
080        return current.getObject(key);
081    }
082
083    /**
084     * Delegates getString using the resource bundle corresponding to the local (create one if it doesn't exist).
085     */
086    public String getString(String key, Locale locale) {
087        ResourceBundle bundle = map.get(locale);
088        if (bundle == null) {
089            if (cl == null) {
090                bundle = ResourceBundle.getBundle("messages", locale);
091            } else {
092                bundle = ResourceBundle.getBundle("messages", locale, cl);
093            }
094            map.put(locale, bundle);
095        }
096        return bundle.getString(key);
097    }
098
099}