2 package org.bull.novaforge.tools.tab2poConverter;
\r
4 import java.io.BufferedReader;
\r
6 import java.io.FileInputStream;
\r
7 import java.io.FileOutputStream;
\r
8 import java.io.FilenameFilter;
\r
9 import java.io.IOException;
\r
10 import java.io.StringReader;
\r
11 import java.util.ArrayList;
\r
12 import java.util.HashMap;
\r
13 import java.util.List;
\r
14 import java.util.Map;
\r
15 import java.util.regex.Matcher;
\r
16 import java.util.regex.Pattern;
\r
18 import org.apache.commons.io.IOUtils;
\r
20 // TODO Les Ajouts de messages manquants doivent être réalisé également dans le POT
\r
21 // TODO Prise en compte des messages en doublons
\r
25 * <li>Suppression de la fonctionnalité d'ajout des messages non trouvés de le PO. Méthode à
\r
26 * préférer : ajout dans un fichier PHP dédié</li>
\r
29 public class FileConverter implements Runnable
\r
31 private static final Map<String, String> FILENAME_MAPPING = new HashMap<String, String>();
\r
33 private static final FilenameFilter FILTER_FILE_TAB = new FilenameFilter()
\r
35 public boolean accept(final File root, final String name)
\r
37 return name.endsWith(".tab");
\r
43 FILENAME_MAPPING.put("Base", "en");
\r
44 FILENAME_MAPPING.put("French", "fr");
\r
45 FILENAME_MAPPING.put("Spanich", "es");
\r
48 private final boolean debug = false;
\r
50 private final File languageDir;
\r
52 private final File translationDir;
\r
54 private final String resultExtension;
\r
56 public FileConverter(final File languageDirPath,
\r
57 final File translationDirPath,
\r
58 final String resultExtension)
\r
60 if (languageDirPath == null || !languageDirPath.isDirectory())
\r
62 throw new IllegalArgumentException("languageDir (" + languageDirPath
\r
63 + ") is not a directory");
\r
65 this.languageDir = languageDirPath;
\r
66 this.translationDir = translationDirPath;
\r
67 this.resultExtension = resultExtension;
\r
72 System.out.println("---------------------------------------------");
\r
73 System.out.println("TAB translation file migration");
\r
76 System.out.println("languageDir=" + this.languageDir);
\r
77 System.out.println("translationDir=" + this.translationDir);
\r
78 System.out.println("resultExtension=" + this.resultExtension);
\r
79 System.out.println("---------------------------------------------");
\r
82 // récupération des fichiers de traduction (*.tab)
\r
83 for (final File tabFile : this.languageDir.listFiles(FILTER_FILE_TAB))
\r
85 System.out.println("\n> Processing file " + tabFile.getName());
\r
86 // . récupérer le fichier *.po correspondant à la langue du fichier tab
\r
87 final String poFilename = FILENAME_MAPPING.get(tabFile.getName().replaceFirst("\\.tab$",
\r
89 if (poFilename == null)
\r
91 throw new RuntimeException("No filename mapping found for " + poFilename);
\r
94 final File poFile = new File(this.translationDir, poFilename + ".po");
\r
95 final File outputFile = new File(this.translationDir, poFilename + this.resultExtension);
\r
96 this.process(tabFile, poFile, outputFile);
\r
99 System.out.println("\n\nMigration finished\n---------------------------------------------");
\r
102 protected void process(final File tabFile, final File poFile, final File outputFile)
\r
104 if (!poFile.exists())
\r
106 System.out.print("\n\t-> PO File (" + poFile + ") do not exist");
\r
109 final String poContent;
\r
112 poContent = IOUtils.toString(new FileInputStream(poFile));
\r
114 catch (final Exception e)
\r
116 throw new RuntimeException("Problem when reading file " + poFile, e);
\r
118 final String tabFileContent;
\r
121 tabFileContent = IOUtils.toString(new FileInputStream(tabFile));
\r
123 catch (final Exception e)
\r
125 throw new RuntimeException("Problem when reading file " + tabFile, e);
\r
127 final String result = this.getReplacedText(tabFileContent, poContent);
\r
130 IOUtils.write(result, new FileOutputStream(outputFile));
\r
132 catch (final Exception e)
\r
134 throw new RuntimeException("Problem when writing file " + outputFile, e);
\r
136 System.out.println("\n-> Update file " + outputFile.getName());
\r
139 protected static final Pattern PATTERN_TAB_LINE = Pattern.compile(
\r
140 "(^|[\\n\\r])([^#\\n\\r\\s]+)[ \\t]+([^\\n\\r\\s]+)([ \\t]+[^\\n\\r]+)?($|[\\n\\r])",
\r
143 protected String getReplacedText(final String tabFileContent, final String poFileContent)
\r
145 String result = poFileContent;
\r
147 for (final I18nMessage message : this.extractMessage(tabFileContent))
\r
149 // . pour chaque ligne :
\r
150 // . . extraire le "domaine", l' "ID" du message, sa "traduction"
\r
151 // . . remplacer les motifs msgid\s+"(^[\n\r]+)"[\s\n\r]+msgstr\s+""
\r
152 // . . pour chaque occurance :
\r
153 // . . . rechercher dans le fichier tab, récupérer l'ID catché par group 1
\r
154 // . . . remplacer l'occurence par la bonne traduction
\r
155 final String updatedText = this.replace(result, message);
\r
156 if (!result.equals(updatedText))
\r
159 result = updatedText;
\r
162 System.out.println("\n\t-> Nb updates: " + update);
\r
166 protected List<I18nMessage> extractMessage(final String tabFileContent)
\r
168 final List<I18nMessage> result = new ArrayList<I18nMessage>();
\r
169 // . lecture des lignes du fichier tab
\r
170 final BufferedReader r = new BufferedReader(
\r
171 new StringReader(tabFileContent.replace("\r", "")));
\r
175 while ((line = r.readLine()) != null)
\r
177 final Matcher matcherTabFile = PATTERN_TAB_LINE.matcher(line);
\r
178 if (matcherTabFile.find())
\r
180 result.add(new I18nMessage(// domain
\r
181 matcherTabFile.group(2),
\r
183 matcherTabFile.group(3),
\r
185 matcherTabFile.group(4)));
\r
189 catch (final IOException e)
\r
191 throw new RuntimeException(e);
\r
196 protected String replace(final String input, final I18nMessage msg)
\r
200 System.out.print("\t" + msg.getId());
\r
203 final String strFind = "(^|[\\n\\r])msgid\\s+\"" + Pattern.quote(msg.getId())
\r
204 + "\"[\\s\\n\\r]+msgstr\\s+\"\"";
\r
205 final String strRepl = "msgid \"" + msg.getId() + "\"\nmsgstr \"" + msg.getPoTranslation()
\r
208 final Matcher m = Pattern.compile(strFind).matcher(input);
\r
212 System.out.println("\tMsg not found in PO file: " + msg.getId() + "\t("
\r
213 + msg.getTabTranslation() + ")");
\r
218 output = m.replaceAll("$1" + Matcher.quoteReplacement(strRepl));
\r