Load texts from ResourceBundles in the theme

git-svn-id: https://svn.libreccm.org/ccm/trunk@5855 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2019-03-06 19:08:03 +00:00
parent 29bfeebf2e
commit d504f2dbb9
2 changed files with 103 additions and 7 deletions

View File

@ -11,13 +11,10 @@ import com.arsdigita.globalization.GlobalizationHelper;
import com.arsdigita.subsite.Site;
import com.arsdigita.templating.PresentationManager;
import com.arsdigita.themedirector.ThemeDirector;
import com.arsdigita.themedirector.ui.ThemeXSLParameterGenerator;
import com.arsdigita.util.UncheckedWrapperException;
import com.arsdigita.web.Web;
import com.arsdigita.xml.Document;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.cache.WebappTemplateLoader;
@ -42,6 +39,8 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
@ -203,8 +202,16 @@ public class FreeMarkerPresentationManager implements PresentationManager {
if (selectedLocale == null) {
data.put("selectedLanguage", "");
} else {
data.put("selectedLanguage", selectedLocale.getLanguage());
data.put("selectedLanguage",
GlobalizationHelper.getNegotiatedLocale().getLanguage());
}
configuration.setSharedVariable(
"getLocalizedText",
new GetLocalizedText(servletContext,
themePath,
selectedLocale.getLanguage()));
data.put("serverName", request.getServerName());
data.put("serverPort", request.getServerPort());
data.put("userAgent", request.getHeader("user-Agent"));
@ -330,9 +337,9 @@ public class FreeMarkerPresentationManager implements PresentationManager {
public Object exec(final List list) throws TemplateModelException {
if (list.isEmpty()) {
throw new IllegalArgumentException("GetContentItemTemplate "
+ "requires the following parameters: "
+ "item: NodeModel, view: String, style: String");
throw new IllegalArgumentException(
"GetContentItemTemplate requires the following parameters: "
+ "item: NodeModel, view: String, style: String");
}
// final Object arg0 = list.get(0);
@ -372,6 +379,42 @@ public class FreeMarkerPresentationManager implements PresentationManager {
}
private class GetLocalizedText implements TemplateMethodModelEx {
private final ResourceBundle resourceBundle;
public GetLocalizedText(final ServletContext servletContext,
final String themePath,
final String language) {
final String bundleName = String.format("%stexts", themePath);
final ThemeResourceBundleControl control
= new ThemeResourceBundleControl(
servletContext);
resourceBundle = ResourceBundle.getBundle(bundleName,
new Locale(language),
control);
}
@Override
public Object exec(final List list) throws TemplateModelException {
if (list.size() != 1) {
throw new IllegalArgumentException(
"GetLocalizedText requires exactly one parameter: "
+ "The key for the text to localize."
);
}
final String key = ((TemplateScalarModel) list
.get(0))
.getAsString();
return resourceBundle.getString(key);
}
}
private String findContentItemTemplate(
final Templates templates,
final String objectType,

View File

@ -0,0 +1,53 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.libreccm.theming;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import javax.servlet.ServletContext;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class ThemeResourceBundleControl extends ResourceBundle.Control {
private final ServletContext servletContext;
public ThemeResourceBundleControl(final ServletContext servletContext) {
super();
this.servletContext = servletContext;
}
@Override
public ResourceBundle newBundle(final String baseName,
final Locale locale,
final String format,
final ClassLoader loader,
final boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
final String bundleName = toBundleName(baseName, locale);
final String fileName = String.format("%s.properties", bundleName);
final InputStream stream = servletContext
.getResourceAsStream(fileName);
if (stream == null) {
return null;
} else {
return new PropertyResourceBundle(stream);
}
}
}