From 9752bd5f880dcb0797c5858ede64c4f7ee94170c Mon Sep 17 00:00:00 2001 From: luccioman Date: Fri, 4 Sep 2015 13:44:44 +0200 Subject: [PATCH] Added utils to help translation without launching full YaCy application : - translate all source files with a locale - list all non translated files with a locale --- .../translation/ListNonTranslatedFiles.java | 111 +++++++++++++++++ .../yacy/utils/translation/TranslateAll.java | 100 +++++++++++++++ .../utils/translation/TranslatorUtil.java | 116 ++++++++++++++++++ 3 files changed, 327 insertions(+) create mode 100755 source/net/yacy/utils/translation/ListNonTranslatedFiles.java create mode 100755 source/net/yacy/utils/translation/TranslateAll.java create mode 100755 source/net/yacy/utils/translation/TranslatorUtil.java diff --git a/source/net/yacy/utils/translation/ListNonTranslatedFiles.java b/source/net/yacy/utils/translation/ListNonTranslatedFiles.java new file mode 100755 index 000000000..fe50749e9 --- /dev/null +++ b/source/net/yacy/utils/translation/ListNonTranslatedFiles.java @@ -0,0 +1,111 @@ +// ListNonTranslatedFiles.java +// ------------------------------------- +// part of YACY +// (C) by Michael Peter Christen; mc@yacy.net +// first published on http://www.anomic.de +// Frankfurt, Germany, 2004 +// +// $LastChangedDate$ +// $LastChangedRevision$ +// $LastChangedBy$ +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package net.yacy.utils.translation; + +import java.io.File; +import java.io.FilenameFilter; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import net.yacy.cora.util.ConcurrentLog; +import net.yacy.data.ListManager; +import net.yacy.data.Translator; +import net.yacy.kelondro.util.FileUtils; + +/** + * Util to help identifying non translated files. + * + * @author luc + * + */ +public class ListNonTranslatedFiles extends TranslatorUtil { + + /** + * Print on standard output result of search + * @param nonTranslatedFiles list of non translated files + */ + private static void printResults(List nonTranslatedFiles) { + System.out.println(nonTranslatedFiles.size() + " files are not translated."); + for(File file : nonTranslatedFiles) { + System.out.println(file); + } + } + + /** + * List all files from srcDir directory which are not translated using + * specified locale file with specified extensions. If no argument is set, + * default values are used. + * + * @param args + * runtime arguments
+ * + */ + public static void main(String args[]) { + File sourceDir = getSourceDir(args); + Path sourcePath = sourceDir.toPath(); + + File translationFile = getTranslationFile(args); + + List extensions = ListManager + .string2vector(getExtensions(args)); + + FilenameFilter fileFilter = new SourceFileFilter(extensions); + + String excludedDir = "locale"; + + ConcurrentLog.info("ListNonTranslatedFiles", "Listing non translated " + + extensions + " files from " + sourceDir + " using " + + translationFile); + + try { + Set translatedRelativePaths = Translator.loadTranslationsLists(translationFile).keySet(); + + List srcFiles = FileUtils.getFilesRecursive(sourceDir, excludedDir, fileFilter); + + List nonTranslatedFiles = new ArrayList<>(); + for(File srcFile : srcFiles) { + Path relativeSrcFile = sourcePath.relativize(srcFile.toPath()); + if(!translatedRelativePaths.contains(relativeSrcFile.toString())) { + nonTranslatedFiles.add(srcFile); + } + } + + printResults(nonTranslatedFiles); + + + } finally { + ConcurrentLog.shutdown(); + } + + } + +} diff --git a/source/net/yacy/utils/translation/TranslateAll.java b/source/net/yacy/utils/translation/TranslateAll.java new file mode 100755 index 000000000..de1f699c2 --- /dev/null +++ b/source/net/yacy/utils/translation/TranslateAll.java @@ -0,0 +1,100 @@ +// TranslateAll.java +// ------------------------------------- +// part of YACY +// (C) by Michael Peter Christen; mc@yacy.net +// first published on http://www.anomic.de +// Frankfurt, Germany, 2004 +// +// $LastChangedDate$ +// $LastChangedRevision$ +// $LastChangedBy$ +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package net.yacy.utils.translation; + +import java.io.File; + +import net.yacy.cora.util.ConcurrentLog; +import net.yacy.data.Translator; + +/** + * Util to translate all files without launching full YaCy application. + * + * @author luc + * + */ +public class TranslateAll extends TranslatorUtil { + + /** + * Translate all files from srcDir directory into dstDir directory using + * specified locale file with specified extensions. If no argument is set, + * default values are used. + * + * @param args + * runtime arguments
+ *
    + *
  • args[0] : source dir path
  • + *
  • args[1] : destination dir path
  • + *
  • args[2] : translation file path
  • + *
  • args[3] : extensions (separated by commas)
  • + *
