Added code to generate new english.pot file and some tools to ease

transition of existing translations towards the new content.
This commit is contained in:
Zukero
2018-08-10 23:45:50 +02:00
parent 84e46ffd20
commit 3800bf8ff0
9 changed files with 852 additions and 1 deletions

View File

@@ -0,0 +1,100 @@
/**
*
* @author Balázs Tóth (tobal17@gmail.com)
*
* Modified by Kevin POCHAT for ATCS
*/
package net.launchpad.tobal.poparser;
import java.util.Vector;
public class POEntry
{
private POLine[] Lines;
public enum StringType
{
/**translator comments*/
TRLCMNT,
/**extracted comments*/
EXTCMNT,
/**reference*/
REFERENCE,
/**flag*/
FLAG,
/**previous context*/
PREVCTXT,
/**previous untranslated string singular*/
PREVUNTRSTRSING,
/**previous untranslated string plural*/
PREVUNTRSTRPLUR,
/**untranslated string singular*/
MSGID,
/**translated string*/
MSGSTR,
/**context*/
MSGCTXT,
/**header line*/
HEADER
// TODO: support for plural untranslated strings,
// and translated string cases
}
POEntry()
{
Lines = new POLine[0];
}
public void addLine(StringType type, String string)
{
boolean hasType = false;
POLine line = null;
for(int i = 0; i < Lines.length; i++)
{
if(Lines[i].getType() == type)
{
hasType = true;
line = Lines[i];
break;
}
}
if(hasType)
{
line.addString(string);
}
else
{
line = new POLine(type, string);
POLine[] templines = Lines.clone();
Lines = new POLine[Lines.length+1];
for(int i = 0; i < Lines.length-1; i++)
{
Lines[i] = templines[i];
}
Lines[Lines.length-1] = line;
}
}
public POLine[] getLines()
{
return Lines;
}
public Vector<String> getStringsFromLine(int index)
{
return Lines[index].getStrings();
}
public Vector<String> getStringsByType(POEntry.StringType type)
{
for(int i = 0; i < Lines.length; i++)
{
if(Lines[i].getType() == type)
{
return Lines[i].getStrings();
}
}
return null;
}
}

View File

@@ -0,0 +1,139 @@
/**
*
* @author Balázs Tóth (tobal17@gmail.com)
*
* Modified by Kevin POCHAT for ATCS
*/
package net.launchpad.tobal.poparser;
import java.io.File;
import java.util.Vector;
public class POFile
{
private POEntry[] entries;
private POEntry header;
private File file;
POFile(POEntry[] entries, POEntry header, File file)
{
this.entries = entries;
this.header = header;
this.file = file;
}
/**
* Returns with the name of the po file
* @return name of po file
*/
public String getFileName()
{
return file == null ? null : file.getAbsolutePath();
}
/**
* Returns with the POEntry object array
* @return POEntry array
*/
public POEntry[] getEntryArray()
{
return entries;
}
/**
* Gets the POEntry object specified by the index
* @param index, index of the entry
* @return one POEntry object
*/
public POEntry getEntry(int index)
{
return entries[index];
}
/**
* Returns how many entries are there in the po file
* @return count of entries
*/
public int getEntryLength()
{
return entries.length;
}
/**
* Checks if the specified flag is set in the entry,
* given by the entry index.
* @param flag, string representing the flag
* @param entryIndex, index of the entry to examine
* @return true, if the flag is set, false otherwise
*/
public boolean checkFlag(String flag, int entryIndex)
{
boolean status = false;
Vector<String> strings = new Vector<String>();
strings = entries[entryIndex].getStringsByType(POEntry.StringType.FLAG);
if (strings != null)
{
for(int i = 0; i < strings.size(); i++)
{
if (strings.get(i).contains(flag))
{
status = true;
}
}
}
return status;
}
/**
* Returns with all the strings of the given type, from
* the specified entry.
* @param entryIndex
* @param type
* @return String array of specified type
*/
public String[] getStringsFromEntryByType(int entryIndex, POEntry.StringType type)
{
Vector<String> vector = entries[entryIndex].getStringsByType(type);
String[] str = new String[vector.size()];
for(int i = 0; i < str.length; i++)
{
str[i] = vector.get(i);
}
return str;
}
/**
* For debug purposes
*/
public void printFile()
{
for(int i = 0; i < entries.length; i++)
{
POLine[] lines = entries[i].getLines();
for(int j = 0; j < lines.length; j++)
{
Vector<String> strings = lines[j].getStrings();
for(int k = 0; k < strings.size(); k++)
{
System.out.println(strings.get(k));
}
}
}
}
/**
* For debug purposes
*/
public void printHeader()
{
POLine[] lines = header.getLines();
for(int j = 0; j < lines.length; j++)
{
Vector<String> strings = lines[j].getStrings();
for(int k = 0; k < strings.size(); k++)
{
System.out.println(strings.get(k));
}
}
}
}

