Skip to content

Commit

Permalink
[VIVO-1837] get i18n bundle from WebappDaoFactoryConfig preferred loc…
Browse files Browse the repository at this point in the history
…ales (#178)

* apply i18n text to VClassDaoJena.getLabelForClass
* expose i18n bundle through webapp dao factory interface
* use whole messages for i18n
* if request available use request bundle else use context bundle
* clear cache when context theme directory changes
* make getOverridingLocale from context private
* select locale based on preferred locale from webapp dao factory
* use first preferred local even when no selectable locales available on context
* build default preferred locales from default preferred languages

Resolves: https://jira.lyrasis.org/browse/VIVO-1837
  • Loading branch information
wwelling committed Aug 28, 2020
1 parent 2863d1f commit 80140a8
Show file tree
Hide file tree
Showing 19 changed files with 296 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;
import java.util.Set;

import edu.cornell.mannlib.vitro.webapp.i18n.I18nBundle;

public interface WebappDaoFactory {

/**
Expand Down Expand Up @@ -132,4 +134,6 @@ public interface WebappDaoFactory {

public MenuDao getMenuDao();

public I18nBundle getI18nBundle();

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import edu.cornell.mannlib.vitro.webapp.dao.PropertyDao.FullPropertyKey;
import edu.cornell.mannlib.vitro.webapp.rdfservice.filter.LanguageFilteringUtils;

public class WebappDaoFactoryConfig {

private List<String> preferredLanguages;
private List<Locale> preferredLocales;
private String defaultNamespace;
private Set<String> nonUserNamespaces;
private boolean isUnderlyingStoreReasoned = false;
public Map<FullPropertyKey, String> customListViewConfigFileMap;

public WebappDaoFactoryConfig() {
preferredLanguages = Arrays.asList("en-US", "en", "EN");
preferredLanguages = Arrays.asList("en-US", "en", "EN");
preferredLocales = LanguageFilteringUtils.languagesToLocales(preferredLanguages);
defaultNamespace = "http://vitro.mannlib.cornell.edu/ns/default#";
nonUserNamespaces = new HashSet<String>();
nonUserNamespaces.add(VitroVocabulary.vitroURI);
Expand All @@ -33,6 +37,14 @@ public void setPreferredLanguages(List<String> pl) {
this.preferredLanguages = pl;
}

public List<Locale> getPreferredLocales() {
return this.preferredLocales;
}

public void setPreferredLocales(List<Locale> pl) {
this.preferredLocales = pl;
}

public String getDefaultNamespace() {
return defaultNamespace;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import edu.cornell.mannlib.vitro.webapp.dao.VClassGroupDao;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilters;
import edu.cornell.mannlib.vitro.webapp.i18n.I18nBundle;

/**
* This wraps a WebappDaoFactory and applies filtering.
Expand Down Expand Up @@ -67,7 +68,7 @@ public class WebappDaoFactoryFiltering implements WebappDaoFactory {
transient private PropertyGroupDao filteringPropertyGroupDao=null;
transient private PropertyInstanceDao filteringPropertyInstanceDao=null;

public WebappDaoFactoryFiltering( WebappDaoFactory innerDao, VitroFilters filters){
public WebappDaoFactoryFiltering(WebappDaoFactory innerDao, VitroFilters filters){
if( innerDao == null )
throw new Error("innerWebappDaoFactory must be non-null");
this.filters = filters;
Expand Down Expand Up @@ -276,4 +277,9 @@ public DisplayModelDao getDisplayModelDao(){
public void close() {
innerWebappDaoFactory.close();
}

@Override
public I18nBundle getI18nBundle() {
return innerWebappDaoFactory.getI18nBundle();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public MainMenu getMainMenu( String url ) {
return getMenu( getOntModelSelector().getDisplayModel(), url );
}

@Override
public MainMenu getMainMenu( ServletRequest req, String url ) {
OntModel displayModel = LanguageFilteringUtils.wrapOntModelInALanguageFilter(getOntModelSelector().getDisplayModel(), req );
return getMenu(displayModel, url) ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package edu.cornell.mannlib.vitro.webapp.dao.jena;

import static java.lang.String.format;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -60,15 +62,18 @@
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.jena.event.EditEvent;
import edu.cornell.mannlib.vitro.webapp.i18n.I18nBundle;
import edu.cornell.mannlib.vitro.webapp.web.URLEncoder;

public class VClassDaoJena extends JenaBaseDao implements VClassDao {

protected static final Log log = LogFactory.getLog(VClassDaoJena.class);
private final I18nBundle i18n;
private boolean isUnderlyingStoreReasoned = false;

public VClassDaoJena(WebappDaoFactoryJena wadf, boolean isUnderlyingStoreReasoned) {
super(wadf);
this.i18n = wadf.getI18nBundle();
this.isUnderlyingStoreReasoned = isUnderlyingStoreReasoned;
}

Expand All @@ -91,17 +96,19 @@ public String getLabelForClass(OntClass cls,boolean withPrefix,boolean forPickLi
Restriction rest = cls.asRestriction();
OntProperty onProperty = rest.getOnProperty();
StringBuilder labelStr = new StringBuilder();
labelStr.append("restriction on ").append(getLabelOrId(onProperty)).append(": ");
labelStr.append(format("%s ", i18n.text("restriction_on")))
.append(getLabelOrId(onProperty))
.append(": ");
if (rest.isAllValuesFromRestriction() || rest.isSomeValuesFromRestriction()) {
Resource fillerRes = null;
if (rest.isAllValuesFromRestriction()) {
AllValuesFromRestriction avfRest = rest.asAllValuesFromRestriction();
fillerRes = avfRest.getAllValuesFrom();
labelStr.append("all values from ");
labelStr.append(format("%s ", i18n.text("all_values_from")));
} else {
SomeValuesFromRestriction svfRest = rest.asSomeValuesFromRestriction();
fillerRes = svfRest.getSomeValuesFrom();
labelStr.append("some values from ");
labelStr.append(format("%s ", i18n.text("some_values_from")));
}
if (fillerRes.canAs(OntClass.class)) {
OntClass avf = fillerRes.as(OntClass.class);
Expand All @@ -115,7 +122,7 @@ public String getLabelForClass(OntClass cls,boolean withPrefix,boolean forPickLi
}
} else if (rest.isHasValueRestriction()) {
HasValueRestriction hvRest = rest.asHasValueRestriction();
labelStr.append("has value ");
labelStr.append(format("%s ", i18n.text("has_value")));
RDFNode fillerNode = hvRest.getHasValue();
try {
if (fillerNode.isResource()) {
Expand All @@ -128,22 +135,22 @@ public String getLabelForClass(OntClass cls,boolean withPrefix,boolean forPickLi
}
} else if (rest.isMinCardinalityRestriction()) {
MinCardinalityRestriction mcRest = rest.asMinCardinalityRestriction();
labelStr.append("minimum cardinality ");
labelStr.append(format("%s ", i18n.text("minimum_cardinality")));
labelStr.append(mcRest.getMinCardinality());
} else if (rest.isMaxCardinalityRestriction()) {
MaxCardinalityRestriction mcRest = rest.asMaxCardinalityRestriction();
labelStr.append("maximum cardinality ");
labelStr.append(format("%s ", i18n.text("maximum_cardinality")));
labelStr.append(mcRest.getMaxCardinality());
} else if (rest.isCardinalityRestriction()) {
CardinalityRestriction cRest = rest.asCardinalityRestriction();
labelStr.append("cardinality ");
labelStr.append(format("%s ", i18n.text("cardinality")));
labelStr.append(cRest.getCardinality());
}
return labelStr.toString();
} else if (isBooleanClassExpression(cls)) {
StringBuilder labelStr = new StringBuilder("(");
if (cls.isComplementClass()) {
labelStr.append("not ");
labelStr.append(format("%s ", i18n.text("not")));
ComplementClass ccls = cls.as(ComplementClass.class);
labelStr.append(getLabelForClass(ccls.getOperand(), withPrefix, forPickList));
} else if (cls.isIntersectionClass()) {
Expand All @@ -153,7 +160,7 @@ public String getLabelForClass(OntClass cls,boolean withPrefix,boolean forPickLi
OntClass operand = operandIt.next();
labelStr.append(getLabelForClass(operand, withPrefix, forPickList));
if (operandIt.hasNext()) {
labelStr.append(" and ");
labelStr.append(format(" %s ", i18n.text("and")));
}
}
} else if (cls.isUnionClass()) {
Expand All @@ -163,7 +170,7 @@ public String getLabelForClass(OntClass cls,boolean withPrefix,boolean forPickLi
OntClass operand = operandIt.next();
labelStr.append(getLabelForClass(operand, withPrefix, forPickList));
if (operandIt.hasNext()) {
labelStr.append(" or ");
labelStr.append(format(" %s ", i18n.text("or")));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactoryConfig;
import edu.cornell.mannlib.vitro.webapp.i18n.I18n;
import edu.cornell.mannlib.vitro.webapp.i18n.I18nBundle;
import edu.cornell.mannlib.vitro.webapp.modelaccess.ModelNames;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.jena.model.RDFServiceModel;
Expand Down Expand Up @@ -611,5 +613,10 @@ public String makePickListName(ResourceBean bean) {
}
}

@Override
public I18nBundle getI18nBundle() {
// return context based bundle for preferred locales
return I18n.bundle(config.getPreferredLocales());
}

}
Loading

0 comments on commit 80140a8

Please sign in to comment.