Skip to content

Commit

Permalink
Merge pull request #12 from WietseV/issues/#9-empty-slingmessage
Browse files Browse the repository at this point in the history
Don't set sling:message when a label is empty
  • Loading branch information
bdhoine authored Feb 1, 2024
2 parents 4645731 + 69e8292 commit 47e1cf3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Dictionaries with .json files are no longer showed in dictionary
list: [#8](https://github.com/orbinson/aem-dictionary-translator/pull/5)
- Labels with no translation for a specific language no longer have a
sling:message so the value won't be empty but will have a fallback
from another language or the key itself: [#12](https://github.com/orbinson/aem-dictionary-translator/pull/12)

### Added

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_KEY;
import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGE;
import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGEENTRY;
import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;

@Component(service = Servlet.class)
@SlingServletResourceTypes(
resourceSuperType = "granite/ui/components/coral/foundation/form",
Expand Down Expand Up @@ -70,11 +76,13 @@ private void addMessage(ResourceResolver resourceResolver, Resource dictionary,

if (resource != null) {
String path = resource.getPath();
resourceResolver.create(resource, JcrUtil.createValidName(key), Map.of(
"jcr:primaryType", "sling:MessageEntry",
"sling:key", key,
"sling:message", message
));
Map<String, Object> properties = new HashMap<>();
properties.put(JCR_PRIMARYTYPE, SLING_MESSAGEENTRY);
properties.put(SLING_KEY, key);
if (!message.isBlank()){
properties.put(SLING_MESSAGE, message);
}
resourceResolver.create(resource, JcrUtil.createValidName(key), properties);
LOG.trace("Create label with key '{}' and message '{}' on path '{}'", key, message, path);
resourceResolver.commit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGE;
import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGEENTRY;

@Component(service = Servlet.class)
@SlingServletResourceTypes(
resourceSuperType = "granite/ui/components/coral/foundation/form",
Expand Down Expand Up @@ -88,8 +91,12 @@ private void addMessage(ResourceResolver resourceResolver, Resource dictionary,
if (labelResource != null) {
ValueMap valueMap = labelResource.adaptTo(ModifiableValueMap.class);
if (valueMap != null) {
valueMap.put("sling:message", message);
LOG.trace("Updated label with name '{}' and message '{}' on path '{}'", name, message, labelResource.getPath());
if (message.isBlank()) {
valueMap.remove(SLING_MESSAGE);
} else {
valueMap.put(SLING_MESSAGE, message);
LOG.trace("Updated label with name '{}' and message '{}' on path '{}'", name, message, labelResource.getPath());
}
}
}
resourceResolver.commit();
Expand All @@ -99,10 +106,10 @@ private void addMessage(ResourceResolver resourceResolver, Resource dictionary,
}
}

private Resource getLabelResource(ResourceResolver resourceResolver, Resource languageResource, String name) throws RepositoryException {
public Resource getLabelResource(ResourceResolver resourceResolver, Resource languageResource, String name) throws RepositoryException {
if (languageResource.getChild(name) == null) {
Session session = resourceResolver.adaptTo(Session.class);
JcrUtil.createPath(languageResource.getPath() + "/" + name, "sling:MessageEntry", session);
JcrUtil.createPath(languageResource.getPath() + "/" + name, SLING_MESSAGEENTRY, session);
session.save();
}
return languageResource.getChild(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
import java.io.IOException;
import java.util.Map;

import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_KEY;
import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGE;
import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGEENTRY;
import static junit.framework.Assert.assertNull;
import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

Expand Down Expand Up @@ -84,8 +89,33 @@ void doPostWithValidParams() throws ServletException, IOException {
Resource resource = context.resourceResolver().getResource("/content/dictionaries/i18n/en/greeting");
ValueMap properties = resource.getValueMap();
assertNotNull(resource);
assertEquals("sling:MessageEntry", properties.get("jcr:primaryType"));
assertEquals("greeting", properties.get("sling:key"));
assertEquals("Hello", properties.get("sling:message"));
assertEquals(SLING_MESSAGEENTRY, properties.get(JCR_PRIMARYTYPE));
assertEquals("greeting", properties.get(SLING_KEY));
assertEquals("Hello", properties.get(SLING_MESSAGE));
}

@Test
void doPostWithEmptyMessage() throws ServletException, IOException {
context.create().resource("/content/dictionaries/i18n/en", Map.of("jcr:language", "en"));
context.create().resource("/content/dictionaries/i18n/fr", Map.of("jcr:language", "fr"));

context.request().setMethod("POST");
context.request().setParameterMap(Map.of(
"dictionary", "/content/dictionaries/i18n",
"key", "greeting",
"en", "Hello",
"fr", ""
));

servlet.service(context.request(), context.response());

assertEquals(HttpServletResponse.SC_OK, context.response().getStatus());

Resource resource = context.resourceResolver().getResource("/content/dictionaries/i18n/fr/greeting");
ValueMap properties = resource.getValueMap();
assertNotNull(resource);
assertEquals(SLING_MESSAGEENTRY, properties.get(JCR_PRIMARYTYPE));
assertEquals("greeting", properties.get(SLING_KEY));
assertNull(properties.get(SLING_MESSAGE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import java.io.IOException;
import java.util.Map;

import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_KEY;
import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGE;
import static be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants.SLING_MESSAGEENTRY;
import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

Expand Down Expand Up @@ -72,12 +76,12 @@ void doPostWithValidParams() throws ServletException, IOException {
context.create().resource("/content/dictionaries/i18n/en", Map.of("jcr:language", "en"));
context.create().resource("/content/dictionaries/i18n/en/appel", Map.of(
"dictionary", "/content/dictionaries/i18n", "key", "appel",
"en", "apple", "sling:MessageEntry", "jcr:primaryType")
"en", "apple", SLING_MESSAGEENTRY, JCR_PRIMARYTYPE)
);
context.create().resource("/content/dictionaries/i18n/fr", Map.of("jcr:language", "fr"));
context.create().resource("/content/dictionaries/i18n/en/appel", Map.of(
"dictionary", "/content/dictionaries/i18n", "key", "appel",
"fr", "pomme", "sling:MessageEntry", "jcr:primaryType")
"fr", "pomme", SLING_MESSAGEENTRY, JCR_PRIMARYTYPE)
);

context.request().setMethod("POST");
Expand All @@ -95,8 +99,8 @@ void doPostWithValidParams() throws ServletException, IOException {
Resource resource = context.resourceResolver().getResource("/content/dictionaries/i18n/en/appel");
ValueMap properties = resource.getValueMap();
assertNotNull(resource);
assertEquals("sling:MessageEntry", properties.get("jcr:primaryType"));
assertEquals("appel", properties.get("sling:key"));
assertEquals("Hello", properties.get("sling:message"));
assertEquals(SLING_MESSAGEENTRY, properties.get(JCR_PRIMARYTYPE));
assertEquals("appel", properties.get(SLING_KEY));
assertEquals("Hello", properties.get(SLING_MESSAGE));
}
}

0 comments on commit 47e1cf3

Please sign in to comment.