View File

@@ -0,0 +1,42 @@
/**
*
* @author Balázs Tóth (tobal17@gmail.com)
*
* Modified by Kevin POCHAT for ATCS
*/
package net.launchpad.tobal.poparser;
import java.util.Vector;
public class POLine
{
private POEntry.StringType type;
private Vector<String> strings;
POLine(POEntry.StringType type, String string)
{
this.type = type;
this.strings = new Vector<String>();
this.strings.add(string);
}
public void addString(String string)
{
strings.add(string);
}
public Vector<String> getStrings()
{
return strings;
}
public POEntry.StringType getType()
{
return type;
}
public int getVectorSize()
{
return strings.size();
}
}

View File

@@ -0,0 +1,275 @@
/**
*
* @author Balázs Tóth (tobal17@gmail.com)
*
* Based on the work of István Nyitrai
*
* Modified by Kevin POCHAT for ATCS
*/
package net.launchpad.tobal.poparser;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.BufferedReader;
import java.util.Vector;
public class POParser
{
private POEntry[] entries;
private POEntry header;
// private File file;
private POEntry.StringType parserMode;
/**
* Creates a POParser object. Use getPOFile() method,
* to access parsed data.
* @param file, File object of the PO file
*/
public POParser()
{
parserMode = null;
}
public POFile parseFile(File file)
{
return parse(file);
}
public POFile parseStream(BufferedReader br) throws IOException {
return parse(br);
}
private String unQuote(String string)
{
String str = new String();
if(string.startsWith("\""))
{
str = string.substring(1);
string = str;
}
if(string.endsWith("\""))
{
str = string.substring(0, string.length()-1);
}
return str;
}
private POFile parse(File file)
{
POFile result = null;
try
{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
result = parse(br);
br.close();
fr.close();
}
catch (java.io.FileNotFoundException e)
{
System.out.println(e.toString());
}
catch (java.io.IOException e) {
System.out.println(e.toString());
}
return result;
}
private POFile parse(BufferedReader br) throws IOException {
Vector<String> rawentry = new Vector<String>(1, 1);
Vector<Vector<String>> rawentries = new Vector<Vector<String>>();
String line;
int id = 0;
while((line = br.readLine()) != null)
{
if(!line.equals(""))
{
if(!line.startsWith("#~")) // ignore
{
rawentry.add(line);
}
}
else
{
if(rawentry.size() > 0)
{
rawentry.add(0, String.valueOf(id));
id++;
rawentries.add(new Vector<String>(rawentry));
rawentry = new Vector<String>(1, 1);
}
}
}
if(!rawentry.equals(rawentries.lastElement()) && rawentry.size() > 0)
{
rawentry.add(0, String.valueOf(id));
rawentries.add(new Vector<String>(rawentry));
}
this.header = parseHeader(rawentries);
this.entries = parseEntries(rawentries);
return new POFile(entries, header, null);
}
private POEntry parseHeader(Vector<Vector<String>> vectors)
{
POEntry tempheader = new POEntry();
// is this header?
Vector<String> rawentry = vectors.get(0);
if(new Integer(rawentry.get(0)) == 0 && rawentry.contains("msgid \"\""))
{
for(int i = 1; i < rawentry.size(); i++)
{
String str = rawentry.get(i);
tempheader.addLine(POEntry.StringType.HEADER, str);
str = new String();
}
return tempheader;
}
else
{
return null;
}
}
private POEntry[] parseEntries(Vector<Vector<String>> vectors)
{
String line = new String();
boolean thereIsHeader = false;
// is this header
Vector<String> rawentry = vectors.get(0);
if(new Integer(rawentry.get(0)) == 0 && rawentry.contains("msgid \"\""))
{
thereIsHeader = true;
}
int size;
if(thereIsHeader)
{
size = vectors.size()-1;
}
else
{
size = vectors.size();
}
POEntry[] tempentries = new POEntry[size];
for(int i = 0; i < size; i++)
{
POEntry entry = new POEntry();
if(thereIsHeader)
rawentry = vectors.get(i+1);
else
rawentry = vectors.get(i);
rawentry.remove(0);
for(int j = 0; j < rawentry.size(); j++)
{
line = rawentry.get(j);
POEntry.StringType strType = null;
int subStrIndex = 0;
if(line.startsWith("#"))
{
parserMode = null;
if(line.startsWith("# "))
{
strType = POEntry.StringType.TRLCMNT;
if (line.startsWith("# "))
{
subStrIndex = 3;
}
else
{
subStrIndex = 2;
}
}
if(line.startsWith("#."))
{
strType = POEntry.StringType.EXTCMNT;
subStrIndex = 3;
}
if(line.startsWith("#:"))
{
strType = POEntry.StringType.REFERENCE;
subStrIndex = 3;
}
if(line.startsWith("#,"))
{
strType = POEntry.StringType.FLAG;
subStrIndex = 3;
}
if(line.startsWith("#|"))
{
// TODO: can these comments be multi line? if no,
// drop support for it
if(line.startsWith("#| msgctxt "))
{
strType = POEntry.StringType.PREVCTXT;
parserMode = strType;
subStrIndex = 11;
}
if(line.startsWith("#| msgid "))
{
strType = POEntry.StringType.PREVUNTRSTRSING;
parserMode = strType;
subStrIndex = 9;
}
if(line.startsWith("#| msgid_plural "))
{
strType = POEntry.StringType.PREVUNTRSTRPLUR;
parserMode = strType;
subStrIndex = 16;
}
}
String str = new String();
str = line.substring(subStrIndex);
entry.addLine(strType, str);
}
else if(line.startsWith("msg"))
{
parserMode = null;
if(line.startsWith("msgctxt "))
{
strType = POEntry.StringType.MSGCTXT;
parserMode = strType;
subStrIndex = 8;
}
if(line.startsWith("msgid "))
{
strType = POEntry.StringType.MSGID;
parserMode = strType;
subStrIndex = 6;
}
if(line.startsWith("msgstr "))
{
strType = POEntry.StringType.MSGSTR;
parserMode = strType;
subStrIndex = 7;
}
String str = new String();
// TODO: is unquoting nessessary?
str = unQuote(line.substring(subStrIndex));
entry.addLine(strType, str);
}
else
{
if(parserMode != null)
{
entry.addLine(parserMode, unQuote(line));
}
}
}
tempentries[i] = entry;
}
return tempentries;
}
}

View File

@@ -0,0 +1,24 @@
/**
* @author Balázs Tóth (tobal17@gmail.com)
*
* Modified by Kevin POCHAT for ATCS
*/
package net.launchpad.tobal.poparser;
import java.io.File;
public class ParserTest
{
public static void main(String args[])
{
File file = new File("C:\\file.po");
POParser parser = new POParser();
POFile po = parser.parseFile(file);
po.printHeader();
po.printFile();
// is the 3th entry fuzzy?
boolean fuzzy = po.checkFlag("fuzzy", 3);
// give me the msgid of the 4th entry
String[] str = po.getStringsFromEntryByType(4, POEntry.StringType.MSGID);
}
}