add basic support for json array values
This commit is contained in:
parent
433cf1183f
commit
dff48382c2
@ -4,6 +4,7 @@
|
||||
|
||||
## [Unreleased]
|
||||
### Added
|
||||
- Basic support for json array values
|
||||
- Settings option to opt-out code assistance inside editor
|
||||
- Kotlin support for key completion and annotation
|
||||
|
||||
|
51
src/main/java/de/marhali/easyi18n/util/JsonArrayUtil.java
Normal file
51
src/main/java/de/marhali/easyi18n/util/JsonArrayUtil.java
Normal file
@ -0,0 +1,51 @@
|
||||
package de.marhali.easyi18n.util;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Utility methods to read and write json arrays.
|
||||
* @author marhali
|
||||
*/
|
||||
public class JsonArrayUtil {
|
||||
|
||||
public static String ARRAY_PREFIX = "!arr[";
|
||||
public static String ARRAY_SUFFIX = "]";
|
||||
public static char ARRAY_DELIMITER = ';';
|
||||
|
||||
public static String read(JsonArray array) {
|
||||
StringBuilder builder = new StringBuilder(ARRAY_PREFIX);
|
||||
|
||||
for(int i = 0; i < array.size(); i++) {
|
||||
if(i > 0) {
|
||||
builder.append(ARRAY_DELIMITER);
|
||||
}
|
||||
|
||||
String value = array.get(i).getAsString().replace(";", "\\;");
|
||||
builder.append(StringUtil.escapeControls(value, true));
|
||||
}
|
||||
|
||||
builder.append(ARRAY_SUFFIX);
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static JsonArray write(String concat) {
|
||||
concat = concat.substring(ARRAY_PREFIX.length(), concat.length() - ARRAY_SUFFIX.length());
|
||||
String regex = "(?<!\\\\)" + Pattern.quote(String.valueOf(ARRAY_DELIMITER));
|
||||
|
||||
JsonArray array = new JsonArray();
|
||||
|
||||
for(String element : concat.split(regex)) {
|
||||
element = element.replace("\\" + ARRAY_DELIMITER, String.valueOf(ARRAY_DELIMITER));
|
||||
array.add(StringEscapeUtils.unescapeJava(element));
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
public static boolean isArray(String concat) {
|
||||
return concat != null && concat.startsWith(ARRAY_PREFIX) && concat.endsWith(ARRAY_SUFFIX);
|
||||
}
|
||||
}
|
@ -27,9 +27,14 @@ public class JsonUtil {
|
||||
public static void writeTree(String locale, JsonObject parent, LocalizedNode node) {
|
||||
if(node.isLeaf() && !node.getKey().equals(LocalizedNode.ROOT_KEY)) {
|
||||
if(node.getValue().get(locale) != null) {
|
||||
|
||||
if(JsonArrayUtil.isArray(node.getValue().get(locale))) {
|
||||
parent.add(node.getKey(), JsonArrayUtil.write(node.getValue().get(locale)));
|
||||
} else {
|
||||
String value = StringEscapeUtils.unescapeJava(node.getValue().get(locale));
|
||||
parent.add(node.getKey(), new JsonPrimitive(value));
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
for(LocalizedNode children : node.getChildren()) {
|
||||
@ -78,7 +83,11 @@ public class JsonUtil {
|
||||
}
|
||||
|
||||
Map<String, String> messages = leafNode.getValue();
|
||||
String value = StringUtil.escapeControls(entry.getValue().getAsString(), true);
|
||||
|
||||
String value = entry.getValue().isJsonArray()
|
||||
? JsonArrayUtil.read(entry.getValue().getAsJsonArray())
|
||||
: StringUtil.escapeControls(entry.getValue().getAsString(), true);
|
||||
|
||||
messages.put(locale, value);
|
||||
leafNode.setValue(messages);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user