add support for array values
This commit is contained in:
parent
cdd7188769
commit
a01b817d3b
@ -0,0 +1,23 @@
|
||||
package de.marhali.easyi18n.io.properties;
|
||||
|
||||
import de.marhali.easyi18n.io.ArrayMapper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Map for 'properties' array values.
|
||||
* @author marhali
|
||||
*/
|
||||
public class PropertiesArrayMapper extends ArrayMapper {
|
||||
public static String read(String[] list) {
|
||||
return read(Arrays.stream(list).iterator(), Object::toString);
|
||||
}
|
||||
|
||||
public static String[] write(String concat) {
|
||||
List<String> list = new ArrayList<>();
|
||||
write(concat, list::add);
|
||||
return (String[]) list.toArray();
|
||||
}
|
||||
}
|
@ -2,9 +2,10 @@ package de.marhali.easyi18n.io.properties;
|
||||
|
||||
import de.marhali.easyi18n.model.Translation;
|
||||
import de.marhali.easyi18n.model.TranslationData;
|
||||
import de.marhali.easyi18n.model.TranslationNode;
|
||||
import de.marhali.easyi18n.util.StringUtil;
|
||||
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@ -14,20 +15,20 @@ import java.util.Map;
|
||||
*/
|
||||
public class PropertiesMapper {
|
||||
|
||||
// TODO: support array values
|
||||
|
||||
public static void read(String locale, SortableProperties properties, TranslationData data) {
|
||||
for(Map.Entry<Object, Object> entry : properties.entrySet()) {
|
||||
String key = String.valueOf(entry.getKey());
|
||||
String content = StringUtil.escapeControls(String.valueOf(entry.getValue()), true);
|
||||
Object value = entry.getValue();
|
||||
|
||||
Translation translation = data.getTranslation(key);
|
||||
TranslationNode childNode = data.getRootNode().getOrCreateChildren(key);
|
||||
Translation translation = childNode.getValue();
|
||||
|
||||
if(translation == null) {
|
||||
translation = new Translation();
|
||||
}
|
||||
String content = value instanceof String[]
|
||||
? PropertiesArrayMapper.read((String[]) value)
|
||||
: StringUtil.escapeControls(String.valueOf(value), true);
|
||||
|
||||
translation.put(locale, content);
|
||||
childNode.setValue(translation);
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,9 +37,16 @@ public class PropertiesMapper {
|
||||
Translation translation = data.getTranslation(key);
|
||||
|
||||
if(translation != null && translation.containsKey(locale)) {
|
||||
String content = StringEscapeUtils.unescapeJava(translation.get(locale));
|
||||
String content = translation.get(locale);
|
||||
|
||||
if(PropertiesArrayMapper.isArray(content)) {
|
||||
properties.put(key, PropertiesArrayMapper.write(content));
|
||||
} else if(NumberUtils.isNumber(content)) {
|
||||
properties.put(key, NumberUtils.createNumber(content));
|
||||
} else {
|
||||
properties.put(key, content);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user