Skip to content

Commit

Permalink
Loading message bundles using the flat-classpath theme provider (#11711)
Browse files Browse the repository at this point in the history
Closes #11186
  • Loading branch information
pedroigor committed May 5, 2022
1 parent fc974fc commit eab2dff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import org.keycloak.theme.ClasspathThemeResourceProviderFactory;

public class FlatClasspathThemeResourceProviderFactory extends ClasspathThemeResourceProviderFactory {
Expand All @@ -42,6 +44,18 @@ public InputStream getResourceAsStream(String path) throws IOException {
return null;
}

@Override
public Properties getMessages(String baseBundlename, Locale locale) throws IOException {
Properties messages = new Properties();
Enumeration<URL> resources = classLoader.getResources(THEME_RESOURCES_MESSAGES + baseBundlename + "_" + locale.toString() + ".properties");

while (resources.hasMoreElements()) {
loadMessages(messages, resources.nextElement());
}

return messages;
}

@Override
public String getId() {
return ID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,20 @@ protected InputStream getResourceAsStream(String path, URL rootResourceURL) thro

@Override
public Properties getMessages(String baseBundlename, Locale locale) throws IOException {
Properties m = new Properties();
InputStream in = classLoader.getResourceAsStream(THEME_RESOURCES_MESSAGES + baseBundlename + "_" + locale.toString() + ".properties");
if(in != null){
Charset encoding = PropertiesUtil.detectEncoding(in);
Properties messages = new Properties();
URL resource = classLoader.getResource(THEME_RESOURCES_MESSAGES + baseBundlename + "_" + locale.toString() + ".properties");
loadMessages(messages, resource);
return messages;
}

protected void loadMessages(Properties messages, URL resource) throws IOException {
if (resource != null) {
Charset encoding = PropertiesUtil.detectEncoding(resource.openStream());
// detectEncoding closes the stream
try (Reader reader = new InputStreamReader(
classLoader.getResourceAsStream(THEME_RESOURCES_MESSAGES + baseBundlename + "_" + locale.toString() + ".properties"), encoding)) {
m.load(reader);
try (Reader reader = new InputStreamReader(resource.openStream(), encoding)) {
messages.load(reader);
}
}
return m;
}

@Override
Expand Down

0 comments on commit eab2dff

Please sign in to comment.