Skip to content

Commit

Permalink
move to theme resource
Browse files Browse the repository at this point in the history
  • Loading branch information
edewit authored and pedroigor committed Oct 23, 2023
1 parent f3d3871 commit e4632c9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public KeycloakApplication() {

singletons.add(new ObjectMapperResolver());
classes.add(WelcomeResource.class);
classes.add(LocaleResource.class);

platform.onStartup(this::startup);
platform.onShutdown(this::shutdown);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,39 @@
*/
package org.keycloak.services.resources;

import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import org.keycloak.common.Version;
import org.keycloak.common.util.MimeTypeUtil;
import org.keycloak.encoding.ResourceEncodingHelper;
import org.keycloak.encoding.ResourceEncodingProvider;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.services.ServicesLogger;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.services.util.CacheControlUtil;
import org.keycloak.services.util.LocaleUtil;
import org.keycloak.theme.Theme;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
* Theme resource
Expand Down Expand Up @@ -86,4 +103,62 @@ public Response getResource(@PathParam("version") String version, @PathParam("th
}
}

@GET
@Path("{theme}/{locale}")
@Produces(MediaType.APPLICATION_JSON)
public List<KeySource> getLocalizationTexts(@PathParam("theme") String theme, @PathParam("locale") String localeString,
@QueryParam("source") boolean showSource) throws IOException {
final RealmModel realm = session.getContext().getRealm();

Theme theTheme = session.theme().getTheme(Theme.Type.valueOf(theme.toUpperCase()));
final Locale locale = Locale.forLanguageTag(localeString);
if (showSource) {
Properties messagesByLocale = theTheme.getMessages("messages", locale);
Set<KeySource> result = messagesByLocale.entrySet().stream().map(e ->
new KeySource((String) e.getKey(), (String) e.getValue(), Source.THEME)).collect(toSet());

Map<Locale, Properties> realmLocalizationMessages = LocaleUtil.getRealmLocalizationTexts(realm, locale);
for (Locale currentLocale = locale; currentLocale != null; currentLocale = LocaleUtil.getParentLocale(currentLocale)) {
final List<KeySource> realmOverride = realmLocalizationMessages.get(currentLocale).entrySet().stream().map(e ->
new KeySource((String) e.getKey(), (String) e.getValue(), Source.REALM)).collect(toList());
result.addAll(realmOverride);
}

return new ArrayList<>(result);
}
return theTheme.getEnhancedMessages(realm, locale).entrySet().stream().map(e ->
new KeySource((String) e.getKey(), (String) e.getValue())).collect(toList());
}
}

enum Source {
THEME,
REALM
}
class KeySource {
private String key;
private String value;
private Source source;

public KeySource(String key, String value) {
this.key = key;
this.value = value;
}

public KeySource(String key, String value, Source source) {
this(key, value);
this.source = source;
}

public String getKey() {
return key;
}

public String getValue() {
return value;
}

public Source getSource() {
return source;
}
}

0 comments on commit e4632c9

Please sign in to comment.