+ */ + public static void main(String args[]) { + File sourceDir = getSourceDir(args); + + File destDir = getDestDir(args); + + File translationFile = getTranslationFile(args); + + String extensions = getExtensions(args); + + ConcurrentLog.info("TranslateAll", "Translating " + extensions + + " files from " + sourceDir + " to " + destDir + " using " + + translationFile); + + try { + Translator.translateFilesRecursive(sourceDir, destDir, + translationFile, extensions, "locale"); + } finally { + ConcurrentLog.shutdown(); + } + + } + + /** + * @param args + * main parameters + * @return translation source dir from parameters or default + * @throws IllegalArgumentException + * when no parameters is set and default is not found + */ + protected static File getDestDir(String[] args) { + File destDir; + if (args.length > 1) { + destDir = new File(args[1]); + } else { + String tmpDir = System.getProperty("java.io.tmpdir"); + if (tmpDir == null) { + throw new IllegalArgumentException( + "No destination dir specified, and default not found"); + } + destDir = new File(tmpDir + File.separator + + TranslateAll.class.getCanonicalName()); + } + return destDir; + } + +} diff --git a/source/net/yacy/utils/translation/TranslatorUtil.java b/source/net/yacy/utils/translation/TranslatorUtil.java new file mode 100755 index 000000000..a88cd3f28 --- /dev/null +++ b/source/net/yacy/utils/translation/TranslatorUtil.java @@ -0,0 +1,116 @@ +// TranslateAll.java +// ------------------------------------- +// part of YACY +// (C) by Michael Peter Christen; mc@yacy.net +// first published on http://www.anomic.de +// Frankfurt, Germany, 2004 +// +// $LastChangedDate$ +// $LastChangedRevision$ +// $LastChangedBy$ +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package net.yacy.utils.translation; + +import java.io.File; +import java.util.Locale; + +import net.yacy.yacy; +import net.yacy.data.Translator; + +/** + * Base class for launching a {@link Translator} method without full YaCy + * application started. + * + * @author luc + * + */ +public abstract class TranslatorUtil { + + /** + * @param args + * main parameters + * @return translation source dir from parameters or default + * (workingDir/htroot) + * @throws IllegalArgumentException + * when no parameters is set and default is not found + */ + protected static File getSourceDir(String[] args) { + File sourceDir; + if (args.length > 0) { + sourceDir = new File(args[0]); + } else { + String workingDir = System.getProperty("user.dir"); + if (workingDir == null) { + throw new IllegalArgumentException( + "No translation file specified, and default not found"); + } + sourceDir = new File(workingDir, "htroot"); + if (!sourceDir.exists() && !sourceDir.isDirectory()) { + throw new IllegalArgumentException( + "No translation file specified, and default not found : " + + sourceDir.getPath()); + } + } + return sourceDir; + } + + /** + * @param args + * main parameters + * @return translation file from parameters or default (base on current + * Locale) + * @throws IllegalArgumentException + * when no parameters is set and default is not found + */ + protected static File getTranslationFile(String[] args) { + File translationFile; + if (args.length > 2) { + translationFile = new File(args[2]); + } else { + String workingDir = System.getProperty("user.dir"); + if (workingDir == null) { + throw new IllegalArgumentException( + "No translation file specified, and default not found"); + } + translationFile = new File(workingDir, "locales" + File.separator + + Locale.getDefault().getLanguage() + ".lng"); + if (!translationFile.exists()) { + throw new IllegalArgumentException( + "No translation file specified, and default not found : " + + translationFile.getPath()); + } + } + return translationFile; + } + + /** + * @param args + * main parameters + * @return extensions list from parameters or default (same as used in + * {@link yacy}) + */ + protected static String getExtensions(String[] args) { + String extensions; + if (args.length > 3) { + extensions = args[3]; + } else { + extensions = "html,template,inc"; + } + return extensions; + } + +}