Skip to content

Commit 6b927df

Browse files
committed
Added I18N tools: import and export .tsv
1 parent 484ff3d commit 6b927df

File tree

2 files changed

+168
-64
lines changed

2 files changed

+168
-64
lines changed

adventure-editor/src/main/java/com/bladecoder/engineeditor/scneditor/ToolsWindow.java

Lines changed: 137 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
******************************************************************************/
1616
package com.bladecoder.engineeditor.scneditor;
1717

18+
import java.io.File;
19+
import java.io.IOException;
1820
import java.util.ArrayList;
1921
import java.util.HashMap;
2022

21-
import com.bladecoder.engine.util.Config;
22-
import com.bladecoder.engine.util.EngineLogger;
23+
import javafx.application.Platform;
24+
import javafx.stage.FileChooser;
25+
2326
import com.badlogic.gdx.scenes.scene2d.Actor;
2427
import com.badlogic.gdx.scenes.scene2d.Stage;
2528
import com.badlogic.gdx.scenes.scene2d.ui.Container;
@@ -39,7 +42,10 @@
3942
import com.bladecoder.engine.model.Verb;
4043
import com.bladecoder.engine.model.World;
4144
import com.bladecoder.engine.util.ActionUtils;
45+
import com.bladecoder.engine.util.Config;
46+
import com.bladecoder.engine.util.EngineLogger;
4247
import com.bladecoder.engineeditor.Ctx;
48+
import com.bladecoder.engineeditor.utils.I18NUtils;
4349
import com.bladecoder.engineeditor.utils.Message;
4450
import com.bladecoder.engineeditor.utils.RunProccess;
4551

