optimize Translator

- translateFilesRecursive: load translation once (reduce io), return true on complete success
  - remove resulting unused translateFiles() variant
- translate: use StringBuilder parameter (skip toString conversion)
- remove not needed static declaration
- upd some javadoc
pull/58/head
reger 9 years ago
parent 786c92f95d
commit a2e0f00456

@ -67,11 +67,11 @@ public class Translator {
/** /**
* Translate source using entries in translationTable * Translate source using entries in translationTable
* @param source text to translate. Mus be non null. * @param source text to translate. Must be non null.
* @param translationTable translation entries : text to translate -> translation * @param translationTable translation entries : text to translate -> translation
* @return source translated * @return source translated
*/ */
public static String translate(final String source, public String translate(final StringBuilder source,
final Map<String, String> translationTable) { final Map<String, String> translationTable) {
final Set<Map.Entry<String, String>> entries = translationTable.entrySet(); final Set<Map.Entry<String, String>> entries = translationTable.entrySet();
StringBuilder builder = new StringBuilder(source); StringBuilder builder = new StringBuilder(source);
@ -146,7 +146,7 @@ public class Translator {
* @param translationList map of translations * @param translationList map of translations
* @return true when destFile was sucessfully written, false otherwise * @return true when destFile was sucessfully written, false otherwise
*/ */
public static boolean translateFile(final File sourceFile, final File destFile, final Map<String, String> translationList){ public boolean translateFile(final File sourceFile, final File destFile, final Map<String, String> translationList){
StringBuilder content = new StringBuilder(); StringBuilder content = new StringBuilder();
BufferedReader br = null; BufferedReader br = null;
@ -167,7 +167,7 @@ public class Translator {
} }
} }
String processedContent = translate(content.toString(), translationList); String processedContent = translate(content, translationList);
BufferedWriter bw = null; BufferedWriter bw = null;
try{ try{
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile), StandardCharsets.UTF_8)); bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile), StandardCharsets.UTF_8));
@ -186,11 +186,17 @@ public class Translator {
return true; return true;
} }
public boolean translateFiles(final File sourceDir, final File destDir, final File baseDir, final File translationFile, final String extensions){ /**
return translateFiles(sourceDir, destDir, baseDir, loadTranslationsLists(translationFile), extensions); * Translate files in sourceDir (relative path of baseDir) write result to destDir
} *
* @param sourceDir relative path
public static boolean translateFiles(final File sourceDir, final File destDir, final File baseDir, final Map<String, Map<String, String>> translationLists, final String extensions){ * @param destDir destination
* @param baseDir base dir of source
* @param translationLists translation to use
* @param extensions file extension to include in translation
* @return
*/
public boolean translateFiles(final File sourceDir, final File destDir, final File baseDir, final Map<String, Map<String, String>> translationLists, final String extensions){
destDir.mkdirs(); destDir.mkdirs();
final List<String> exts = ListManager.string2vector(extensions); final List<String> exts = ListManager.string2vector(extensions);
final File[] sourceFiles = sourceDir.listFiles(new ExtensionsFileFilter(exts)); final File[] sourceFiles = sourceDir.listFiles(new ExtensionsFileFilter(exts));
@ -219,17 +225,29 @@ public class Translator {
return true; return true;
} }
/**
* Translate files starting with sourceDir and all subdirectories.
*
* @param sourceDir
* @param destDir
* @param translationFile translation language file
* @param extensions extension of files to include in translation
* @param notdir directory to exclude
* @return true if all files translated (or none)
*/
public boolean translateFilesRecursive(final File sourceDir, final File destDir, final File translationFile, final String extensions, final String notdir) { public boolean translateFilesRecursive(final File sourceDir, final File destDir, final File translationFile, final String extensions, final String notdir) {
final List<File> dirList = FileUtils.getDirsRecursive(sourceDir, notdir); final List<File> dirList = FileUtils.getDirsRecursive(sourceDir, notdir);
dirList.add(sourceDir); dirList.add(sourceDir);
final Map<String, Map<String, String>> translationLists = loadTranslationsLists(translationFile);
boolean erg = true;
for (final File file : dirList) { for (final File file : dirList) {
if (file.isDirectory() && !file.getName().equals(notdir)) { if (file.isDirectory() && !file.getName().equals(notdir)) {
//cuts the sourcePath and prepends the destPath //cuts the sourcePath and prepends the destPath
File file2 = new File(destDir, file.getPath().substring(sourceDir.getPath().length())); File file2 = new File(destDir, file.getPath().substring(sourceDir.getPath().length()));
translateFiles(file, file2, sourceDir, translationFile, extensions); erg &= translateFiles(file, file2, sourceDir, translationLists, extensions);
} }
} }
return true; return erg;
} }
public static Map<String, String> langMap(@SuppressWarnings("unused") final serverSwitch env) { public static Map<String, String> langMap(@SuppressWarnings("unused") final serverSwitch env) {

Loading…
Cancel
Save