add simple array support for yaml locale files
This commit is contained in:
parent
2801b0f0ac
commit
8bbd2698d2
@ -1,13 +1,16 @@
|
|||||||
package de.marhali.easyi18n.io.implementation;
|
package de.marhali.easyi18n.io.implementation;
|
||||||
|
|
||||||
import com.google.gson.*;
|
|
||||||
import com.intellij.openapi.application.*;
|
import com.intellij.openapi.application.*;
|
||||||
import com.intellij.openapi.project.*;
|
import com.intellij.openapi.project.*;
|
||||||
import com.intellij.openapi.vfs.*;
|
import com.intellij.openapi.vfs.*;
|
||||||
|
|
||||||
import de.marhali.easyi18n.io.*;
|
import de.marhali.easyi18n.io.*;
|
||||||
import de.marhali.easyi18n.model.*;
|
import de.marhali.easyi18n.model.*;
|
||||||
import de.marhali.easyi18n.util.*;
|
import de.marhali.easyi18n.util.*;
|
||||||
|
import de.marhali.easyi18n.util.array.YamlArrayUtil;
|
||||||
|
|
||||||
import org.jetbrains.annotations.*;
|
import org.jetbrains.annotations.*;
|
||||||
|
|
||||||
import thito.nodeflow.config.*;
|
import thito.nodeflow.config.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@ -68,9 +71,14 @@ public class YamlTranslatorIO implements TranslatorIO {
|
|||||||
if (map != null) {
|
if (map != null) {
|
||||||
load(locale, finalChild, map);
|
load(locale, finalChild, map);
|
||||||
} else {
|
} else {
|
||||||
String value = section.getString(key).orElse(null);
|
|
||||||
if (value != null) {
|
if(section.isList(key) && section.getList(key).isPresent()) {
|
||||||
child.getValue().put(locale, value);
|
child.getValue().put(locale, YamlArrayUtil.read(section.getList(key).get()));
|
||||||
|
} else {
|
||||||
|
String value = section.getString(key).orElse(null);
|
||||||
|
if (value != null) {
|
||||||
|
child.getValue().put(locale, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -81,7 +89,7 @@ public class YamlTranslatorIO implements TranslatorIO {
|
|||||||
if (node.isLeaf() && !node.getKey().equals(LocalizedNode.ROOT_KEY)) {
|
if (node.isLeaf() && !node.getKey().equals(LocalizedNode.ROOT_KEY)) {
|
||||||
String value = node.getValue().get(locale);
|
String value = node.getValue().get(locale);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
section.set(path, value);
|
section.set(path, YamlArrayUtil.isArray(value) ? YamlArrayUtil.write(value) : value);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (LocalizedNode child : node.getChildren()) {
|
for (LocalizedNode child : node.getChildren()) {
|
||||||
|
@ -1,51 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -6,6 +6,7 @@ import com.google.gson.JsonPrimitive;
|
|||||||
|
|
||||||
import de.marhali.easyi18n.model.LocalizedNode;
|
import de.marhali.easyi18n.model.LocalizedNode;
|
||||||
|
|
||||||
|
import de.marhali.easyi18n.util.array.JsonArrayUtil;
|
||||||
import org.apache.commons.lang.StringEscapeUtils;
|
import org.apache.commons.lang.StringEscapeUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
58
src/main/java/de/marhali/easyi18n/util/array/ArrayUtil.java
Normal file
58
src/main/java/de/marhali/easyi18n/util/array/ArrayUtil.java
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package de.marhali.easyi18n.util.array;
|
||||||
|
|
||||||
|
import de.marhali.easyi18n.util.StringUtil;
|
||||||
|
import org.apache.commons.lang.StringEscapeUtils;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility methods for simple array support.
|
||||||
|
* @author marhali
|
||||||
|
*/
|
||||||
|
public abstract class ArrayUtil {
|
||||||
|
|
||||||
|
static final String PREFIX = "!arr[";
|
||||||
|
static final String SUFFIX = "]";
|
||||||
|
static final char DELIMITER = ';';
|
||||||
|
|
||||||
|
static final String SPLITERATOR_REGEX =
|
||||||
|
MessageFormat.format("(?<!\\\\){0}", Pattern.quote(String.valueOf(DELIMITER)));
|
||||||
|
|
||||||
|
static <T> String read(Iterator<T> elements, Function<T, String> stringFactory) {
|
||||||
|
StringBuilder builder = new StringBuilder(PREFIX);
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
while(elements.hasNext()) {
|
||||||
|
if(i > 0) {
|
||||||
|
builder.append(DELIMITER);
|
||||||
|
}
|
||||||
|
|
||||||
|
String value = stringFactory.apply(elements.next());
|
||||||
|
|
||||||
|
builder.append(StringUtil.escapeControls(
|
||||||
|
value.replace(String.valueOf(DELIMITER), "\\" + DELIMITER), true));
|
||||||
|
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.append(SUFFIX);
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write(String concat, Consumer<String> writeElement) {
|
||||||
|
concat = concat.substring(PREFIX.length(), concat.length() - SUFFIX.length());
|
||||||
|
|
||||||
|
for(String element : concat.split(SPLITERATOR_REGEX)) {
|
||||||
|
element = element.replace("\\" + DELIMITER, String.valueOf(DELIMITER));
|
||||||
|
writeElement.accept(StringEscapeUtils.unescapeJava(element));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isArray(String concat) {
|
||||||
|
return concat != null && concat.startsWith(PREFIX) && concat.endsWith(SUFFIX);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package de.marhali.easyi18n.util.array;
|
||||||
|
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility methods to read and write json arrays.
|
||||||
|
* @author marhali
|
||||||
|
*/
|
||||||
|
public class JsonArrayUtil extends ArrayUtil {
|
||||||
|
public static String read(JsonArray array) {
|
||||||
|
return read(array.iterator(), JsonElement::getAsString);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JsonArray write(String concat) {
|
||||||
|
JsonArray array = new JsonArray();
|
||||||
|
write(concat, array::add);
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package de.marhali.easyi18n.util.array;
|
||||||
|
|
||||||
|
import thito.nodeflow.config.ListSection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility methods to read and write yaml lists.
|
||||||
|
* @author marhali
|
||||||
|
*/
|
||||||
|
public class YamlArrayUtil extends ArrayUtil {
|
||||||
|
|
||||||
|
public static String read(ListSection list) {
|
||||||
|
return read(list.iterator(), Object::toString);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ListSection write(String concat) {
|
||||||
|
ListSection list = new ListSection();
|
||||||
|
write(concat, list::add);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
@ -197,6 +197,12 @@ public interface Section {
|
|||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default boolean isList(String path) {
|
||||||
|
Optional<?> o = getObject(path);
|
||||||
|
return o.isPresent() && o.get() instanceof List;
|
||||||
|
}
|
||||||
|
|
||||||
default Optional<ListSection> getList(String path) {
|
default Optional<ListSection> getList(String path) {
|
||||||
return getObject(path).map(o -> {
|
return getObject(path).map(o -> {
|
||||||
if (o instanceof List) {
|
if (o instanceof List) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user