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

org.nuiton.i18n.plugin.bundle.AbstractMakeI18nBundleMojo Maven / Gradle / Ivy

/*
 * #%L
 * I18n :: Maven Plugin
 * 
 * $Id: AbstractMakeI18nBundleMojo.java 1839 2011-01-12 16:41:52Z tchemit $
 * $HeadURL: http://svn.nuiton.org/svn/i18n/tags/i18n-2.3/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractMakeI18nBundleMojo.java $
 * %%
 * Copyright (C) 2007 - 2010 CodeLutin
 * %%
 * 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.bundle;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoFailureException;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.SortedSet;

/**
 * Common mojo to all final bundle maker.
 *
 * @author tchemit 
 * @since 2.0
 */
public abstract class AbstractMakeI18nBundleMojo extends AbstractI18nBundleMojo {

    /**
     * Directory where to generate aggregated bundles.
     *
     * @parameter expression="${i18n.bundleOutputDir}" default-value="${basedir}/target/generated-sources/resources/META-INF"
     * @required
     * @since 1.0.0
     */
    protected File bundleOutputDir;

    /**
     * Name of the bundle to generate.
     *
     * @parameter expression="${i18n.bundleOutputName}" default-value="${project.artifactId}-i18n"
     * @required
     * @since 1.0.2
     */
    protected String bundleOutputName;

    /**
     * A flag to generate a bundle with the first locale defined as a default
     * bundle (with no locale specialization).
     *
     * @parameter expression="${i18n.generateDefaultLocale}" default-value="false"
     * @since 2.1
     */
    protected boolean generateDefaultLocale;

    /**
     * A flag to check that bundles are complete (no missing i18n translations).
     * 

* Note : This behaviour will be activated is {@link #failsIfWarning} is on. * * @parameter expression="${i18n.checkBundle}" default-value="true" * @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" * @since 1.0.0 */ protected boolean showEmpty; /** * A flag to make the build fails if there is some warnings while generating * bundle, says when it misses some translations. *

* Note : This parameter should be used in a release profile to * ensure bundles are complete. * * @parameter expression="${i18n.failsIfWarning}" default-value="false" * @since 2.0 */ protected boolean failsIfWarning; /** to keep all none translated i18n keys by locale. */ protected Map> unsafeMapping; @Override public void init() throws Exception { super.init(); if (failsIfWarning) { // check bundle if wants to fail on unsafe bundles checkBundle = true; unsafeMapping = new HashMap>(); } else { unsafeMapping = null; } createDirectoryIfNecessary(bundleOutputDir); } protected void failsIfWarning() throws MojoFailureException { if (!failsIfWarning) { // no check return; } if (unsafeMapping != null && !unsafeMapping.isEmpty()) { // there is at least one not complete bundle, faisl the build throw new MojoFailureException( "Bundles for locale(s) " + unsafeMapping.keySet() + " are not complete. Use the -Di18n.showEmpty to see " + "missing translations."); } } /** * Gets the bundle file for the given parameters. * * @param root the root directory where bundles are stored * @param artifactId the artifactId (says the prefix of bundle) * @param locale the locale used in bundle ({@code null} means no locale specialized) * @param create a flag to create the file if none existing * @return the bundle file * @throws IOException if any IO problem while creating it (if needed). * @since 2.1 */ protected abstract File getBundleFile(File root, String artifactId, Locale locale, boolean create) throws IOException; /** * Generates the default bundle, says the bundle with no locale specialized. *

* This bundle is a copy of the bundle of the first locale (which in fact * is considered as the main locale). * * @throws IOException if any IO problem while the copy. * @since 2.1 */ protected void generateDefaultBundle() throws IOException { File bundleFirstLocale = getBundleFile(bundleOutputDir, bundleOutputName, locales[0], false ); File bundleWithoutLocale = getBundleFile(bundleOutputDir, bundleOutputName, null, false ); if (!isSilent()) { getLog().info("Generate default bundle at " + bundleWithoutLocale); } FileUtils.copyFile(bundleFirstLocale, bundleWithoutLocale); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy