Skip to content

Commit

Permalink
Merge pull request #40 from orbinson/feature/add-non-default-languages
Browse files Browse the repository at this point in the history
Feature/add non default languages
  • Loading branch information
royteeuwen authored Sep 16, 2024
2 parents e6a9da8 + ccfaa6f commit c126594
Show file tree
Hide file tree
Showing 45 changed files with 775 additions and 750 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package be.orbinson.aem.dictionarytranslator.exception;

public class DictionaryException extends Exception {
public DictionaryException(String message) {
super(message);
}

public DictionaryException(String message, Throwable cause) {
super(message, cause);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ public interface Dictionary {

boolean isEditable();

int getLabelCount();
int getKeyCount();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

import javax.inject.Named;
import java.text.DateFormat;
import java.util.*;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Model(
adaptables = SlingHttpServletRequest.class,
Expand Down Expand Up @@ -85,25 +87,12 @@ public String getBasename() {
}

@Override
public int getLabelCount() {
public int getKeyCount() {
return getKeys().size();
}

@Override
public List<String> getKeys() {
Set<String> keys = new TreeSet<>();
for (String language : dictionaryService.getLanguages(resource)) {
Resource child = resource.getChild(language);
if (child != null) {
child.listChildren().forEachRemaining(item -> addKey(keys, item));
}
}
return List.copyOf(keys);
}

private static void addKey(Set<String> keys, Resource item) {
if (item.isResourceType("sling:MessageEntry")) {
keys.add(item.getName());
}
return dictionaryService.getKeys(resource);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
package be.orbinson.aem.dictionarytranslator.services;

import be.orbinson.aem.dictionarytranslator.exception.DictionaryException;
import com.day.cq.replication.ReplicationException;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;

import javax.jcr.RepositoryException;
import java.util.List;
import java.util.Map;

public interface DictionaryService {
Map<String, String> getLanguagesForPath(ResourceResolver resourceResolver, String path);

List<Resource> getDictionaries(ResourceResolver resourceResolver);

List<String> getLanguages(Resource resource);
void createDictionary(Resource parent, String name, String[] languages, String basename) throws PersistenceException;

void deleteDictionary(ResourceResolver resourceResolver, String dictionaryPath) throws DictionaryException;

List<String> getLanguages(Resource dictionaryResource);

void deleteLanguage(ResourceResolver resourceResolver, Resource dictionaryResource, String language) throws DictionaryException;

void addLanguage(Resource dictionaryResource, String language, String basename) throws PersistenceException;

Resource getLanguageResource(Resource dictionaryResource, String language);

Map<String, String> getLanguagesForPath(ResourceResolver resourceResolver, String dictionaryPath);

String getBasename(Resource dictionaryResource);

void createDictionary(Resource parent, String name, String[] languages, String basename) throws PersistenceException;
List<String> getKeys(Resource dictionaryResource);

boolean keyExists(Resource dictionaryResource, String language, String key);

Resource getMessageEntryResource(Resource languageResource, String key);

void createMessageEntry(ResourceResolver resourceResolver, Resource dictionaryResource, String language, String key, String message) throws PersistenceException;

void updateMessageEntry(ResourceResolver resourceResolver, Resource dictionaryResource, String language, String key, String message) throws PersistenceException, RepositoryException;

void addLanguage(Resource dictionary, String language, String basename) throws PersistenceException;
void deleteMessageEntry(ResourceResolver resourceResolver, Resource combiningMessageEntryResource) throws PersistenceException, ReplicationException;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package be.orbinson.aem.dictionarytranslator.services.impl;

import be.orbinson.aem.dictionarytranslator.services.DictionaryService;
import be.orbinson.aem.dictionarytranslator.utils.DictionaryConstants;
import com.adobe.granite.ui.components.ds.ValueMapResource;
import com.day.text.Text;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.apache.sling.spi.resource.provider.ResolveContext;
import org.apache.sling.spi.resource.provider.ResourceContext;
import org.apache.sling.spi.resource.provider.ResourceProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import java.util.*;

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

@Component(
service = ResourceProvider.class,
property = {
ResourceProvider.PROPERTY_ROOT + "=" + CombiningMessageEntryResourceProvider.ROOT
}
)
public class CombiningMessageEntryResourceProvider extends ResourceProvider<Object> {

public static final String ROOT = "/mnt/dictionary";

public static final String RESOURCE_TYPE = "aem-dictionary-translator/components/combining-message-entry";

public static final String KEY = "key";
public static final String DICTIONARY_PATH = "dictionaryPath";
public static final String LANGUAGES = "languages";
public static final String MESSAGE_ENTRY_PATHS = "messageEntryPaths";

@Reference
private DictionaryService dictionaryService;

private Map<String, Object> getValuesAndMessageEntryPaths(Resource dictionaryResource, String key, List<String> languages) {
Map<String, Object> properties = new HashMap<>();
List<String> messageEntryPaths = new ArrayList<>();

for (String language : languages) {
Resource languageResource = dictionaryService.getLanguageResource(dictionaryResource, language);
if (languageResource != null) {
Resource messageEntryResource = dictionaryService.getMessageEntryResource(languageResource, key);
if (messageEntryResource != null && messageEntryResource.isResourceType(DictionaryConstants.SLING_MESSAGEENTRY)) {
properties.put(language, messageEntryResource.getValueMap().get(DictionaryConstants.SLING_MESSAGE, ""));
messageEntryPaths.add(messageEntryResource.getPath());
}
}
}

properties.put(MESSAGE_ENTRY_PATHS, messageEntryPaths);

return properties;
}

@Override
public @Nullable Resource getResource(@NotNull ResolveContext<Object> ctx, @NotNull String path, @NotNull ResourceContext resourceContext, @Nullable Resource parent) {
ResourceResolver resourceResolver = ctx.getResourceResolver();
if (!path.startsWith(ROOT) || ROOT.equals(path)) {
// Not applying combining message entry resource provider
return null;
}

String key = Text.getName(path);
String dictionaryPath = Text.getRelativeParent(path, 1).replaceFirst(ROOT, "");
Resource dictionaryResource = resourceResolver.getResource(dictionaryPath);

if (dictionaryResource != null && isMessageEntry(dictionaryResource, key)) {
Map<String, Object> properties = new HashMap<>();
properties.put(KEY, key);
properties.put("path", path);
properties.put(DICTIONARY_PATH, dictionaryPath);
List<String> languages = dictionaryService.getLanguages(dictionaryResource);
properties.put(LANGUAGES, languages);
properties.putAll(getValuesAndMessageEntryPaths(dictionaryResource, key, languages));
return new ValueMapResource(resourceResolver, path, RESOURCE_TYPE, new ValueMapDecorator(properties));
}
return null;
}

private boolean isMessageEntry(Resource dictionaryResource, String key) {
List<String> languages = dictionaryService.getLanguages(dictionaryResource);
// in order to speed things up always start with language "en" (if existing)
String mostCompleteLanguage = Locale.ENGLISH.getLanguage();
if (languages.contains(mostCompleteLanguage)) {
languages.remove(mostCompleteLanguage);
languages.add(0, mostCompleteLanguage);
}

if (!languages.isEmpty()) {
for (String language : languages) {
Resource languageResource = dictionaryService.getLanguageResource(dictionaryResource, language);
if (languageResource != null) {
Resource messageEntryResource = dictionaryService.getMessageEntryResource(languageResource, key);
if (messageEntryResource != null && messageEntryResource.isResourceType(SLING_MESSAGEENTRY)) {
return true;
}
}
}
}
return false;
}

@Override
public @Nullable Iterator<Resource> listChildren(@NotNull ResolveContext<Object> ctx, @NotNull Resource parent) {
return Collections.emptyIterator();
}

}
Loading

0 comments on commit c126594

Please sign in to comment.