001/*
002 *  Copyright 2023 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 */
016package org.ametys.plugins.workflow.component;
017
018import java.util.Locale;
019import java.util.Map;
020import java.util.Set;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.context.Context;
024import org.apache.avalon.framework.context.ContextException;
025import org.apache.avalon.framework.context.Contextualizable;
026import org.apache.cocoon.components.ContextHelper;
027
028/**
029 * Component for managing languages
030 */
031public class WorkflowLanguageManager implements Component, Contextualizable
032{
033    /** The Workflow Language Manager ROLE */
034    public static final String ROLE = WorkflowLanguageManager.class.getName();
035    
036    /** The context */
037    protected Context _context;
038    
039    public void contextualize(Context context) throws ContextException
040    {
041        _context = context;
042    }
043    
044    /** 
045     * Get currently used language 
046     * @return the language code
047     */
048    public String getCurrentLanguage()
049    {
050        Map objectModel = ContextHelper.getObjectModel(_context);
051        Locale locale = org.apache.cocoon.i18n.I18nUtils.findLocale(objectModel, "locale", null, Locale.getDefault(), true);
052        return locale.getLanguage();
053    }
054    
055    /**
056     * Get a set of the authorized languages for current application
057     * Must be overriden by CMS to access it
058     * @return a set of the languages codes
059     */
060    public Set<String> getLanguages()
061    {
062        Map objectModel = ContextHelper.getObjectModel(_context);
063        Locale locale = org.apache.cocoon.i18n.I18nUtils.findLocale(objectModel, "locale", null, Locale.getDefault(), true);
064        return Set.of(locale.getLanguage());
065    }
066    
067}