All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.i18n.plugin.GenerateMojo Maven / Gradle / Ivy

/*
 * #%L
 * I18n :: Maven Plugin
 * 
 * $Id: GenerateMojo.java 1843 2011-01-19 20:29:56Z tchemit $
 * $HeadURL: http://svn.nuiton.org/svn/i18n/tags/i18n-2.3.1/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/GenerateMojo.java $
 * %%
 * Copyright (C) 2007 - 2010 CodeLutin, Tony Chemit
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */

package org.nuiton.i18n.plugin;

import org.nuiton.io.SortedProperties;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Locale;

/**
 * Merge new generated i18n bundles with older existing ones.
 *
 * @author jruchaud 
 * @author chemit 
 * @goal gen
 * @phase generate-resources
 * @execute goal=get
 */
public class GenerateMojo extends AbstractI18nMojo {

    /**
     * A flag to check that bundles are complete (no missing i18n translations).
     *
     * @parameter expression="${i18n.checkBundle}" default-value="true"
     * @required
     * @since 1.0.0
     */
    protected boolean checkBundle;

    /**
     * A flag to show missing i18n translation.
     * 

* Note : Need the {@link #checkBundle} to be activated). * * @parameter expression="${i18n.showEmpty}" default-value="false" * @required * @since 1.0.0 */ protected boolean showEmpty; /** * To keep a backup of old i18n bundles (suffiex by a {@code ~}). *

* Note: By default, this property is not active. * * @parameter expression="${i18n.keepBackup}" default-value="false" */ protected boolean keepBackup; /** * To keep generated getter files. *

* Note: By default, this property is not active. * * @parameter expression="${i18n.keepGetters}" default-value="false" */ protected boolean keepGetters; @Override protected boolean checkSkip() { if (!needGeneration()) { getLog().info("No getter detected - all files are up to date."); return false; } return true; } @Override protected void doAction() throws Exception { if (!silent) { getLog().info("config - src basedir : " + src.getAbsolutePath()); getLog().info("config - out basedir : " + out.getAbsolutePath()); getLog().info("config - locales : " + Arrays.toString(locales)); } for (Locale locale : locales) { if (!silent) { getLog().info("prepare bundle for locale " + locale); } // Merge File bundleSrc = getI18nFile(src, artifactId, locale, false); File bundleOut = getI18nFile(out, artifactId, locale, false); File bundleGetterOut = getI18nFile( out, artifactId + GetterMojo.FROM_GETTERS, locale, false); SortedProperties propertiesSrc = new SortedProperties(encoding); if (bundleSrc.exists()) { propertiesSrc.load(bundleSrc); } SortedProperties propertiesOut = new SortedProperties(encoding); if (!strictMode) { // si on n'est pas en mode strict, on doit push back in // bundle out, all the bundle src keys propertiesOut.putAll(propertiesSrc); } propertiesOut.load(bundleGetterOut); // Parcours des clés for (Object key : propertiesOut.keySet()) { Object oldKey = propertiesOut.get(key); Object value = propertiesSrc.get(oldKey); // Récupération de la clé si elle a été renommée if (!key.equals(oldKey) && value == null) { value = propertiesSrc.get(key); } if (value != null) { propertiesOut.put(key, value); } else { propertiesOut.put(key, ""); } } //fixme : on devrait laisser le fichier en utf8 ? FileOutputStream outStream = new FileOutputStream(bundleOut); try { propertiesOut.store(outStream); } finally { outStream.close(); } // Sauvegarde avant copie if (genSrc && keepBackup) { backupFile(bundleSrc); } if (!silent) { getLog().info("merge bundle " + locale + " to out"); } if (checkBundle) { checkBundle(locale, propertiesOut, showEmpty, null); } if (genSrc) { // Copie des fichiers dans les sources copyFile(bundleOut, bundleSrc); if (!silent) { getLog().info("copy bundle " + locale + " to src"); } } if (!keepGetters) { if (isVerbose()) { getLog().info("Will delete getter " + bundleGetterOut); } deleteFile(bundleGetterOut); } } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy