From c264cda6784cb322fd3f9b634cc340f746f6f666 Mon Sep 17 00:00:00 2001 From: allo Date: Tue, 20 Jun 2006 18:52:17 +0000 Subject: [PATCH] support to parse gettext files and to generate updated files, where only the new strings are empty. git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@2222 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- ChangeLog | 2 +- htroot/Gettext.html | 24 +++- htroot/Gettext.java | 48 ++++++-- source/de/anomic/data/gettext.java | 174 ++++++++++++++++++++++++----- 4 files changed, 205 insertions(+), 43 deletions(-) diff --git a/ChangeLog b/ChangeLog index f411a068e..44e5fabcf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,7 +4,7 @@ version 0.46 * ADDED: more authentication Methods (cookieAuth) * ADDED: Form-Login on User.html * ADDED: nice Errorpage for httpauth-fail. - * ADDED: support to generate gettext locales + * ADDED: support to generate gettext locales and parse them. version 0.45 * UPDATED: new Design of search page diff --git a/htroot/Gettext.html b/htroot/Gettext.html index 041894e61..4e8860087 100644 --- a/htroot/Gettext.html +++ b/htroot/Gettext.html @@ -1 +1,23 @@ -#[gettext]# \ No newline at end of file +#(mode)# + + + + Gettext Locales + #%env/templates/metas.template%# + + + #%env/templates/header.template%# +

Gettext Locales

+ Get empty gettext file +
+ + old file: + +
+ +#%env/templates/footer.template%# + + +:: +#[gettext]# +#(/mode)# \ No newline at end of file diff --git a/htroot/Gettext.java b/htroot/Gettext.java index 2e59c9e0b..b687cf5cb 100644 --- a/htroot/Gettext.java +++ b/htroot/Gettext.java @@ -1,6 +1,8 @@ import java.io.File; +import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Iterator; +import java.util.Map; import de.anomic.data.gettext; import de.anomic.http.httpHeader; @@ -33,19 +35,41 @@ import de.anomic.server.serverSwitch; public class Gettext{ public static serverObjects respond(httpHeader header, serverObjects post, serverSwitch env) { serverObjects prop = new serverObjects(); - String htRootPath = env.getConfig("htRootPath", "htroot"); - File sourceDir = new File(env.getRootPath(), htRootPath); - ArrayList list = gettext.createGettextRecursive(sourceDir, "html,template,inc", "locale"); - Iterator it=list.iterator(); - String out=""; - while(it.hasNext()){ - out+=(String)it.next()+"\n"; + + + if(post != null && post.get("mode").equals("1")){ + prop.put("mode", "1"); + File oldfile=null; + String oldfilename; + if(post.containsKey("oldfile")){ + oldfilename=(String) post.get("oldfile"); + oldfile=new File(env.getRootPath(), oldfilename); + if(!oldfile.exists()) + //TODO: display warning? + oldfile=null; + } + + String htRootPath = env.getConfig("htRootPath", "htroot"); + File sourceDir = new File(env.getRootPath(), htRootPath); + ArrayList list; + try { + list = gettext.createGettextRecursive(sourceDir, "html,template,inc", "locale", oldfile); + } catch (FileNotFoundException e) { + // TODO warn the user + list = gettext.createGettextRecursive(sourceDir, "html,template,inc", "locale", (Map)null); + } + Iterator it=list.iterator(); + String out=""; + while(it.hasNext()){ + out+=(String)it.next()+"\n"; + } + //this does not work + /*httpHeader outheader=new httpHeader(); + outheader.put("Content-Type", "text/plain"); + prop.setOutgoingHeader(outheader);*/ + prop.put("mode_gettext", out); } - //this does not work - /*httpHeader outheader=new httpHeader(); - outheader.put("Content-Type", "text/plain"); - prop.setOutgoingHeader(outheader);*/ - prop.put("gettext", out); + return prop; } diff --git a/source/de/anomic/data/gettext.java b/source/de/anomic/data/gettext.java index 5079891f0..8996d2285 100644 --- a/source/de/anomic/data/gettext.java +++ b/source/de/anomic/data/gettext.java @@ -23,6 +23,7 @@ package de.anomic.data; +import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -31,15 +32,17 @@ import java.io.InputStreamReader; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; import java.util.Iterator; - -import de.anomic.server.serverSwitch; +import java.util.Map; public class gettext{ - public static String createGettextOutput(serverSwitch env){ - return ""; + public static ArrayList createGettextRecursive(File sourceDir, String extensions, String notdir, File oldgettextfile) throws FileNotFoundException{ + if(oldgettextfile==null) + return createGettextRecursive(sourceDir, extensions, notdir, (Map)null); //no old file + return createGettextRecursive(sourceDir, extensions, notdir, parseGettext(oldgettextfile)); } - public static ArrayList createGettextRecursive(File sourceDir, String extensions, String notdir){ + public static ArrayList createGettextRecursive(File sourceDir, String extensions, String notdir, Map oldgettext){ ArrayList list=new ArrayList(); ArrayList exts=listManager.string2arraylist(extensions); Iterator it2; @@ -74,40 +77,51 @@ public class gettext{ } } } - list=createGettext(filenames); + list=createGettext(filenames, oldgettext); return list; } - /* - * create a list of gettext file for some textfiles - * @param filenames the ArrayList with the Filenames - */ - public static ArrayList createGettext(ArrayList filenames){ + private static ArrayList getGettextHeader(){ + return getGettextHeader("UNKNOWN", "EMAIL"); + } + private static ArrayList getGettextHeader(String translator, String email){ ArrayList list=new ArrayList(); - ArrayList tmp=null; - String filename=null; - Iterator it=filenames.iterator(); - list.add("#yacy translation"); list.add("msgid \"\""); list.add("msgstr \"\""); list.add("\"Content-Type: text/plain; charset=UTF-8\\n\""); list.add("\"Content-Transfer-Encoding: 8bit\\n\""); - list.add("\"Last-Translator: UNKNOWN\\n\""); + list.add("\"Last-Translator: "+translator+"\\n\""); SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-mm-dd HH:MMZ"); list.add("\"PO-Revision-Date: "+dateformat.format(new Date())+"\\n\""); list.add("\"Project-Id-Version: YaCy\\n\""); - list.add("\"Language-Team: \""); + list.add("\"Language-Team: <"+email+">\\n\""); list.add("\"X-Generator: YaCy\\n\""); list.add("\"Mime-Version: 1.0\\n\""); list.add(""); + return list; + } + public static ArrayList createGettext(ArrayList filenames, File oldgettextfile) throws FileNotFoundException{ + return createGettext(filenames, parseGettext(oldgettextfile)); + } + /* + * create a list of gettext file for some textfiles + * @param filenames the ArrayList with the Filenames + * @param oldgettextmap a map with the old translations. + */ + public static ArrayList createGettext(ArrayList filenames, Map oldgettext){ + ArrayList list=new ArrayList(); + ArrayList tmp=null; + String filename=null; + Iterator it=filenames.iterator(); + list.addAll(getGettextHeader()); while(it.hasNext()){ try { filename=(String)it.next(); - tmp=getGettextSource(new File(filename)); + tmp=getGettextSource(new File(filename), oldgettext); } catch (FileNotFoundException e) { System.out.println("File \""+filename+"\" not found."); } @@ -116,14 +130,32 @@ public class gettext{ } return list; } + public static ArrayList getGettextSource(File inputfile, File oldmapfile) throws FileNotFoundException{ + if(oldmapfile != null && oldmapfile.exists()) + return getGettextSource(inputfile, parseGettext(oldmapfile)); + return getGettextSource(inputfile); + } public static ArrayList getGettextSource(File inputfile) throws FileNotFoundException{ + return getGettextSource(inputfile, new HashMap()); + } + public static ArrayList getGettextSource(File inputfile, Map oldgettextmap) throws FileNotFoundException{ + if(oldgettextmap==null) + oldgettextmap=new HashMap(); + ArrayList strings=getGettextItems(inputfile); ArrayList list=new ArrayList(); Iterator it=strings.iterator(); + if(strings.isEmpty()) + return null; list.add("#"+inputfile.getName()); + String key; while(it.hasNext()){ - list.add("msgid \""+((String)it.next()).replaceAll("\"", "\\\"").replaceAll("\n", "\\\\n")+"\""); - list.add("msgstr \"\""); + key=((String)it.next()).replaceAll("\"", "\\\\\"").replaceAll("\n", "\\\\n"); + list.add("msgid \""+key+"\""); + if(oldgettextmap.containsKey(key)) + list.add("msgstr \""+oldgettextmap.get(key)+"\""); + else + list.add("msgstr \"\""); list.add(""); } return list; @@ -176,18 +208,102 @@ public class gettext{ } catch (IOException e) {} return list; } + public static HashMap parseGettext(File gettextfile) throws FileNotFoundException{ + ArrayList gettext=new ArrayList(); + String line; + BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(gettextfile))); + try { + line = br.readLine(); + while (line != null) { + gettext.add(line); + line = br.readLine(); + } + } catch (IOException e) {} + return parseGettext(gettext); + } + public static HashMap parseGettext(ArrayList gettext){ + HashMap map = new HashMap(); + int mode=0; //1= in msgid, 2= in msgstr + String msgid = "", msgstr = "", tmp = ""; + + Iterator it=gettext.iterator(); + while(it.hasNext()){ + tmp=(String) it.next(); + if(tmp.startsWith("msgid \"")){ + if(mode==2) + map.put(msgid, msgstr); + msgid=tmp.substring(7,tmp.length()-1).replaceAll("\\\"", "\""); + msgstr=""; + mode=1; + }else if(tmp.startsWith("msgstr \"")){ + mode=2; + msgstr=tmp.substring(8,tmp.length()-1); + }else if(tmp.startsWith("\"")){ + //multiline strings with "..." on each line + if(mode==1){ + msgid+="\n"+tmp.substring(1,tmp.length()-1).replaceAll("\\\"", "\""); + }else if(mode==2){ + msgstr+="\n"+tmp.substring(1,tmp.length()-1).replaceAll("\\\"", "\""); + } + } + } + map.put(msgid, msgstr); //the last one cannot be put, on the next msgid ;-) + return map; + } public static void main(String[] argv){ - if(argv.length < 1){ - System.out.println("Syntax: java de.anomic.data.gettext [inputfile]"); + if(argv.length < 2){ + System.out.println("Syntax: java de.anomic.data.gettext creategettext [inputfile] ... [inputfile]"); + System.out.println("Syntax: java de.anomic.data.gettext parsegettext [gettextfile]"); + System.out.println("Syntax: java de.anomic.data.gettext updategettext [gettextfile] [inputfile] ... [inputfile]"); System.exit(1); } - ArrayList filelist=new ArrayList(); - for(int i=0;i2){ + System.out.println("only one file allowed for parsegettext"); + System.exit(1); + } + try { + HashMap translations=parseGettext(new File(argv[1])); + Iterator it=translations.keySet().iterator(); + String key=""; + while(it.hasNext()){ + key=(String)it.next(); + System.out.println("key: "+key); + System.out.println("value: "+translations.get(key)); + } + } catch (FileNotFoundException e) { + System.exit(1); + } + }else if(argv[0].equals("updategettext")){ + if(argv.length < 3){ + System.out.println("Too less arguments"); + System.exit(1); + } + ArrayList filelist=new ArrayList(); + for(int i=2;i