Package org.internna.iwebmvc.spring.i18n

Source Code of org.internna.iwebmvc.spring.i18n.I18nDataLoader

/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.spring.i18n;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.internna.iwebmvc.core.context.ContextHolder;
import org.internna.iwebmvc.core.dao.DAO;
import org.internna.iwebmvc.model.core.Locale;
import org.internna.iwebmvc.utils.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.stereotype.Component;

/**
* Retrieves all i18n data from database and creates properties files.
*
* @author Jose Noheda
* @since 1.0
*
*/
@Component("i18nDataLoader")
public class I18nDataLoader implements InitializingBean {

    private final String query = "SELECT lo.locale, i.key, l.localizedValue FROM LocalizedValue l JOIN l.locale as lo JOIN l.i18nEntry as i";

    @Autowired @Qualifier("baseDAO") protected DAO dao;
    @Autowired protected ContextHolder context;
    @Autowired protected ReloadableResourceBundleMessageSource source;

    public void afterPropertiesSet() throws Exception {
        source.setResourceLoader(new FileSystemResourceLoader());
        generate();
    }

    @SuppressWarnings("unchecked")
    public synchronized void generate() throws IOException {
        List<Object[]> values = (List<Object[]>) dao.executeQuery(query);
        List<Locale> locales = context.getAvailableLocales();
        Map<Object, Properties> data = new HashMap<Object, Properties>(locales.size());
        for (int i = 0; i < locales.size(); i++) data.put(locales.get(i).getLocale(), new Properties());
        for (Object[] value : values) data.get(value[0]).setProperty(value[1].toString(), value[2].toString());
        File dataFile = null;
        for (int i = 0; i < locales.size(); i++) {
            if (i == 0) {
                dataFile = File.createTempFile("i18n", ".properties");
                data.get(locales.get(i).getLocale()).store(new FileOutputStream(dataFile), "");
                String basePath = StringUtils.stripFilenameExtension(dataFile.getAbsolutePath());
                source.setBasename(basePath);
                basePath += "_" + locales.get(i).getLocale() + ".properties";
                dataFile = new File(basePath);
            } else {
                String oldSuffix = locales.get(i - 1).getLocale();
                dataFile = new File(dataFile.getParentFile(), dataFile.getName().replace(oldSuffix, locales.get(i).getLocale()));
            }
            data.get(locales.get(i).getLocale()).store(new FileOutputStream(dataFile), "");
        }
        source.clearCache();
    }

}
TOP

Related Classes of org.internna.iwebmvc.spring.i18n.I18nDataLoader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.