-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from orbinson/feature/add-non-default-languages
Feature/add non default languages
- Loading branch information
Showing
45 changed files
with
775 additions
and
750 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
core/src/main/java/be/orbinson/aem/dictionarytranslator/exception/DictionaryException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
...in/java/be/orbinson/aem/dictionarytranslator/exception/DictionaryTranslatorException.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,5 +24,5 @@ public interface Dictionary { | |
|
||
boolean isEditable(); | ||
|
||
int getLabelCount(); | ||
int getKeyCount(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 26 additions & 4 deletions
30
core/src/main/java/be/orbinson/aem/dictionarytranslator/services/DictionaryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
166 changes: 0 additions & 166 deletions
166
core/src/main/java/be/orbinson/aem/dictionarytranslator/services/LabelResourceProvider.java
This file was deleted.
Oops, something went wrong.
115 changes: 115 additions & 0 deletions
115
...rbinson/aem/dictionarytranslator/services/impl/CombiningMessageEntryResourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
Oops, something went wrong.