@@ -53,8 +59,10 @@ public ToolsWindow(Skin skin, ScnWidget sw) {
5359
scnWidget = sw;
5460

5561
Table table = new Table(skin);
56-
TextButton button1 = new TextButton("Add Intelligent Cutmode", skin, "no-toggled");
57-
TextButton button2 = new TextButton("Test in Android device", skin, "no-toggled");
62+
TextButton setCutModeButton = new TextButton("Add Intelligent Cutmode", skin, "no-toggled");
63+
TextButton testInAndroidButton = new TextButton("Test in Android device", skin, "no-toggled");
64+
TextButton exportTSVButton = new TextButton("I18N - Export texts as .tsv", skin, "no-toggled");
65+
TextButton importTSVButton = new TextButton("I18N - Import.tsv file", skin, "no-toggled");
5866

5967
table.top();
6068
table.add(new Label("Tools", skin, "big")).center();
@@ -64,13 +72,19 @@ public ToolsWindow(Skin skin, ScnWidget sw) {
6472
setBackground(drawable);
6573

6674
table.row();
67-
table.add(button2).expandX().fill();
68-
69-
// table.row();
70-
// table.add(button1).expandX().fill();
75+
table.add(testInAndroidButton).expandX().fill();
76+
77+
table.row();
78+
table.add(exportTSVButton).expandX().fill();
79+
80+
table.row();
81+
table.add(importTSVButton).expandX().fill();
82+
83+
// table.row();
84+
// table.add(button1).expandX().fill();
7185

7286
// ADD CUTMODE FOR VERBS THAT HAVE ONLY A LOOKAT OR SAY ACTION
73-
button1.addListener(new ChangeListener() {
87+
setCutModeButton.addListener(new ChangeListener() {
7488
@Override
7589
public void changed(ChangeEvent event, Actor actor) {
7690

@@ -127,61 +141,134 @@ public void changed(ChangeEvent event, Actor actor) {
127141
});
128142

129143
// TEST IN ANDROID DEVICE
130-
button2.addListener(new ChangeListener() {
144+
testInAndroidButton.addListener(new ChangeListener() {
131145
@Override
132146
public void changed(ChangeEvent event, Actor actor) {
147+
testInAndroid();
148+
}
133149

134-
if (Ctx.project.getSelectedScene() == null) {
135-
String msg = "There are no scenes in this chapter.";
136-
Message.showMsg(getStage(), msg, 3);
137-
return;
150+
});
151+
152+
exportTSVButton.addListener(new ChangeListener() {
153+
@Override
154+
public void changed(ChangeEvent event, Actor actor) {
155+
exportTSV();
156+
}
157+
});
158+
159+
importTSVButton.addListener(new ChangeListener() {
160+
@Override
161+
public void changed(ChangeEvent event, Actor actor) {
162+
importTSV();
163+
}
164+
});
165+
166+
prefSize(200, 200);
167+
setSize(200, 200);
168+
}
169+
170+
private void exportTSV() {
171+
Platform.runLater(new Runnable() {
172+
@Override
173+
public void run() {
174+
175+
try {
176+
final FileChooser chooser = new FileChooser();
177+
chooser.setTitle("Select the file to export the project texts");
178+
final File outFile = chooser.showSaveDialog(null);
179+
180+
if (outFile == null) {
181+
return;
182+
}
183+
184+
I18NUtils.exportTSV(Ctx.project.getProjectDir().getAbsolutePath(), outFile.getAbsolutePath(),
185+
Ctx.project.getChapter().getId(), "default");
186+
187+
Message.showMsg(getStage(), outFile.getName() + " exported sucessfully.", 4);
188+
189+
I18NUtils.compare(Ctx.project.getProjectDir().getAbsolutePath(), Ctx.project.getChapter().getId(),
190+
null, "en");
191+
} catch (IOException e) {
192+
Message.showMsg(getStage(), "There was a problem generating the .tsv file.", 4);
193+
}
194+
}
195+
});
196+
}
197+
198+
private void importTSV() {
199+
Platform.runLater(new Runnable() {
200+
@Override
201+
public void run() {
202+
try {
203+
final FileChooser chooser = new FileChooser();
204+
chooser.setTitle("Select the .tsv file to import");
205+
final File inFile = chooser.showOpenDialog(null);
206+
if (inFile == null) {
207+
return;
208+
}
209+
210+
I18NUtils.importTSV(Ctx.project.getProjectDir().getAbsolutePath(), inFile.getAbsolutePath(),
211+
Ctx.project.getChapter().getId(), "default");
212+
213+
// Reload texts
214+
Ctx.project.getI18N().load(Ctx.project.getChapter().getId());
215+
216+
Message.showMsg(getStage(), inFile.getName() + " imported sucessfully.", 4);
217+
218+
} catch (IOException e) {
219+
Message.showMsg(getStage(), "There was a problem importing the .tsv file.", 4);
220+
e.printStackTrace();
221+
}
222+
}
223+
});
224+
}
225+
226+
private void testInAndroid() {
227+
if (Ctx.project.getSelectedScene() == null) {
228+
String msg = "There are no scenes in this chapter.";
229+
Message.showMsg(getStage(), msg, 3);
230+
return;
231+
}
232+
233+
Ctx.project.getProjectConfig().setProperty(Config.CHAPTER_PROP, Ctx.project.getChapter().getId());
234+
Ctx.project.getProjectConfig().setProperty(Config.TEST_SCENE_PROP, Ctx.project.getSelectedScene().getId());
235+
Ctx.project.setModified();
236+
237+
try {
238+
Ctx.project.saveProject();
239+
} catch (Exception ex) {
240+
String msg = "Something went wrong while saving the project.\n\n" + ex.getClass().getSimpleName() + " - "
241+
+ ex.getMessage();
242+
Message.showMsgDialog(getStage(), "Error", msg);
243+
return;
244+
}
245+
246+
new Thread(new Runnable() {
247+
Stage stage = getStage();
248+
249+
@Override
250+
public void run() {
251+
Message.showMsg(stage, "Running scene on Android device...", 5);
252+
253+
if (!RunProccess.runGradle(Ctx.project.getProjectDir(), "android:installDebug android:run")) {
254+
Message.showMsg(stage, "There was a problem running the project", 4);
138255
}
139-
140-
Ctx.project.getProjectConfig().setProperty(Config.CHAPTER_PROP, Ctx.project.getChapter().getId());
141-
Ctx.project.getProjectConfig().setProperty(Config.TEST_SCENE_PROP, Ctx.project.getSelectedScene().getId());
256+
257+
Ctx.project.getProjectConfig().remove(Config.CHAPTER_PROP);
258+
Ctx.project.getProjectConfig().remove(Config.TEST_SCENE_PROP);
142259
Ctx.project.setModified();
143-
260+
144261
try {
145262
Ctx.project.saveProject();
146263
} catch (Exception ex) {
147264
String msg = "Something went wrong while saving the project.\n\n" + ex.getClass().getSimpleName()
148265
+ " - " + ex.getMessage();
149-
Message.showMsgDialog(getStage(), "Error", msg);
266+
EngineLogger.error(msg);
150267
return;
151268
}
152269

153-
new Thread(new Runnable() {
154-
Stage stage = getStage();
155-
156-
@Override
157-
public void run() {
158-
Message.showMsg(stage, "Running scene on Android device...", 5);
159-
160-
if (!RunProccess.runGradle(Ctx.project.getProjectDir(), "android:installDebug android:run")) {
161-
Message.showMsg(stage, "There was a problem running the project", 4);
162-
}
163-
164-
Ctx.project.getProjectConfig().remove(Config.CHAPTER_PROP);
165-
Ctx.project.getProjectConfig().remove(Config.TEST_SCENE_PROP);
166-
Ctx.project.setModified();
167-
168-
try {
169-
Ctx.project.saveProject();
170-
} catch (Exception ex) {
171-
String msg = "Something went wrong while saving the project.\n\n" + ex.getClass().getSimpleName()
172-
+ " - " + ex.getMessage();
173-
EngineLogger.error(msg);
174-
return;
175-
}
176-
177-
}
178-
}).start();
179-
180270
}
271+
}).start();
181272

182-
});
183-
184-
prefSize(200, 200);
185-
setSize(200, 200);
186273
}
187274
}

adventure-editor/src/main/java/com/bladecoder/engineeditor/utils/I18NUtils.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
import java.io.UnsupportedEncodingException;
3131
import java.io.Writer;
3232
import java.net.URLEncoder;
33+
import java.util.ArrayList;
3334
import java.util.Collections;
3435
import java.util.Enumeration;
3536
import java.util.Properties;
37+
import java.util.Set;
3638
import java.util.TreeSet;
3739

3840
import com.bladecoder.engine.i18n.I18N;
@@ -43,11 +45,17 @@ public class I18NUtils {
4345
private static final String TSV_EXT = ".tsv";
4446
private static final String PROPERTIES_EXT = ".properties";
4547

46-
public static final void exportTSV(String projectPath, final String chapterId, String defaultLocale)
48+
public static final void exportTSV(String projectPath, String outFile, final String chapterId, String defaultLocale)
4749
throws FileNotFoundException, IOException {
4850
String modelPath = projectPath + Project.MODEL_PATH;
4951
File defaultChapter = new File(modelPath, chapterId + PROPERTIES_EXT);
50-
File outputFile = new File(modelPath, chapterId + TSV_EXT);
52+
53+
File outputFile;
54+
55+
if(outFile == null)
56+
outputFile = new File(modelPath, chapterId + TSV_EXT);
57+
else
58+
outputFile = new File(outFile);
5159

5260
// 1. Find all chapter properties
5361
File[] files = new File(modelPath).listFiles(new FilenameFilter() {
@@ -89,9 +97,18 @@ public boolean accept(File arg0, String arg1) {
8997
}
9098

9199
writer.write("\n");
100+
101+
Set<Object> keySet = props[0].keySet();
102+
ArrayList<String> keys = new ArrayList<>();
103+
104+
for (Object key : keySet) {
105+
keys.add((String)key);
106+
}
107+
108+
Collections.sort(keys);
92109

93-
for (Object key : props[0].keySet()) {
94-
writer.write((String) key);
110+
for (String key : keys) {
111+
writer.write(key);
95112

96113
for (Properties p : props) {
97114
if(p.getProperty((String) key) == null) {
@@ -109,10 +126,10 @@ public boolean accept(File arg0, String arg1) {
109126
}
110127

111128
@SuppressWarnings("serial")
112-
public static final void importTSV(String projectPath, String chapterId, String defaultLocale)
129+
public static final void importTSV(String projectPath, String tsvFile, String chapterId, String defaultLocale)
113130
throws FileNotFoundException, IOException {
114131
String modelPath = projectPath + Project.MODEL_PATH;
115-
File inputFile = new File(modelPath, chapterId + TSV_EXT);
132+
File inputFile = new File(tsvFile);
116133

117134
try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
118135
// get header
@@ -297,30 +314,30 @@ public static final String translatePhrase(String phrase, String sourceLangCode,
297314

298315
public static void usage() {
299316
System.out.println("Usage:"
300-
+ "\n\tI18NUtils tsv2properties project_path chapter_id default_locale"
301-
+ "\n\tI18NUtils properties2tsv project_path chapter_id default_locale"
317+
+ "\n\tI18NUtils import_tsv project_path input_tsv_file chapter_id default_locale"
318+
+ "\n\tI18NUtils export_tsv project_path ouput_tsv_file chapter_id default_locale"
302319
+ "\n\tI18NUtils newlocale project_path chapter_id default_locale new_locale"
303320
+ "\n\tI18NUtils sync project_path chapter_id dest_locale"
304321
+ "\n\tI18NUtils compare project_path chapter_id dest_locale");
305322
}
306323

307324
public static final void main(String[] args) throws FileNotFoundException, IOException {
308325

309-
if (args[0].equals("tsv2properties")) {
310-
if (args.length != 4) {
326+
if (args[0].equals("import_tsv")) {
327+
if (args.length != 5) {
311328
usage();
312329
System.exit(-1);
313330
}
314331

315-
importTSV(args[1], args[2], args[3]);
332+
importTSV(args[1], args[2], args[3], args[4]);
316333
System.out.println("Properties generated sucessfully.");
317-
} else if (args[0].equals("properties2tsv")) {
318-
if (args.length != 4) {
334+
} else if (args[0].equals("export_tsv")) {
335+
if (args.length != 5) {
319336
usage();
320337
System.exit(-1);
321338
}
322339

323-
exportTSV(args[1], args[2], args[3]);
340+
exportTSV(args[1], args[2], args[3], args[4]);
324341
System.out.println(".tsv file generated sucessfully.");
325342
} else if (args[0].equals("newlocale")) {
326343
if (args.length != 5) {

0 commit comments

Comments
 (0)