Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import javax.annotation.PostConstruct;

import com.adobe.cq.wcm.core.components.util.AbstractComponentImpl;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.LanguageManager;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
Expand All @@ -37,7 +39,11 @@
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.designer.Style;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.ResourceBundle;

/**
* Search model implementation.
Expand Down Expand Up @@ -113,6 +119,12 @@ public class SearchImpl extends AbstractComponentImpl implements Search {
*/
private String searchRootPagePath;

/**
* The localized messages.
*/
private final Map<String, String> i18nMessagesMap = new HashMap<>();


/**
* Initialize the model.
*/
Expand Down Expand Up @@ -161,4 +173,18 @@ public String getExportedType() {
return request.getResource().getResourceType();
}

public String getI18nMessages() {
Locale pageLocale = currentPage.getLanguage(false);
ResourceBundle resourceBundle = request.getResourceBundle(pageLocale);
I18n i18n = new I18n(resourceBundle);
i18nMessagesMap.put("{0} result", i18n.get("{0} result"));
i18nMessagesMap.put("{0} results", i18n.get("{0} results"));
i18nMessagesMap.put("No results", i18n.get("No results"));
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(i18nMessagesMap);
} catch (Exception e) {
return "{}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,24 @@
}
}

function localizeMessage(searchElementId, message, values = []) {
const i18nMessages = JSON.parse(document.querySelector("#" + searchElementId).getAttribute("data-i18n-messages"));
let translation = i18nMessages[message];
// Insert values
if (values.length > 0) {
translation = translation.replace(/{(\d+)}/g, function(match, index) {
return typeof values[index] !== "undefined" ? values[index] : match;
});
}
return translation;
}

// useful for Accessibility, helping users with low vision and users with cognitive disabilities to identify the change in results
function updateSearchResultsStatusMessageElement(searchElementId, totalResults) {
var searchResultsStatusMessage = document.querySelector("#" + searchElementId + "> .cmp_search__info");
searchResultsStatusMessage.style.visibility = "visible";
var searchResultsFoundMessage = totalResults === 1 ? totalResults + " result" : totalResults + " results";
var searchResultsNotFoundMessage = "No results";
var searchResultsFoundMessage = localizeMessage(searchElementId, totalResults === 1 ? "{0} result" : "{0} results", [totalResults]);
var searchResultsNotFoundMessage = localizeMessage(searchElementId, "No results");
searchResultsStatusMessage.innerText = totalResults > 0 ? searchResultsFoundMessage : searchResultsNotFoundMessage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
role="search"
data-cmp-is="search"
data-cmp-min-length="${search.searchTermMinimumLength}"
data-cmp-results-size="${search.resultsSize}">
data-cmp-results-size="${search.resultsSize}"
data-i18n-messages='${search.i18nMessages}'>
<div class="cmp_search__info" aria-live="polite" role="status"></div>
<form class="cmp-search__form" data-cmp-hook-search="form"
method="get" action="${currentPage.path @ addSelectors=['searchresults'], extension='json', suffix = search.relativePath}"
Expand Down
Loading