Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/Gettext.java diff -u maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/Gettext.java:1.1 maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/Gettext.java:1.2 --- maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/Gettext.java:1.1 Tue Oct 23 13:36:12 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/Gettext.java Wed Oct 24 10:01:44 2007 @@ -11,12 +11,13 @@ import org.codehaus.plexus.util.cli.Commandline; import org.codehaus.plexus.util.cli.StreamConsumer; import org.codelutin.util.FileUtil; -import org.codelutin.util.StringUtil; /** * * @author julien + * * @goal gettext + * @phase compile */ public class Gettext extends AbstractI18nPlugin { @@ -25,7 +26,7 @@ * @parameter expression="${i18n.encoding}" default-value="utf-8" */ protected String encoding; - + /** * @description Gettext keywords (see -k in help for details). * @parameter expression="${i18n.keywords}" default-value="-k_ -k_n" @@ -45,44 +46,37 @@ */ protected File srcJava; - /** - * @description Nom du projet. - * @parameter expression="${i18n.artifactId}" default-value="${project.artifactId}" - * @readonly - */ - protected String artifactId; - public void execute() throws MojoExecutionException, MojoFailureException { - log.info("Exec Xgettext commande"); - // Initialisation du répertoire de travail out.mkdirs(); - File xgettextOut = new File(out.getAbsolutePath() + File.separatorChar + artifactId + ".po"); - + File xgettextOut = new File(out.getAbsolutePath() + File.separatorChar + artifactId + ".properties"); + // Récupération des clés Commandline xgettext = new Commandline(); xgettext.setExecutable(xgettextCmd); xgettext.createArgument().setValue("--from-code=" + encoding); xgettext.createArgument().setValue("--output=" + xgettextOut.getAbsolutePath()); xgettext.createArgument().setValue("--language=Java"); +// xgettext.createArgument().setValue("--omit-header"); + xgettext.createArgument().setValue("--msgstr-prefix="); xgettext.createArgument().setLine(keywords); + xgettext.createArgument().setValue("--properties-output"); DirectoryScanner ds = new DirectoryScanner(); ds.setBasedir(srcJava); ds.setIncludes(new String[] {"**/*.java"}); ds.scan(); String[] files = ds.getIncludedFiles(); - + for (int i = 0; i < files.length; i++) { xgettext.createArgument().setValue(getAbsolutePath(files[i])); } - - log.info("Xgettext commande : " + xgettext); + + log.info("Xgettext command : " + xgettext); StreamConsumer outLog = new LoggerStreamConsumer(log, LoggerStreamConsumer.INFO); StreamConsumer errLog = new LoggerStreamConsumer(log, LoggerStreamConsumer.WARN); - - try { + try { CommandLineUtils.executeCommandLine(xgettext, outLog, errLog); } catch (CommandLineException e) { log.error("Could not execute " + xgettextCmd + ".", e); @@ -90,10 +84,8 @@ } // Création des bundles - String[] bundlesSplit = StringUtil.split(bundles, ","); - for (String bundle : bundlesSplit) { - bundle = bundle.trim(); - File bundleOut = new File(out.getAbsolutePath() + File.separatorChar + artifactId + "-" + bundle + ".po"); + for (String bundle : bundles) { + File bundleOut = new File(out.getAbsolutePath() + File.separatorChar + artifactId + "-" + bundle + ".properties"); try { FileUtil.copy(xgettextOut, bundleOut); log.info("Generate bundle " + bundleOut.getAbsolutePath()); Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/AbstractI18nPlugin.java diff -u maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/AbstractI18nPlugin.java:1.1 maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/AbstractI18nPlugin.java:1.2 --- maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/AbstractI18nPlugin.java:1.1 Tue Oct 23 13:36:12 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/AbstractI18nPlugin.java Wed Oct 24 10:01:44 2007 @@ -15,10 +15,10 @@ /** * @description Langues des bundles générés. - * @parameter expression="${i18n.bundles}" default-value="fr, en" + * @parameter expression="${i18n.bundles}" default-value="" * @required */ - protected String bundles; + protected String[] bundles; /** * @description Répertoire sources des fichiers i18n. @@ -35,10 +35,10 @@ protected File out; /** - * @description Force la génération des fichiers properties de langues i18n. - * @parameter expression="${i18n.genProperties}" default-value="false" + * @description Merge les les fichiers properties des dépendances. + * @parameter expression="${i18n.genDependenciess}" default-value="false" */ - protected boolean genProperties; + protected boolean genDependencies; /** * @description Met les fichiers générés dans le répertoire des sources i18n. @@ -47,6 +47,13 @@ protected boolean genSrc; /** + * @description Nom du projet. + * @parameter default-value="${project.artifactId}" + * @readonly + */ + protected String artifactId; + + /** * Log */ protected Log log = getLog(); Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/Generate.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/Generate.java:1.1 --- /dev/null Wed Oct 24 10:01:49 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/Generate.java Wed Oct 24 10:01:44 2007 @@ -0,0 +1,119 @@ +package org.codelutin.i18n.plugin; + + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.channels.FileChannel; +import java.util.Set; +import java.util.jar.JarFile; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.cli.CommandLineException; +import org.codehaus.plexus.util.cli.CommandLineUtils; +import org.codehaus.plexus.util.cli.Commandline; +import org.codehaus.plexus.util.cli.StreamConsumer; +import org.codelutin.util.FileUtil; + +/** + * + * @author julien + * + * @goal gen + * @phase compile + * @execute goal=merge + */ +public class Generate extends AbstractI18nPlugin { + + /** Taille du buffer pour les lectures/écritures */ + private static final int BUFFER_SIZE = 8 * 1024; + + /** + * @description msgcat command. + * @parameter expression="${i18n.msgmergeCmd}" default-value="msgmerge" + */ + protected String msgmergeCmd; + + /** + * @description Dépendance du projet. + * @parameter default-value="${project}" + * @readonly + */ + protected MavenProject project; + + /** + * @description Target java. + * @parameter expression="${i18n.outJava}" default-value="${project.build.outputDirectory}" + * @readonly + */ + protected File outJava; + + public void execute() throws MojoExecutionException, MojoFailureException { + Set artifacts = project.getArtifacts(); + + try { + for (String bundle : bundles) { + File bundleJava = new File(outJava.getAbsolutePath() + File.separatorChar + artifactId + "-" + bundle + ".properties"); + FileChannel bundleJavaChannel = new FileOutputStream(bundleJava).getChannel(); + + File bundleOut = new File(out.getAbsolutePath() + File.separatorChar + artifactId + "-" + bundle + ".properties"); + FileChannel bundleOutChannel = new FileInputStream(bundleOut).getChannel(); + bundleOutChannel.transferTo(0, bundleOutChannel.size(), bundleJavaChannel); + bundleOutChannel.close(); + + if(genDependencies) { + for (Artifact artifact : artifacts) { + File libBundle = extract(artifact.getFile().getAbsolutePath(), artifact.getArtifactId() + "-" + bundle + ".properties"); + + if(libBundle != null) { + FileChannel libBundleChannel = new FileInputStream(libBundle).getChannel(); + libBundleChannel.transferTo(0, libBundleChannel.size(), bundleJavaChannel); + libBundleChannel.close(); + } + } + } + + bundleJavaChannel.close(); + log.info("Generate bundle " + bundleJava.getAbsolutePath()); + } + }catch (Exception e) { + log.error("Generate Error I/O ", e); + throw new MojoFailureException("Generate Error I/O "); + } + } + + private File extract(String zipName, String fileName) { + try { + ZipFile file = new JarFile(zipName); + ZipEntry entry = file.getEntry(fileName); + if(entry != null) { + InputStream in = file.getInputStream(entry); + File target = new File(out, entry.getName()); + + OutputStream out = new BufferedOutputStream(new FileOutputStream(target)); + byte[] buffer = new byte[BUFFER_SIZE]; + int len = 0; + while ((len = in.read(buffer, 0, BUFFER_SIZE)) != -1) { + out.write(buffer, 0, len); + } + out.close(); + in.close(); + + return target; + } + } catch (Exception e) { + log.error(e); + } + return null; + } + +} Index: maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/Merge.java diff -u /dev/null maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/Merge.java:1.1 --- /dev/null Wed Oct 24 10:01:49 2007 +++ maven-i18n-plugin/src/java/org/codelutin/i18n/plugin/Merge.java Wed Oct 24 10:01:44 2007 @@ -0,0 +1,84 @@ +package org.codelutin.i18n.plugin; + +import java.io.File; +import java.io.IOException; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.codehaus.plexus.util.cli.CommandLineException; +import org.codehaus.plexus.util.cli.CommandLineUtils; +import org.codehaus.plexus.util.cli.Commandline; +import org.codehaus.plexus.util.cli.StreamConsumer; +import org.codelutin.util.FileUtil; + +/** + * + * @author julien + * + * @goal merge + * @phase compile + * @execute goal=gettext + */ +public class Merge extends AbstractI18nPlugin { + + /** + * @description msgcat command. + * @parameter expression="${i18n.msgmergeCmd}" default-value="msgmerge" + */ + protected String msgmergeCmd; + + public void execute() throws MojoExecutionException, MojoFailureException { + for (String bundle : bundles) { + File bundleSrc = new File(src.getAbsolutePath() + File.separatorChar + artifactId + "-" + bundle + ".properties"); + File bundleOut = new File(out.getAbsolutePath() + File.separatorChar + artifactId + "-" + bundle + ".properties"); + + if(bundleSrc.exists()) { + Commandline msgmerge = new Commandline(); + msgmerge.setExecutable(msgmergeCmd); +// msgmerge.createArgument().setValue("-q"); + msgmerge.createArgument().setValue("--sort-by-file"); + msgmerge.createArgument().setValue("--add-location"); + msgmerge.createArgument().setValue("--output-file=" + bundleOut.getAbsolutePath()); + msgmerge.createArgument().setValue("--properties-input"); + msgmerge.createArgument().setValue("--properties-output"); + msgmerge.createArgument().setValue(bundleSrc.getAbsolutePath()); + msgmerge.createArgument().setValue(bundleOut.getAbsolutePath()); + + log.info("Msgmerge command : " + msgmerge); + + StreamConsumer outLog = new LoggerStreamConsumer(log, LoggerStreamConsumer.INFO); + StreamConsumer errLog = new LoggerStreamConsumer(log, LoggerStreamConsumer.WARN); + + try { + CommandLineUtils.executeCommandLine(msgmerge, outLog, errLog); + } catch (CommandLineException e) { + log.error("Could not execute " + msgmergeCmd + ".", e); + throw new MojoFailureException("Could not execute " + msgmergeCmd + "."); + } + + if(genSrc) { + try { + FileUtil.copy(bundleSrc, new File(src.getAbsolutePath() + File.separatorChar + artifactId + "-" + bundle + ".properties" + "~")); + FileUtil.copy(bundleOut, bundleSrc); + } catch (IOException e) { + log.error("File Error I/O ", e); + throw new MojoFailureException("File Error I/O "); + } + } + + log.info("Merge bundle " + bundleSrc.getAbsolutePath()); + } else { + if(genSrc) { + try { + bundleSrc.getParentFile().mkdirs(); + FileUtil.copy(bundleOut, bundleSrc); + log.info("Copy bundle " + bundleSrc.getAbsolutePath()); + } catch (IOException e) { + log.error("File Error I/O ", e); + throw new MojoFailureException("File Error I/O "); + } + } + } + } + } +}