forked from omegat-org/omegat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateDocIndexUtil.java
157 lines (134 loc) · 5.51 KB
/
CreateDocIndexUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**************************************************************************
OmegaT - Computer Assisted Translation (CAT) tool
with fuzzy matching, translation memory, keyword search,
glossaries, and translation leveraging into updated projects.
Copyright (C) 2009 Alex Buloichik
Home page: http:https://www.omegat.org/
Support center: http:https://groups.yahoo.com/group/OmegaT/
This file is part of OmegaT.
OmegaT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmegaT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http:https://www.gnu.org/licenses/>.
**************************************************************************/
package util;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.omegat.util.OStrings;
public class CreateDocIndexUtil {
protected static final String MARK_BEG = "<table>";
protected static final String MARK_END = "</table>";
protected static Properties langExceptionsNames = new Properties();
public static void main(String[] args) throws Exception {
InputStream in = CreateDocIndexUtil.class
.getResourceAsStream("lang_exceptions.properties");
try {
langExceptionsNames.load(in);
} finally {
in.close();
}
File[] ls = new File("docs").listFiles(new FileFilter() {
public boolean accept(File f) {
return f.isDirectory() && new File(f, "index.html").exists();
}
});
Arrays.sort(ls, new Comparator<File>() {
public int compare(File o1, File o2) {
return o1.getName().compareTo(o2.getName());
}
});
String template = readTemplate();
int mb = template.indexOf(MARK_BEG);
int me = template.indexOf(MARK_END) + MARK_END.length();
if (mb < 0 || me < 0) {
throw new Exception(
"Error creating docs/index.html: there are no marks");
}
StringBuilder text = new StringBuilder();
text.append("<table>" + System.getProperty("line.separator"));
int count = 0;
for (File f : ls) {
String locale = f.getName();
String localeName = getLocaleName(locale);
String transVersion = getDocVersion(locale);
// Skip incomplete translations
if (transVersion == null)
continue;
// Add some HTML for the translation
text.append("<tr><td><a href=\"").append(locale).append(
"/index.html#__sethome");
text.append("\">");
text.append(localeName);
text.append("</a></td><td>(");
if (transVersion.equals(OStrings.VERSION))
text.append("<font color=\"green\"><strong>");
else
text.append("<font color=\"red\">");
text.append(transVersion);
if (transVersion.equals(OStrings.VERSION))
text.append("</strong>");
text.append("</font>)</td></tr>"
+ System.getProperty("line.separator"));
count++;
}
text.append("</table>");
System.out.println("Add " + count + " locales to docs/index.html");
writeOut(template.substring(0, mb) + text + template.substring(me));
}
protected static String readTemplate() throws IOException {
Reader rd = new InputStreamReader(
new FileInputStream("docs/index.html"), "UTF-8");
return IOUtils.toString(rd);
}
protected static void writeOut(String text) throws IOException {
Writer wr = new OutputStreamWriter(new FileOutputStream(
"docs/index.html"), "UTF-8");
wr.write(text);
wr.flush();
wr.close();
}
/**
* Returns the full locale name for a locale tag.
*
* @see Locale.getDisplayName(Locale inLocale)
* @author Henry Pijffers ([email protected])
* @author Didier Briel
*/
private static String getLocaleName(String localeTag) {
String language = localeTag.substring(0, 2);
String name = langExceptionsNames.getProperty(language);
if (name == null) {
String country = localeTag.length() >= 5 ? localeTag
.substring(3, 5) : "";
Locale locale = new Locale(language, country);
name = locale.getDisplayName(locale);
}
return name;
}
private static String getDocVersion(String locale) throws IOException {
Properties prop = new Properties();
prop
.load(new FileInputStream("docs/" + locale
+ "/version.properties"));
return prop.getProperty("version");
}
}