-
Notifications
You must be signed in to change notification settings - Fork 9
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 #129 from UKGovLD/18-language-select
Language selection UI
- Loading branch information
Showing
10 changed files
with
350 additions
and
82 deletions.
There are no files selected for viewing
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
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
29 changes: 29 additions & 0 deletions
29
src/main/java/com/epimorphics/registry/language/Language.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,29 @@ | ||
package com.epimorphics.registry.language; | ||
|
||
/** | ||
* Representation of a supported language. | ||
*/ | ||
interface Language { | ||
/** | ||
* @return The ISO 639-1 language code. | ||
*/ | ||
String getCode(); | ||
|
||
/** | ||
* @return The user friendly label, in the corresponding language (if available). | ||
*/ | ||
String getLabel(); | ||
|
||
class Base implements Language { | ||
private final String code; | ||
private final String label; | ||
|
||
Base(String code, String label) { | ||
this.code = code; | ||
this.label = label; | ||
} | ||
|
||
@Override public String getCode() { return code; } | ||
@Override public String getLabel() { return label; } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/epimorphics/registry/language/LanguageConfig.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,13 @@ | ||
package com.epimorphics.registry.language; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Maintains the state of the registry's supported languages. | ||
*/ | ||
interface LanguageConfig { | ||
/** | ||
* @return The list of languages supported by the registry. | ||
*/ | ||
List<Language> getLanguages(); | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/epimorphics/registry/language/LanguageManager.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,32 @@ | ||
package com.epimorphics.registry.language; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* Multilingual registry support. | ||
*/ | ||
public interface LanguageManager { | ||
/** | ||
* Determine whether the registry is configured to support multiple languages. | ||
* @return True if and only if multiple languages are supported. | ||
*/ | ||
Boolean isMultilingual(); | ||
|
||
/** | ||
* @return All of the supported languages. | ||
*/ | ||
List<Language> getLanguages(); | ||
|
||
/** | ||
* Determines whether to use cookies to store users' language preference. | ||
* @return True if and only if cookies should be used. Otherwise, false. | ||
*/ | ||
Boolean getUseCookies(); | ||
|
||
class Default implements LanguageManager { | ||
@Override public Boolean isMultilingual() { return false; } | ||
@Override public List<Language> getLanguages() { return Collections.emptyList(); } | ||
@Override public Boolean getUseCookies() { return false; } | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/main/java/com/epimorphics/registry/language/LanguageRegister.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,97 @@ | ||
package com.epimorphics.registry.language; | ||
|
||
import com.epimorphics.appbase.core.App; | ||
import com.epimorphics.appbase.core.Startup; | ||
import com.epimorphics.registry.core.Description; | ||
import com.epimorphics.registry.core.Register; | ||
import com.epimorphics.registry.core.Registry; | ||
import com.epimorphics.registry.message.MessagingService; | ||
import com.epimorphics.registry.message.ProcessIfChanges; | ||
import com.epimorphics.registry.store.RegisterEntryInfo; | ||
import com.epimorphics.registry.store.StoreAPI; | ||
import com.epimorphics.registry.vocab.RegistryVocab; | ||
import org.apache.jena.rdf.model.Resource; | ||
import org.apache.jena.rdf.model.Statement; | ||
import org.apache.jena.vocabulary.RDF; | ||
import org.apache.jena.vocabulary.RDFS; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LanguageRegister implements LanguageConfig, Startup { | ||
private static final String REGISTER = "/system/language"; | ||
|
||
private Logger log = LoggerFactory.getLogger(LanguageRegister.class); | ||
private List<Language> languages = new ArrayList<>(); | ||
|
||
@Override public void startup(App app) { | ||
Registry reg = app.getA(Registry.class); | ||
initLanguages(reg); | ||
|
||
String register = reg.getBaseURI() + REGISTER; | ||
MessagingService msgSvc = reg.getMessagingService(); | ||
MessagingService.Process onMonitorChange = new ProcessIfChanges(msg -> initLanguages(reg), register); | ||
|
||
msgSvc.processMessages(onMonitorChange); | ||
} | ||
|
||
public synchronized void initLanguages(Registry reg) { | ||
List<Language> nextLanguages = new ArrayList<>(); | ||
|
||
String uri = reg.getBaseURI() + REGISTER; | ||
StoreAPI store = reg.getStore(); | ||
store.beginSafeRead(); | ||
|
||
try { | ||
Description desc = store.getDescription(uri); | ||
if (desc instanceof Register) { | ||
Register register = desc.asRegister(); | ||
List<RegisterEntryInfo> members = register.getMembers(); | ||
members.forEach(entry -> addLanguage(entry, store, nextLanguages)); | ||
this.languages = nextLanguages; | ||
} else { | ||
log.warn("System register " + uri + " does not exist - unable to configure languages."); | ||
} | ||
} finally { | ||
store.endSafeRead(); | ||
} | ||
} | ||
|
||
private void addLanguage(RegisterEntryInfo entry, StoreAPI store, List<Language> languages) { | ||
Resource root = store.getDescription(entry.getEntityURI()).getRoot(); | ||
if (root.hasProperty(RDF.type, RegistryVocab.Language)) { | ||
Statement langStmt = root.getProperty(RegistryVocab.languageCode); | ||
if (langStmt != null) { | ||
String lang = langStmt.getObject().asLiteral().getLexicalForm(); | ||
String label = getLabel(root, lang); | ||
languages.add(new Language.Base(lang, label)); | ||
log.info("Registered language: " + label + " (" + lang + ")"); | ||
} else { | ||
log.warn("Unable to add language entry " + entry.getItemURI() + ": Resource must specify a dbo:languageCode value."); | ||
} | ||
} else { | ||
log.warn("Unable to add language entry " + entry.getItemURI() + ": Resource must have a rdf:type value of dbo:Language."); | ||
} | ||
|
||
} | ||
|
||
private String getLabel(Resource root, String lang) { | ||
Statement nativeLabel = root.getProperty(RDFS.label, lang); | ||
if (nativeLabel != null) { | ||
return nativeLabel.getObject().asLiteral().getLexicalForm(); | ||
} | ||
|
||
Statement anyLabel = root.getProperty(RDFS.label); | ||
if (anyLabel != null) { | ||
return anyLabel.getObject().asLiteral().getLexicalForm(); | ||
} | ||
|
||
return lang; | ||
} | ||
|
||
public List<Language> getLanguages() { | ||
return languages; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/epimorphics/registry/language/MultiLanguageManager.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,28 @@ | ||
package com.epimorphics.registry.language; | ||
|
||
import java.util.List; | ||
|
||
public class MultiLanguageManager implements LanguageManager { | ||
private Boolean useCookies = false; | ||
private LanguageConfig config; | ||
|
||
public void setUseCookies(Boolean useCookies) { | ||
this.useCookies = useCookies; | ||
} | ||
|
||
public void setConfig(LanguageConfig config) { | ||
this.config = config; | ||
} | ||
|
||
public Boolean isMultilingual() { | ||
return getLanguages().size() > 1; | ||
} | ||
|
||
public List<Language> getLanguages() { | ||
return config.getLanguages(); | ||
} | ||
|
||
public Boolean getUseCookies() { | ||
return useCookies; | ||
} | ||
} |
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
Oops, something went wrong.