Author: tchemit Date: 2012-08-29 23:25:39 +0200 (Wed, 29 Aug 2012) New Revision: 1977 Url: http://nuiton.org/repositories/revision/i18n/1977 Log: fixes #2291: Generate a csv file with all translations for all locales in a csv file in bundle mojo Modified: trunk/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractMakeI18nBundleMojo.java trunk/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/BundleMojo.java trunk/src/site/apt/index.apt trunk/src/site/en/apt/index.apt Modified: trunk/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractMakeI18nBundleMojo.java =================================================================== --- trunk/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractMakeI18nBundleMojo.java 2012-08-29 20:33:23 UTC (rev 1976) +++ trunk/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/AbstractMakeI18nBundleMojo.java 2012-08-29 21:25:39 UTC (rev 1977) @@ -147,7 +147,7 @@ /** * The definitive directory where to generate the bundles (includes the - * package of bunlde). + * package of bundle). * * @since 2.3.2 */ Modified: trunk/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/BundleMojo.java =================================================================== --- trunk/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/BundleMojo.java 2012-08-29 20:33:23 UTC (rev 1976) +++ trunk/i18n-maven-plugin/src/main/java/org/nuiton/i18n/plugin/bundle/BundleMojo.java 2012-08-29 21:25:39 UTC (rev 1977) @@ -25,6 +25,7 @@ package org.nuiton.i18n.plugin.bundle; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Component; @@ -39,18 +40,23 @@ import org.nuiton.io.SortedProperties; import org.nuiton.plugin.PluginHelper; +import java.io.BufferedWriter; import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.net.URL; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; +import java.util.Set; /** * Generate an aggregate i18n bundle for all dependencies of the project. @@ -88,6 +94,36 @@ protected boolean generateDefinitionFile; /** + * A flag to generate the i18n csv file wich contains all translation for + * all locales. + * <p/> + * Useful to translate from a language to another one. + * + * @since 2.5 + */ + @Parameter(property = "i18n.generateCsvFile", defaultValue = "false") + protected boolean generateCsvFile; + + /** + * Location of the csv file to generate if parameter + * {@link #generateCsvFile} is on. + * + * @since 2.5 + */ + @Parameter(property = "i18n.bundleCsvFile", + defaultValue = "${basedir}/target/${project.artifactId}-i18n.csv") + protected File bundleCsvFile; + + /** + * Char separator used when generating the csf bundle file if parameter + * {@link #generateCsvFile} is on. + * + * @since 2.5 + */ + @Parameter(property = "i18n.bundleCsvSeparator", defaultValue = "\t") + protected String bundleCsvSeparator; + + /** * Converter used to change format of bundles. * * @since 2.4 @@ -242,6 +278,11 @@ generateDefinitionFile(version, bundleDico); } + + if (generateCsvFile) { + generateCsvFile(); + } + if (!silent && verbose) { getLog().info("done in " + PluginHelper.convertTime(t00, System.nanoTime())); @@ -304,4 +345,69 @@ } } + /** + * Generates a csv file with all translations. + * <p/> + * First column is key, second is first locale translation, ... + * + * @throws IOException if any IO problem while the copy. + * @since 2.5 + */ + protected void generateCsvFile() throws IOException { + + if (!bundleCsvFile.exists()) { + createNewFile(bundleCsvFile); + } + + Set<String> allKeys = new HashSet<String>(); + Map<Locale, SortedProperties> bundlesByLocale = + new LinkedHashMap<Locale, SortedProperties>(); + for (Locale locale : locales) { + File bundleFile = getBundleFile(outputFolder, + bundleOutputName, + locale, + false + ); + + SortedProperties properties = new SortedProperties(bundleOutputEncoding); + properties.load(bundleFile); + for (Object o : properties.keySet()) { + allKeys.add((String) o); + } + bundlesByLocale.put(locale, properties); + } + List<String> keys = new ArrayList<String>(allKeys); + Collections.sort(keys); + + BufferedWriter writer = new BufferedWriter(new FileWriter(bundleCsvFile)); + try { + // add header line + StringBuilder builder = new StringBuilder("Key"); + for (Locale locale : locales) { + builder.append(bundleCsvSeparator).append(locale.getCountry()); + } + writer.write(builder.toString()); + + // for each key get all translations for all + for (String key : keys) { + writer.newLine(); + builder = new StringBuilder(key); + for (Locale locale : locales) { + SortedProperties properties = bundlesByLocale.get(locale); + Object value = properties.get(key); + builder.append(bundleCsvSeparator); + builder.append(value == null ? "" : value); + } + writer.write(builder.toString()); + } + writer.close(); + } finally { + IOUtils.closeQuietly(writer); + } + + if (!isSilent()) { + getLog().info("Generate csv bundle file at " + bundleCsvFile); + } + } + } Modified: trunk/src/site/apt/index.apt =================================================================== --- trunk/src/site/apt/index.apt 2012-08-29 20:33:23 UTC (rev 1976) +++ trunk/src/site/apt/index.apt 2012-08-29 21:25:39 UTC (rev 1977) @@ -77,6 +77,16 @@ Cela permet de limiter les clefs entrantes (ce qui est nécessaire pour le parseur struts2...) +* Permettre de générer un fichier csv avec toutes les traductions d'un bundle + + Cela peut aider grandement à compléter les traduction à partir des + traductions complètes d'une langue. + + Pour utiliser cette fonctionnalité, il suffit d'ajouter à la ligne de + commande <-Di18n.generateCsvFile> lors de l'execution du mojo <bundle>. + + See {{{http://nuiton.org/issues/2291}Evolution #2291: Generate a csv file with all translations for all locales in a csv file in bundle mojo}}. + Quoi de neuf dans la version 2.4 * Meilleure utilisation des encodings Modified: trunk/src/site/en/apt/index.apt =================================================================== --- trunk/src/site/en/apt/index.apt 2012-08-29 20:33:23 UTC (rev 1976) +++ trunk/src/site/en/apt/index.apt 2012-08-29 21:25:39 UTC (rev 1977) @@ -75,6 +75,14 @@ This permits to limit incoming keys by a regex. +* Can generate a csv file from the generated bundle for all locales. + + This can help people to translate missing keys. + + To use this just add *-Di18n.generateCsvFile* on the bundle mojo. + + See {{{http://nuiton.org/issues/2291}Evolution #2291: Generate a csv file with all translations for all locales in a csv file in bundle mojo}}. + What's new in 2.4 version * Better usage of encodings @@ -109,14 +117,6 @@ another one, using a different syntax from the one used by translations will not work properly. -What's new in 2.5 version - -* Plugin module renamed to i18n-maven-plugin. - - the i18n maven plugin was renamed from <maven-i18n-plugin> to <i18n-maven-plugin>. - - Think to update your poms. - What's new in 2.3 version Version 2.3 improves a lot performance on detection of java file i18n keys