001/*
002 *  Copyright 2015 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.web.cocoon;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.web.skin.Skin;
025import org.ametys.web.skin.SkinModelsManager;
026import org.ametys.web.skin.SkinsManager;
027
028/**
029 * Utils for i18n
030 */
031public class I18nUtils extends org.ametys.core.util.I18nUtils
032{
033    private SkinsManager _skinsManager;
034    private SkinModelsManager _skinModelsManager;
035
036    @Override
037    public void service(ServiceManager manager) throws ServiceException
038    {
039        super.service(manager);
040        _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
041        _skinModelsManager = (SkinModelsManager) manager.lookup(SkinModelsManager.ROLE);
042    }
043    
044    @Override
045    protected void _configure()
046    {
047        super._configure();
048        
049        _addSkinsLocation();
050        _addModelsLocation();
051    }
052    
053    private void _addSkinsLocation ()
054    {
055        for (String skinId : _skinsManager.getSkins())
056        {
057            List<String> locations = new ArrayList<>();
058            
059            Skin skin = _skinsManager.getSkin(skinId);
060            for (Skin skinOrParentSkin : _skinsManager.getSkinAndParents(skin))
061            {
062                locations.add("skin-raw:" + skinOrParentSkin.getId() + "://i18n");
063            }
064            
065            _locations.put("skin." + skinId, new Location("messages", locations.toArray(new String[locations.size()])));
066        }
067    }
068    
069    private void _addModelsLocation()
070    {
071        for (String modelId : _skinModelsManager.getModels())
072        {
073            _locations.put("model." + modelId, new Location("messages", new String[]{"model:" + modelId + "://i18n"}));
074        }
075    }
076}