diff --git a/src/main/java/de/vette/idea/neos/actions/TranslateNodeTypeAction.java b/src/main/java/de/vette/idea/neos/actions/TranslateNodeTypeAction.java index fafe048..f490138 100644 --- a/src/main/java/de/vette/idea/neos/actions/TranslateNodeTypeAction.java +++ b/src/main/java/de/vette/idea/neos/actions/TranslateNodeTypeAction.java @@ -69,8 +69,17 @@ public void actionPerformed(@NotNull AnActionEvent e) { return; } + Collection locales = Settings.getInstance(project).locales; + + if (locales.isEmpty()) { + showNotification(project); + return; + } + List pairs = getTranslateablePaths(virtualFile, project); - String nodeType = extractNodeType(pairs); + if (pairs.isEmpty()) { + return; + } PsiDirectory psiDirectory = PsiManager.getInstance(project).findDirectory(virtualFile.getParent()); JsonFile composerManifest = ComposerUtil.getComposerManifest(psiDirectory); @@ -81,12 +90,6 @@ public void actionPerformed(@NotNull AnActionEvent e) { String packageName = ComposerUtil.getPackageKey(composerManifest); packageDirectory = composerManifest.getContainingDirectory(); - Collection locales = Settings.getInstance(project).locales; - - if (locales.isEmpty()) { - showNotification(project); - return; - } final Optional firstLocaleOpt = locales.stream().findFirst(); if (firstLocaleOpt.isEmpty()) { @@ -99,20 +102,25 @@ public void actionPerformed(@NotNull AnActionEvent e) { properties.setProperty("NEOS_PACKAGE_NAME", packageName); properties.setProperty("SOURCE_LANGUAGE", sourceLocale); - HashMap yamlPairsByTransId = extractNodeTypeTranslationIds(pairs); + HashMap yamlPairsByTransId = extractNodeTypeTranslationIds(pairs); WriteCommandAction.runWriteCommandAction(project, "Translate NodeType", "", () -> { - for (String locale : locales) { - PsiDirectory translationDir = createTranslationDirectories(packageDirectory, locale, nodeType); - processTranslationFile(project, translationDir, locale, sourceLocale, yamlPairsByTransId, properties, nodeType); - } + yamlPairsByTransId.forEach((nodeType, pairsByTransId) -> { + String[] nodeTypeParts = nodeType.split(":"); + String nodeTypeWithoutNamespace = (nodeTypeParts.length == 2) ? nodeTypeParts[1] : nodeType; - // replace text values in node type defintion by "i18n" - yamlPairsByTransId.forEach((transId, yamlKeyValue) -> { - if (!yamlKeyValue.getValueText().equals("i18n")) { - YAMLKeyValue newKeyValue = YAMLElementGenerator.getInstance(project).createYamlKeyValue(yamlKeyValue.getKeyText(), "i18n"); - yamlKeyValue.replace(newKeyValue); + for (String locale : locales) { + PsiDirectory translationDir = createTranslationDirectories(packageDirectory, locale, nodeTypeWithoutNamespace); + processTranslationFile(project, translationDir, locale, sourceLocale, pairsByTransId, properties, nodeTypeWithoutNamespace); } + + // replace text values in node type definition by "i18n" + pairsByTransId.forEach((transId, yamlKeyValue) -> { + if (!yamlKeyValue.getValueText().equals("i18n")) { + YAMLKeyValue newKeyValue = YAMLElementGenerator.getInstance(project).createYamlKeyValue(yamlKeyValue.getKeyText(), "i18n"); + yamlKeyValue.replace(newKeyValue); + } + }); }); }); } @@ -162,12 +170,6 @@ private String getFileName(String nodeType) { return nodeTypeParts[nodeTypeParts.length - 1]; } - private String extractNodeType(List pairs) { - String nodeType = getTranslationKeyParts(pairs.get(0)).get(0); - String[] nodeTypeParts = nodeType.split(":"); - return (nodeTypeParts.length == 2) ? nodeTypeParts[1] : nodeType; - } - private PsiDirectory createTranslationDirectories(PsiDirectory baseDir, String locale, String nodeType) throws IncorrectOperationException { PsiDirectory dir = createSubdirectory(baseDir, "Resources"); dir = createSubdirectory(dir, "Private"); @@ -206,14 +208,25 @@ private Set getExistingIdsForFile(PsiDirectory dir, String fileName) { return getExistingIds(bodyTag); } - private LinkedHashMap extractNodeTypeTranslationIds(List pairs) { - LinkedHashMap nodeTypeIds = new LinkedHashMap<>(); + class PairsByTransId extends LinkedHashMap {} + + private LinkedHashMap extractNodeTypeTranslationIds(List pairs) { + LinkedHashMap nodeTypeIds = new LinkedHashMap<>(); + for (YAMLKeyValue pair : pairs) { List path = getTranslationKeyParts(pair); + String nodeType = path.get(0); + path = path.subList(1, path.size()); // removes the node type name + + if (!nodeTypeIds.containsKey(nodeType)) { + nodeTypeIds.put(nodeType, new PairsByTransId()); + } + String id = String.join(".", path); - nodeTypeIds.put(id, pair); + nodeTypeIds.get(nodeType).put(id, pair); } + return nodeTypeIds; }