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.io.File;
019
020import org.apache.avalon.framework.context.Context;
021import org.apache.avalon.framework.context.ContextException;
022import org.apache.cocoon.Constants;
023
024/**
025 * Utils for i18n
026 */
027public class I18nUtils extends org.ametys.core.util.I18nUtils
028{
029    private org.apache.cocoon.environment.Context _cocoonContext;
030
031    @Override
032    public void contextualize(Context context) throws ContextException
033    {
034        super.contextualize(context);
035        _cocoonContext = (org.apache.cocoon.environment.Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
036    }
037    
038    @Override
039    protected void _configure()
040    {
041        super._configure();
042        
043        _addLocation (new File(_cocoonContext.getRealPath("/skins")), "skin");
044        _addLocation (new File(_cocoonContext.getRealPath("/models")), "model");
045    }
046    
047    private void _addLocation (File baseDir, String catalogueName)
048    {
049        if (baseDir.exists())
050        {
051            File[] files = baseDir.listFiles();
052
053            for (int i = 0; i < files.length; i++)
054            {
055                File file = files[i];
056                if (file.isDirectory() && new File(file, "i18n").exists())
057                {
058                    _locations.put(catalogueName + "." + file.getName(), new Location("messages", new String[]{"context://" + baseDir.getName() + "/" + file.getName() + "/i18n"}));
059                }
060            }
061        }
062    }
063}