Skip to content

Commit

Permalink
support to parse gettext files and to generate updated files, where o…
Browse files Browse the repository at this point in the history
…nly the new strings are empty.

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@2222 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
allo committed Jun 20, 2006
1 parent 761d3bb commit c264cda
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 43 deletions.
2 changes: 1 addition & 1 deletion ChangeLog
Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion htroot/Gettext.html
@@ -1 +1,23 @@
#[gettext]#
#(mode)#
<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Gettext Locales</title>
#%env/templates/metas.template%#
</head>
<body marginheight="0" marginwidth="0" leftmargin="0" topmargin="0">
#%env/templates/header.template%#
<h2>Gettext Locales</h2>
<a href="Gettext.html?mode=1">Get empty gettext file</a>
<form action="Gettext.html" method="GET">
<input type="hidden" name="mode" value="1">
old file: <input type="text" name="oldfile" value="DATA/LOCALE">
<input type="submit" value="get updated file">
</form>

#%env/templates/footer.template%#
</body>
</html>
::
#[gettext]#
#(/mode)#
48 changes: 36 additions & 12 deletions 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;
Expand Down Expand Up @@ -33,19 +35,41 @@
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;
}
Expand Down
174 changes: 145 additions & 29 deletions source/de/anomic/data/gettext.java
Expand Up @@ -23,6 +23,7 @@

package de.anomic.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -31,15 +32,17 @@
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;
Expand Down Expand Up @@ -74,40 +77,51 @@ public static ArrayList createGettextRecursive(File sourceDir, String extensions
}
}
}
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: <EMAIL>\"");
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.");
}
Expand All @@ -116,14 +130,32 @@ public static ArrayList createGettext(ArrayList filenames){
}
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;
Expand Down Expand Up @@ -176,18 +208,102 @@ else if((char)character==')'){
} 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;i<argv.length;i++){
filelist.add(argv[i]);
if(argv[0].equals("creategettext")){
ArrayList filelist=new ArrayList();
for(int i=1;i<argv.length;i++){
filelist.add(argv[i]);
}
ArrayList list = createGettext(filelist, (Map)null);
Iterator it=list.iterator();
while(it.hasNext())
System.out.println((String)it.next());
}else if(argv[0].equals("parsegettext")){
if(argv.length >2){
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<argv.length;i++){
filelist.add(argv[i]);
}
try {
ArrayList list=createGettext(filelist, new File(argv[1]));
Iterator it=list.iterator();
while(it.hasNext())
System.out.println((String)it.next());
} catch (FileNotFoundException e) {
System.out.println("File not found.");
System.exit(1);
}
}else{
System.out.println("unknown Mode ...");
System.exit(1);
}
ArrayList list = createGettext(filelist);
Iterator it=list.iterator();
while(it.hasNext())
System.out.println((String)it.next());
}
}

0 comments on commit c264cda

Please sign in to comment.