Skip to content

Instantly share code, notes, and snippets.

@kuoruan
Created December 5, 2016 05:53
Show Gist options
  • Save kuoruan/ab926bc60ecbdd5561b6081408b5e5e3 to your computer and use it in GitHub Desktop.
Save kuoruan/ab926bc60ecbdd5561b6081408b5e5e3 to your computer and use it in GitHub Desktop.
A util to sort Po File
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Po File Sorter
*
* @author Liao
* @create 2016-12-05 11:00
*/
public class PoSort {
static class Item implements Comparable<Item> {
private String local;
private String translate = "";
public Item() {
}
public Item(String local) {
this.local = local;
}
public String getLocal() {
return local;
}
public void setLocal(String local) {
this.local = local;
}
public String getTranslate() {
return translate;
}
public void setTranslate(String translate) {
this.translate = translate;
}
@Override
public String toString() {
return "Item{" +
"local='" + local + '\'' +
", translate='" + translate + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Item item = (Item) o;
return local != null ? local.equals(item.local) : item.local == null;
}
@Override
public int hashCode() {
return local != null ? local.hashCode() : 0;
}
@Override
public int compareTo(Item o) {
if (this == o) return 0;
if (o == null || getClass() != o.getClass()) {
return 1;
}
if (local != null && o.local != null) {
return local.compareToIgnoreCase(o.local);
}
return 0;
}
}
public static void main(String[] args) {
File input = new File("C:\\Projects\\luci-app-kcptun\\po\\zh-cn\\kcptun.po");
File output = new File(input.getAbsolutePath() + ".sort");
List<Item> items = new ArrayList<Item>();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(input)));
Pattern local = Pattern.compile("msgid\\s+\"(.+)\"");
Pattern translate = Pattern.compile("msgstr\\s+\"(.+)\"");
Matcher matcher;
String str;
Item item;
int line = 0;
while ((str = br.readLine()) != null) {
line++;
matcher = local.matcher(str);
if (matcher.find()) {
item = new Item(matcher.group(1));
while ((str = br.readLine()) != null) {
line++;
matcher = local.matcher(str);
if (matcher.find()) {
throw new RuntimeException("File Error at line " + line);
} else {
matcher = translate.matcher(str);
if (matcher.find()) {
item.setTranslate(matcher.group(1));
break;
}
}
}
items.add(item);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Collections.sort(items);
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));
int size = items.size();
Item item;
for (int i = 0; i < size; i++) {
item = items.get(i);
bw.write("msgid \"" + item.getLocal() + "\"");
bw.newLine();
bw.write("msgstr \"" + item.getTranslate() + "\"");
bw.newLine();
if (i < size - 1) {
bw.newLine();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Sort Complete!");
System.out.println("Output File: " + output.getAbsolutePath());
System.out.println("Total Count: " + items.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment