Skip to content
This repository was archived by the owner on Mar 15, 2023. It is now read-only.

Commit 84c64ab

Browse files
authoredJun 19, 2018
Merge pull request #42 from vickyvxr/master
Plain JSON keys instead of tree keys
2 parents f7296f8 + b4fe95a commit 84c64ab

8 files changed

+50
-8
lines changed
 

‎src/main/java/com/jvms/i18neditor/editor/Editor.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public boolean saveProject() {
209209
if (project != null) {
210210
for (Resource resource : project.getResources()) {
211211
try {
212-
Resources.write(resource, !project.isMinifyResources());
212+
Resources.write(resource, !project.isMinifyResources(), project.isPlainJSON());
213213
} catch (IOException e) {
214214
error = true;
215215
log.error("Error saving resource file " + resource.getPath(), e);
@@ -831,6 +831,7 @@ private void updateTreeNodeStatus(String key) {
831831
private void storeProjectState() {
832832
ExtendedProperties props = new ExtendedProperties();
833833
props.setProperty("minify_resources", project.isMinifyResources());
834+
props.setProperty("plain_json", project.isPlainJSON());
834835
props.setProperty("resource_name", project.getResourceName());
835836
props.setProperty("resource_type", project.getResourceType().toString());
836837
props.store(Paths.get(project.getPath().toString(), PROJECT_FILE));
@@ -842,6 +843,7 @@ private void restoreProjectState(EditorProject project) {
842843
if (Files.exists(path)) {
843844
props.load(Paths.get(project.getPath().toString(), PROJECT_FILE));
844845
project.setMinifyResources(props.getBooleanProperty("minify_resources", settings.isMinifyResources()));
846+
project.setPlainJSON(props.getBooleanProperty("plain_json", settings.isPlainJSON()));
845847
project.setResourceName(props.getProperty("resource_name", settings.getResourceName()));
846848
project.setResourceType(props.getEnumProperty("resource_type", ResourceType.class));
847849
} else {
@@ -858,6 +860,7 @@ private void storeEditorState() {
858860
props.setProperty("window_pos_y", getY());
859861
props.setProperty("window_div_pos", contentPane.getDividerLocation());
860862
props.setProperty("minify_resources", settings.isMinifyResources());
863+
props.setProperty("plain_json", settings.isPlainJSON());
861864
props.setProperty("resource_name", settings.getResourceName());
862865
props.setProperty("check_version", settings.isCheckVersionOnStartup());
863866
props.setProperty("default_input_height", settings.getDefaultInputHeight());
@@ -891,6 +894,7 @@ private void restoreEditorState() {
891894
settings.setLastExpandedNodes(props.getListProperty("last_expanded"));
892895
settings.setLastSelectedNode(props.getProperty("last_selected"));
893896
settings.setMinifyResources(props.getBooleanProperty("minify_resources", false));
897+
settings.setPlainJSON(props.getBooleanProperty("plain_json", false));
894898
settings.setResourceName(props.getProperty("resource_name", DEFAULT_RESOURCE_NAME));
895899
settings.setCheckVersionOnStartup(props.getBooleanProperty("check_version", true));
896900
settings.setDefaultInputHeight(props.getIntegerProperty("default_input_height", 5));

‎src/main/java/com/jvms/i18neditor/editor/EditorProject.java

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class EditorProject {
1919
private ResourceType resourceType;
2020
private List<Resource> resources = Lists.newLinkedList();
2121
private boolean minifyResources;
22+
private boolean plainJSON;
2223

2324
public EditorProject(Path path) {
2425
this.path = path;
@@ -71,4 +72,12 @@ public boolean isMinifyResources() {
7172
public void setMinifyResources(boolean minifyResources) {
7273
this.minifyResources = minifyResources;
7374
}
75+
76+
public boolean isPlainJSON() {
77+
return plainJSON;
78+
}
79+
80+
public void setPlainJSON(boolean plainJSON) {
81+
this.plainJSON = plainJSON;
82+
}
7483
}

‎src/main/java/com/jvms/i18neditor/editor/EditorProjectSettingsPane.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ private void setupUI() {
4141
minifyBox.addChangeListener(e -> project.setMinifyResources(minifyBox.isSelected()));
4242
fieldset1.add(minifyBox, createVerticalGridBagConstraints());
4343
}
44-
44+
if (project.getResourceType().equals(ResourceType.JSON)) {
45+
JCheckBox plainJSONBox = new JCheckBox(MessageBundle.get("settings.plainJSON.title"));
46+
plainJSONBox.setSelected(project.isPlainJSON());
47+
plainJSONBox.addChangeListener(e -> project.setPlainJSON(plainJSONBox.isSelected()));
48+
fieldset1.add(plainJSONBox, createVerticalGridBagConstraints());
49+
}
4550
JPanel resourcePanel = new JPanel(new GridLayout(0, 1));
4651
JLabel resourceNameLabel = new JLabel(MessageBundle.get("settings.resourcename.title"));
4752
JTextField resourceNameField = new JTextField(project.getResourceName());

‎src/main/java/com/jvms/i18neditor/editor/EditorSettings.java

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class EditorSettings {
1515
private int windowHeight;
1616
private String resourceName;
1717
private boolean minifyResources;
18+
private boolean plainJSON;
1819
private List<String> history;
1920
private List<String> lastExpandedNodes;
2021
private String lastSelectedNode;
@@ -134,4 +135,12 @@ public boolean isDoubleClickTreeToggling() {
134135
public void setDoubleClickTreeToggling(boolean doubleClickTreeToggling) {
135136
this.doubleClickTreeToggling = doubleClickTreeToggling;
136137
}
138+
139+
public boolean isPlainJSON() {
140+
return plainJSON;
141+
}
142+
143+
public void setPlainJSON(boolean plainJSON) {
144+
this.plainJSON = plainJSON;
145+
}
137146
}

‎src/main/java/com/jvms/i18neditor/util/Resources.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,16 @@ public static void load(Resource resource) throws IOException {
114114
*
115115
* @param resource the resource to write.
116116
* @param prettyPrinting whether to pretty print the contents
117+
* @param plainKeys
117118
* @throws IOException if an I/O error occurs writing the file.
118119
*/
119-
public static void write(Resource resource, boolean prettyPrinting) throws IOException {
120+
public static void write(Resource resource, boolean prettyPrinting, boolean plainKeys) throws IOException {
120121
ResourceType type = resource.getType();
121122
if (type == ResourceType.Properties) {
122123
ExtendedProperties content = toProperties(resource.getTranslations());
123124
content.store(resource.getPath());
124125
} else {
125-
String content = toJson(resource.getTranslations(), prettyPrinting);
126+
String content = toJson(resource.getTranslations(), prettyPrinting, plainKeys);
126127
if (type == ResourceType.ES6) {
127128
content = jsonToEs6(content);
128129
}
@@ -153,7 +154,7 @@ public static Resource create(Path root, ResourceType type, Optional<Locale> loc
153154
path = Paths.get(root.toString(), locale.get().toString(), baseName + extension);
154155
}
155156
Resource resource = new Resource(type, path, locale.orElse(null));
156-
write(resource, false);
157+
write(resource, false, false);
157158
return resource;
158159
}
159160

@@ -212,16 +213,28 @@ private static void fromJson(String key, JsonElement elem, Map<String,String> co
212213
}
213214
}
214215

215-
private static String toJson(Map<String,String> translations, boolean prettify) {
216+
private static String toJson(Map<String,String> translations, boolean prettify, boolean plainKeys) {
216217
List<String> keys = Lists.newArrayList(translations.keySet());
217-
JsonElement elem = toJson(translations, null, keys);
218+
JsonElement elem = !plainKeys ? toJson(translations, null, keys) : toPlainJson(translations, keys);
218219
GsonBuilder builder = new GsonBuilder().disableHtmlEscaping();
219220
if (prettify) {
220221
builder.setPrettyPrinting();
221222
}
222223
return builder.create().toJson(elem);
223224
}
224225

226+
private static JsonElement toPlainJson(Map<String, String> translations, List<String> keys) {
227+
JsonObject object = new JsonObject();
228+
if (keys.size() > 0) {
229+
translations.forEach((k, v) -> {
230+
if (translations.get(k)!=null){
231+
object.add(k, new JsonPrimitive(translations.get(k)));
232+
}
233+
});
234+
}
235+
return object;
236+
}
237+
225238
private static JsonElement toJson(Map<String,String> translations, String key, List<String> keys) {
226239
if (keys.size() > 0) {
227240
JsonObject object = new JsonObject();

‎src/main/resources/bundles/messages.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ settings.minify.title = Minify translations on save
9191
settings.resourcename.title = Translations filename
9292
settings.treetogglemode.title = Expand and collapse translations using double click
9393
settings.checkversion.title = Check for new version on startup
94-
94+
settings.plainJSON.title= Plain JSON keys
9595
swing.action.copy = Copy
9696
swing.action.cut = Cut
9797
swing.action.delete = Delete

‎src/main/resources/bundles/messages_nl.properties

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ settings.minify.title = Comprimeer vertalingen bij opslaan
9191
settings.resourcename.title = Bestandsnaam vertalingen
9292
settings.treetogglemode.title = Vertalingen in- en uitvouwen met dubbelklik
9393
settings.checkversion.title = Controleer op nieuwe versie bij opstarten
94+
settings.plainJSON.title= Plain JSON keys
9495

9596
swing.action.copy = Kopi\u00ebren
9697
swing.action.cut = Knippen

‎src/main/resources/bundles/messages_pt_BR.properties

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ settings.minify.title = Minificar tradu\u00e7\u00f5es ao salvar
9191
settings.resourcename.title = Nome do arquivo de tradu\u00e7\u00e3es
9292
settings.treetogglemode.title = Expandir e contrair tradu\u00e7\u00f5es usando duplo clique
9393
settings.checkversion.title = Verificar a nova vers\u00e3o na inicializa\u00e7\u00e3o
94+
settings.plainJSON.title= Plain JSON keys
9495

9596
swing.action.copy = Copiar
9697
swing.action.cut = Cortar

0 commit comments

Comments
 (0)
This repository has been archived.