From fe5367d6472c5d85b624d243c78d31b86b90f7ac Mon Sep 17 00:00:00 2001 From: marhali Date: Fri, 7 Jan 2022 18:20:32 +0100 Subject: [PATCH] support dots within a single path node Resolves #73 --- CHANGELOG.md | 3 +++ src/main/java/de/marhali/easyi18n/io/yaml/YamlMapper.java | 8 +++++++- src/main/java/de/marhali/easyi18n/util/PathUtil.java | 8 +++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16643d2..412efb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ # easy-i18n Changelog ## [Unreleased] +### Added +- Support for dots within key nodes in YAML files + ### Fixed - Key focus within tree or table view after translation change diff --git a/src/main/java/de/marhali/easyi18n/io/yaml/YamlMapper.java b/src/main/java/de/marhali/easyi18n/io/yaml/YamlMapper.java index 364ab33..07a078e 100644 --- a/src/main/java/de/marhali/easyi18n/io/yaml/YamlMapper.java +++ b/src/main/java/de/marhali/easyi18n/io/yaml/YamlMapper.java @@ -2,6 +2,7 @@ package de.marhali.easyi18n.io.yaml; import de.marhali.easyi18n.model.Translation; import de.marhali.easyi18n.model.TranslationNode; +import de.marhali.easyi18n.util.PathUtil; import de.marhali.easyi18n.util.StringUtil; import org.apache.commons.lang.StringEscapeUtils; @@ -23,6 +24,9 @@ public class YamlMapper { for(String key : section.getKeys()) { Object value = section.getInScope(key).get(); + key = StringUtil.escapeControls( + key.replace(PathUtil.DELIMITER, "\\" + PathUtil.DELIMITER), true); + TranslationNode childNode = node.getOrCreateChildren(key); if(value instanceof MapSection) { @@ -43,7 +47,9 @@ public class YamlMapper { public static void write(String locale, Section section, TranslationNode node) { for(Map.Entry entry : node.getChildren().entrySet()) { - String key = entry.getKey(); + String key = StringEscapeUtils.unescapeJava( + entry.getKey().replace("\\" + PathUtil.DELIMITER, PathUtil.DELIMITER)); + TranslationNode childNode = entry.getValue(); if(!childNode.isLeaf()) { diff --git a/src/main/java/de/marhali/easyi18n/util/PathUtil.java b/src/main/java/de/marhali/easyi18n/util/PathUtil.java index 64aee8e..a507531 100644 --- a/src/main/java/de/marhali/easyi18n/util/PathUtil.java +++ b/src/main/java/de/marhali/easyi18n/util/PathUtil.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.regex.Pattern; /** * Utility tool for split and merge translation key paths. @@ -18,7 +19,7 @@ import java.util.List; */ public class PathUtil { - public static final char DELIMITER = '.'; + public static final String DELIMITER = "."; private final boolean nestKeys; @@ -32,11 +33,12 @@ public class PathUtil { public @NotNull List split(@NotNull String path) { // Does not contain any sections or key nesting is disabled - if(!path.contains(String.valueOf(DELIMITER)) || !nestKeys) { + if(!path.contains(DELIMITER) || !nestKeys) { return new ArrayList<>(Collections.singletonList(path)); } - return new ArrayList<>(Arrays.asList(path.split("\\" + DELIMITER))); + return new ArrayList<>(Arrays.asList( + path.split("(? sections) {