Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/j9nls/com/ibm/oti/NLSTool/J9ReverseNLS.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2004, 2017 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.oti.NLSTool;
23
24
import java.io.BufferedReader;
25
import java.io.ByteArrayOutputStream;
26
import java.io.File;
27
import java.io.FileInputStream;
28
import java.io.FileWriter;
29
import java.io.IOException;
30
import java.io.InputStream;
31
import java.io.OutputStream;
32
import java.io.StringReader;
33
import java.io.Writer;
34
import java.util.Hashtable;
35
import java.util.Iterator;
36
import java.util.Properties;
37
import java.util.Vector;
38
39
public class J9ReverseNLS implements NLSConstants {
40
41
public static void main(String[] args) throws IOException {
42
43
if (args.length != 2 || args[0].compareToIgnoreCase("-help") == 0) {
44
printUsage();
45
System.exit(0);
46
}
47
48
String propertiesFile = args[0];
49
String locale = args[1];
50
51
J9NLS nlsTool = new J9NLS();
52
Hashtable nlsFiles = nlsTool.searchNLSFiles();
53
54
J9NLS.dp("Reading " + propertiesFile + "...");
55
Properties javaProperties = new Properties();
56
javaProperties.load(new FileInputStream(propertiesFile));
57
58
Vector baseNLSFiles = (Vector)nlsFiles.get("");
59
for (Iterator iter = baseNLSFiles.iterator(); iter.hasNext();) {
60
File nlsInputFile = (File) iter.next();
61
Properties nlsProperties = new Properties();
62
InputStream is = new FileInputStream(nlsInputFile);
63
nlsProperties.load(is);
64
is.close();
65
66
File nlsOutputFile = localeSpecificNLSFile(nlsInputFile, locale);
67
J9NLS.dp("Writing " + nlsOutputFile + "...");
68
Writer out = new FileWriter(nlsOutputFile);
69
out.write("# This file was automatically reverse engineered from " + nlsInputFile.getName() + " and " + propertiesFile + "\n");
70
out.write(MODULE_KEY + "=" + nlsProperties.getProperty(MODULE_KEY) + "\n");
71
out.write(HEADER_KEY + "=" + nlsProperties.getProperty(HEADER_KEY) + "\n");
72
out.write("\n");
73
74
Vector orderedKeys = nlsTool.getOrderedKeys(nlsProperties, nlsInputFile);
75
for (int index = 0; index < orderedKeys.size(); index++) {
76
String key = (String) orderedKeys.get(index);
77
String smallKey = nlsProperties.getProperty(MODULE_KEY) + nlsTool.formatMsgNumber(index);
78
String value = javaProperties.getProperty(smallKey);
79
80
if (value != null) {
81
out.write(encode(key, value));
82
} else {
83
System.err.println("Warning: no translated value found for " + key);
84
out.write("\n#translation required for the following key: \n");
85
out.write("#" + encode(key, nlsProperties.getProperty(key)) + "\n");
86
}
87
}
88
89
out.close();
90
}
91
}
92
93
94
private static void printUsage() {
95
System.out.println("Reverse engineers .NLS files from a .properties file.");
96
System.out.println("Usage: J9ReverseNLS java<locale>.properties locale");
97
System.out.println("e.g. J9ReverseNLS java_fr.properties _fr");
98
}
99
100
private static File localeSpecificNLSFile(File baseFile, String locale) {
101
String baseName = baseFile.getName();
102
String extension = baseName.substring(baseName.indexOf('.'), baseName.length());
103
baseName = baseName.substring(0, baseName.indexOf('.'));
104
return new File(baseFile.getParentFile(), baseName + locale + extension);
105
}
106
107
private static String encode(String key, String plainText) throws IOException {
108
Properties props = new Properties();
109
OutputStream os = new ByteArrayOutputStream();
110
String result = "";
111
112
props.setProperty(key, plainText);
113
props.store(os, null);
114
115
BufferedReader reader = new BufferedReader(new StringReader( os.toString() ) );
116
String line = reader.readLine();
117
/* skip leading comments */
118
while (line != null && line.length() > 0 && line.charAt(0) == '#') {
119
line = reader.readLine();
120
}
121
/* read the rest */
122
while (line != null) {
123
result += line;
124
result += "\n";
125
line = reader.readLine();
126
}
127
128
return result;
129
}
130
}
131
132