Author: tchemit Date: 2011-01-12 17:41:52 +0100 (Wed, 12 Jan 2011) New Revision: 1839 Url: http://nuiton.org/repositories/revision/i18n/1839 Log: Evolution #1203: Add generateDefaultLocale parameter in Bundle like mojos Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/AbstractI18nMojo.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractMakeI18nBundleMojo.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/BundleMojo.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/TapestryBundleMojo.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJspMojo.java trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserTapestryMojo.java Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/AbstractI18nMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/AbstractI18nMojo.java 2011-01-04 13:54:04 UTC (rev 1838) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/AbstractI18nMojo.java 2011-01-12 16:41:52 UTC (rev 1839) @@ -199,16 +199,21 @@ /** * @param root le repertoire ou sont stockes les fichiers i18n * @param artifactId le nom de l'artifact - * @param locale le nom du bundle + * @param locale le nom de la locale (peut-être nulle) * @param create {@code true} pour creer le fichier si non present * @return le fichier i18n * @throws IOException si probleme lors de la creation du fichier */ - public File getI18nFile(File root, String artifactId, - Locale locale, boolean create) throws IOException { - File file = new File( - root.getAbsolutePath() + File.separatorChar + artifactId + - "_" + locale.toString() + ".properties"); + public File getI18nFile(File root, + String artifactId, + Locale locale, + boolean create) throws IOException { + String path = root.getAbsolutePath() + File.separatorChar + artifactId; + if (locale != null) { + path += "_" + locale.toString(); + } + path += ".properties"; + File file = new File(path); if (create && !file.exists()) { createNewFile(file); } Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractMakeI18nBundleMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractMakeI18nBundleMojo.java 2011-01-04 13:54:04 UTC (rev 1838) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractMakeI18nBundleMojo.java 2011-01-12 16:41:52 UTC (rev 1839) @@ -24,9 +24,11 @@ */ 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; @@ -59,6 +61,15 @@ 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). * <p/> * <b>Note :</b> This behaviour will be activated is {@link #failsIfWarning} is on. @@ -127,4 +138,50 @@ } } + /** + * 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. + * <p/> + * 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); + } + } Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/BundleMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/BundleMojo.java 2011-01-04 13:54:04 UTC (rev 1838) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/BundleMojo.java 2011-01-12 16:41:52 UTC (rev 1839) @@ -25,6 +25,8 @@ package org.nuiton.i18n.plugin.bundle; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.nuiton.i18n.bundle.I18nBundleEntry; import org.nuiton.i18n.bundle.I18nBundleUtil; import org.nuiton.i18n.init.DefaultI18nInitializer; @@ -195,6 +197,10 @@ failsIfWarning(); + if (generateDefaultLocale) { + generateDefaultBundle(); + } + if (generateDefinitionFile) { generateDefinitionFile(version, bundleDico); @@ -205,6 +211,14 @@ } } + @Override + protected File getBundleFile(File root, + String artifactId, + Locale locale, + boolean create) throws IOException { + return getI18nFile(root, artifactId, locale, create); + } + protected void generateDefinitionFile(String version, Map<Locale, String> bundleDico) throws IOException { Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/TapestryBundleMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/TapestryBundleMojo.java 2011-01-04 13:54:04 UTC (rev 1838) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/TapestryBundleMojo.java 2011-01-12 16:41:52 UTC (rev 1839) @@ -72,7 +72,7 @@ long t0 = System.nanoTime(); - File bundleOut = getTapestryI18nFile( + File bundleOut = getBundleFile( bundleOutputDir, bundleOutputName, locale, @@ -131,13 +131,16 @@ failsIfWarning(); + if (generateDefaultLocale) { + generateDefaultBundle(); + } + if (!silent && verbose) { getLog().info("done in " + PluginHelper.convertTime(t00, System.nanoTime())); } } - /** * @param root le repertoire ou sont stockes les fichiers i18n * @param artifactId le nom de l'artifact @@ -146,13 +149,18 @@ * @return le fichier i18n * @throws IOException si probleme lors de la creation du fichier */ - public File getTapestryI18nFile(File root, - String artifactId, - Locale locale, - boolean create) throws IOException { + @Override + protected File getBundleFile(File root, + String artifactId, + Locale locale, + boolean create) throws IOException { + String path = root.getAbsolutePath() + File.separatorChar + artifactId; + if (locale != null) { + path += "_" + locale.getLanguage(); + } + path += ".properties"; File file = new File( - root.getAbsolutePath() + File.separatorChar + artifactId + - "_" + locale.getLanguage() + ".properties"); + path); if (create && !file.exists()) { createNewFile(file); } Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJspMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJspMojo.java 2011-01-04 13:54:04 UTC (rev 1838) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserJspMojo.java 2011-01-12 16:41:52 UTC (rev 1839) @@ -282,67 +282,5 @@ return footer; } } - - // /** - // * To remove jsp comment from files. - // * - // * @author tchemit <chemit@codelutin.com> - // * @since 2.0 - // */ - // public static class JspCommentFilter extends DefaultFilter { - // - // private String header = "<" + "%" + "-" + "-"; - // - // private String footer = "-" + "-" + "%" + ">"; - // - // @Override - // protected String performInFilter(String ch) { - // return EMPTY_STRING; - // } - // - // @Override - // protected String performOutFilter(String ch) { - // return ch; - // } - // - // protected String getHeader() { - // return header; - // } - // - // protected String getFooter() { - // return footer; - // } - // } - // - // /** - // * To remove jsp directive from files. - // * - // * @author tchemit <chemit@codelutin.com> - // * @since 2.0 - // */ - // public static class JspDirectiveFilter extends DefaultFilter { - // - // private String header = "<" + "%"; - // - // private String footer = "%" + ">"; - // - // @Override - // protected String performInFilter(String ch) { - // return EMPTY_STRING; - // } - // - // @Override - // protected String performOutFilter(String ch) { - // return ch; - // } - // - // protected String getHeader() { - // return header; - // } - // - // protected String getFooter() { - // return footer; - // } - // } } } Modified: trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserTapestryMojo.java =================================================================== --- trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserTapestryMojo.java 2011-01-04 13:54:04 UTC (rev 1838) +++ trunk/maven-i18n-plugin/src/main/java/org/nuiton/i18n/plugin/parser/impl/ParserTapestryMojo.java 2011-01-12 16:41:52 UTC (rev 1839) @@ -294,6 +294,5 @@ extractKeys(extractor2, fileTemp, encoding, verbose, keys); saveKeysToFile(fileout, keys); } - } }