Fix empty keys & NPE on new locale creation
- Properly represent keys when message definition (for the specific locale) is not present - When new locale files were added - IntelliJ needs to save the application state to make the content available for the plugin
This commit is contained in:
parent
76c94f3c48
commit
f97c184d5e
@ -26,6 +26,8 @@ public class JsonTranslatorIO implements TranslatorIO {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void read(@NotNull String directoryPath, @NotNull Consumer<Translations> callback) {
|
public void read(@NotNull String directoryPath, @NotNull Consumer<Translations> callback) {
|
||||||
|
ApplicationManager.getApplication().saveAll(); // Save opened files (required if new locales were added)
|
||||||
|
|
||||||
ApplicationManager.getApplication().runReadAction(() -> {
|
ApplicationManager.getApplication().runReadAction(() -> {
|
||||||
VirtualFile directory = LocalFileSystem.getInstance().findFileByIoFile(new File(directoryPath));
|
VirtualFile directory = LocalFileSystem.getInstance().findFileByIoFile(new File(directoryPath));
|
||||||
|
|
||||||
@ -59,40 +61,47 @@ public class JsonTranslatorIO implements TranslatorIO {
|
|||||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||||
|
|
||||||
ApplicationManager.getApplication().runWriteAction(() -> {
|
ApplicationManager.getApplication().runWriteAction(() -> {
|
||||||
|
try {
|
||||||
for(String locale : translations.getLocales()) {
|
for(String locale : translations.getLocales()) {
|
||||||
JsonElement content = writeTree(locale, new JsonObject(), translations.getNodes());
|
JsonObject content = new JsonObject();
|
||||||
|
writeTree(locale, content, translations.getNodes());
|
||||||
|
//JsonElement content = writeTree(locale, new JsonObject(), translations.getNodes());
|
||||||
|
|
||||||
String fullPath = directoryPath + "/" + locale + "." + FILE_EXTENSION;
|
String fullPath = directoryPath + "/" + locale + "." + FILE_EXTENSION;
|
||||||
VirtualFile file = LocalFileSystem.getInstance().findFileByIoFile(new File(fullPath));
|
VirtualFile file = LocalFileSystem.getInstance().findFileByIoFile(new File(fullPath));
|
||||||
|
|
||||||
try {
|
|
||||||
file.setBinaryContent(gson.toJson(content).getBytes());
|
file.setBinaryContent(gson.toJson(content).getBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Successfully saved
|
||||||
callback.accept(true);
|
callback.accept(true);
|
||||||
|
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
callback.accept(false);
|
callback.accept(false);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonElement writeTree(String locale, JsonObject parent, LocalizedNode data) {
|
private void writeTree(String locale, JsonObject parent, LocalizedNode node) {
|
||||||
if(data.isLeaf() && !data.getKey().equals(LocalizedNode.ROOT_KEY)) {
|
if(node.isLeaf() && !node.getKey().equals(LocalizedNode.ROOT_KEY)) {
|
||||||
if(data.getValue().get(locale) != null) { // Translation is defined - track it
|
if(node.getValue().get(locale) != null) {
|
||||||
return new JsonPrimitive(data.getValue().get(locale));
|
parent.add(node.getKey(), new JsonPrimitive(node.getValue().get(locale)));
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
for(LocalizedNode children : data.getChildren()) {
|
for(LocalizedNode children : node.getChildren()) {
|
||||||
JsonObject childrenObject = new JsonObject();
|
if(children.isLeaf()) {
|
||||||
parent.add(children.getKey(), childrenObject);
|
writeTree(locale, parent, children);
|
||||||
|
} else {
|
||||||
parent.add(children.getKey(), writeTree(locale, childrenObject, children));
|
JsonObject childrenJson = new JsonObject();
|
||||||
|
writeTree(locale, childrenJson, children);
|
||||||
|
if(childrenJson.size() > 0) {
|
||||||
|
parent.add(children.getKey(), childrenJson);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return parent;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void readTree(String locale, JsonObject json, LocalizedNode data) {
|
private void readTree(String locale, JsonObject json, LocalizedNode data) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user