001/*
002 *  Copyright 2012 Anyware Services
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
017package org.ametys.cms.languages;
018
019import java.util.LinkedHashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.configuration.Configurable;
024import org.apache.avalon.framework.configuration.Configuration;
025import org.apache.avalon.framework.configuration.ConfigurationException;
026
027import org.ametys.runtime.i18n.I18nizableText;
028
029/**
030 * Handle possible languages
031 */
032public class LanguagesManager implements Configurable, Component
033{
034    /** The component role */
035    public static final String ROLE = LanguagesManager.class.getName();
036    
037    private Map<String, Language> _languages;
038
039    @Override
040    public void configure(Configuration configuration) throws ConfigurationException
041    {
042        _languages = new LinkedHashMap<>();
043        
044        for (Configuration lConf : configuration.getChildren("language"))
045        {
046            String code = lConf.getAttribute("code");
047            Language language;
048            
049            if (lConf.getAttributeAsBoolean("i18n", true))
050            {
051                language = new Language(code, new I18nizableText("application", lConf.getValue(code)));
052            }
053            else
054            {
055                language = new Language(code, new I18nizableText(lConf.getValue(code)));
056            }
057            _languages.put(code, language);
058        }
059    }
060    
061    /**
062     * Get the language by its code
063     * @param code The language code
064     * @return The language or <code>null</code> if not found
065     */
066    public Language getLanguage (String code)
067    {
068        return _languages.get(code);
069    }
070    
071    /**
072     * Get the list of possible languages
073     * @return the association language-code Language object
074     */
075    public Map<String, Language> getAvailableLanguages()
076    {
077        return _languages;
078    }
079}