Skip to content

Commit

Permalink
Implement optional binary format for PoFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcashman committed Nov 12, 2021
1 parent 30f46b9 commit e15ba5c
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
24 changes: 24 additions & 0 deletions gettext-lib/src/main/java/org/mini2Dx/gettext/PoFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,30 @@ public void saveTo(File file) throws IOException {
printWriter.close();
}

public void saveToBin(File file) throws IOException {
final DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(file));
outputStream.writeInt(entries.size());
for(TranslationEntry translationEntry : entries) {
translationEntry.writeTo(outputStream);
}
outputStream.flush();
outputStream.close();
}

public static PoFile readFromBin(Locale locale, InputStream inputStream) throws IOException {
final DataInputStream dataInputStream = new DataInputStream(inputStream);
final int totalEntries = dataInputStream.readInt();

final PoFile poFile = new PoFile(locale);
for(int i = 0; i < totalEntries; i++) {
final TranslationEntry translationEntry = new TranslationEntry();
translationEntry.readFrom(dataInputStream);
poFile.entries.add(translationEntry);
}
dataInputStream.close();
return poFile;
}

@Override
public void enterEntry(GetTextParser.EntryContext ctx) {
currentEntry = new TranslationEntry();
Expand Down
113 changes: 113 additions & 0 deletions gettext-lib/src/main/java/org/mini2Dx/gettext/TranslationEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
******************************************************************************/
package org.mini2Dx.gettext;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -94,6 +97,116 @@ public void writeTo(PrintWriter printWriter) {
}
}

public void writeTo(DataOutputStream outputStream) throws IOException {
outputStream.writeInt(translatorComments.size());
for(String comment : translatorComments) {
outputStream.writeUTF(comment);
}

outputStream.writeInt(extractedComments.size());
for(String comment : extractedComments) {
outputStream.writeUTF(comment);
}

outputStream.writeInt(flags.size());
for(String comment : flags) {
outputStream.writeUTF(comment);
}

outputStream.writeInt(mergeComments.size());
for(String comment : mergeComments) {
outputStream.writeUTF(comment);
}

if(reference == null) {
outputStream.writeUTF("");
} else {
outputStream.writeUTF(reference);
}
if(context == null) {
outputStream.writeUTF("");
} else {
outputStream.writeUTF(context);
}
if(id == null) {
outputStream.writeUTF("");
} else {
outputStream.writeUTF(id);
}
if(idPlural == null) {
outputStream.writeUTF("");
} else {
outputStream.writeUTF(idPlural);
}

if(strings.isEmpty()) {
if(idPlural != null && !idPlural.isEmpty()) {
outputStream.writeInt(3);
outputStream.writeUTF("");
outputStream.writeUTF("");
outputStream.writeUTF("");
} else {
outputStream.writeInt(1);
outputStream.writeUTF("");
}
} else if(strings.size() > 1) {
outputStream.writeInt(strings.size());
for(int i = 0; i < strings.size(); i++) {
final String str = strings.get(i);
if(str == null) {
outputStream.writeUTF("");
} else {
outputStream.writeUTF(str);
}
}
} else {
outputStream.writeInt(1);
outputStream.writeUTF(strings.get(0));
}
}

public void readFrom(DataInputStream inputStream) throws IOException {
final int totalTranslatorComments = inputStream.readInt();
for(int i = 0; i < totalTranslatorComments; i++) {
translatorComments.add(inputStream.readUTF());
}

final int totalExtractedComments = inputStream.readInt();
for(int i = 0; i < totalExtractedComments; i++) {
extractedComments.add(inputStream.readUTF());
}

final int totalFlags = inputStream.readInt();
for(int i = 0; i < totalFlags; i++) {
flags.add(inputStream.readUTF());
}

final int totalMergeComments = inputStream.readInt();
for(int i = 0; i < totalMergeComments; i++) {
mergeComments.add(inputStream.readUTF());
}

reference = inputStream.readUTF();
context = inputStream.readUTF();
id = inputStream.readUTF();
idPlural = inputStream.readUTF();

if(reference.isEmpty()) {
reference = null;
}
if(context.isEmpty()) {
context = null;
}
if(idPlural.isEmpty()) {
idPlural = null;
}

final int totalStrings = inputStream.readInt();
for(int i = 0; i < totalStrings; i++) {
strings.add(inputStream.readUTF());
}
}

public List<String> getTranslatorComments() {
return translatorComments;
}
Expand Down
26 changes: 26 additions & 0 deletions gettext-lib/src/test/java/org/mini2Dx/gettext/PoFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
package org.mini2Dx.gettext;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Locale;
Expand All @@ -28,4 +30,28 @@ public void testReadWritePoFile() throws IOException {
Assert.assertEquals(expectedEntry, resultEntry);
}
}

@Test
public void testReadWritePoFileBinary() throws IOException {
final PoFile expected = new PoFile(Locale.ENGLISH, GetTextTest.class.getResourceAsStream("/sample_en.po"));
final File tmpFile = Files.createTempFile("", ".po").toFile();
expected.saveToBin(tmpFile);

final PoFile result = PoFile.readFromBin(Locale.ENGLISH, new FileInputStream(tmpFile));

Assert.assertEquals(expected.getEntries().size(), result.getEntries().size());
for(int i = 0; i < expected.getEntries().size(); i++) {
final TranslationEntry expectedEntry = expected.getEntries().get(i);
final TranslationEntry resultEntry = result.getEntries().get(i);
Assert.assertEquals(expectedEntry, resultEntry);
}
}

@Ignore
@Test
public void testReadPoFileWithNewLineInStr() throws IOException {
final PoFile poFile = new PoFile(Locale.ENGLISH, GetTextTest.class.getResourceAsStream("/sample_newline.po"));
Assert.assertEquals(1, poFile.getEntries().size());
Assert.assertEquals("Unknown\nerror", poFile.getEntries().get(0).getId());
}
}
5 changes: 5 additions & 0 deletions gettext-lib/src/test/resources/sample_newline.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# translator-comments
#. extracted-comments
#: src/msgcmp.java:322
msgid "Unknown\nerror"
msgstr "Unknown\nerror"

0 comments on commit e15ba5c

Please sign in to comment.