mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-04 23:48:42 +01:00
Fixed Locale breaking the page
This commit is contained in:
parent
d3ec409ea3
commit
b1579036a7
@ -16,7 +16,10 @@
|
||||
*/
|
||||
package com.djrapitops.plan.delivery.webserver.response;
|
||||
|
||||
import com.djrapitops.plan.settings.locale.Locale;
|
||||
import com.djrapitops.plan.settings.theme.Theme;
|
||||
import com.djrapitops.plan.storage.file.PlanFiles;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@ -30,4 +33,10 @@ public class CSSResponse extends FileResponse {
|
||||
super.setType(ResponseType.CSS);
|
||||
setContent(getContent());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException {
|
||||
fixThemeColors(theme);
|
||||
super.send(exchange, locale, theme);
|
||||
}
|
||||
}
|
||||
|
@ -115,21 +115,22 @@ public abstract class Response {
|
||||
this.responseHeaders = responseHeaders;
|
||||
}
|
||||
|
||||
protected void translate(Locale locale) {
|
||||
content = locale.replaceMatchingLanguage(content);
|
||||
}
|
||||
|
||||
protected void fixThemeColors(Theme theme) {
|
||||
content = theme.replaceThemeColors(content);
|
||||
}
|
||||
|
||||
public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException {
|
||||
responseHeaders.set("Content-Type", type);
|
||||
responseHeaders.set("Content-Encoding", "gzip");
|
||||
exchange.sendResponseHeaders(getCode(), 0);
|
||||
|
||||
String sentContent = getContent();
|
||||
// TODO Smell
|
||||
if (!(this instanceof JavaScriptResponse)) {
|
||||
sentContent = locale.replaceMatchingLanguage(sentContent);
|
||||
}
|
||||
sentContent = theme.replaceThemeColors(sentContent);
|
||||
|
||||
try (
|
||||
GZIPOutputStream out = new GZIPOutputStream(exchange.getResponseBody());
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream(sentContent.getBytes(StandardCharsets.UTF_8))
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream((content != null ? content : "").getBytes(StandardCharsets.UTF_8))
|
||||
) {
|
||||
byte[] buffer = new byte[2048];
|
||||
int count;
|
||||
|
@ -17,8 +17,11 @@
|
||||
package com.djrapitops.plan.delivery.webserver.response.errors;
|
||||
|
||||
import com.djrapitops.plan.delivery.webserver.response.pages.PageResponse;
|
||||
import com.djrapitops.plan.settings.locale.Locale;
|
||||
import com.djrapitops.plan.settings.theme.Theme;
|
||||
import com.djrapitops.plan.storage.file.PlanFiles;
|
||||
import com.djrapitops.plan.version.VersionCheckSystem;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.text.StringSubstitutor;
|
||||
|
||||
@ -68,6 +71,13 @@ public class ErrorResponse extends PageResponse {
|
||||
this.paragraph = paragraph;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException {
|
||||
translate(locale);
|
||||
fixThemeColors(theme);
|
||||
super.send(exchange, locale, theme);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
|
@ -20,7 +20,12 @@ import com.djrapitops.plan.delivery.rendering.pages.Page;
|
||||
import com.djrapitops.plan.delivery.webserver.response.Response;
|
||||
import com.djrapitops.plan.delivery.webserver.response.ResponseType;
|
||||
import com.djrapitops.plan.exceptions.ParseException;
|
||||
import com.djrapitops.plan.settings.locale.Locale;
|
||||
import com.djrapitops.plan.settings.theme.Theme;
|
||||
import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Response for all HTML Page responses.
|
||||
@ -48,6 +53,13 @@ public class PageResponse extends Response {
|
||||
public PageResponse() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException {
|
||||
translate(locale);
|
||||
fixThemeColors(theme);
|
||||
super.send(exchange, locale, theme);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContent(String content) {
|
||||
super.setContent(HTML_COMPRESSOR.compress(content));
|
||||
|
@ -25,6 +25,8 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Represents loaded language information.
|
||||
@ -104,6 +106,14 @@ public class Locale extends HashMap<Lang, Message> {
|
||||
return from;
|
||||
}
|
||||
|
||||
Pattern scripts = Pattern.compile("(<script>[\\s\\S]*?</script>|<script src=[\"|'].*[\"|']></script>|<link [\\s\\S]*?>)");
|
||||
|
||||
Matcher scriptMatcher = scripts.matcher(from);
|
||||
List<String> foundScripts = new ArrayList<>();
|
||||
while (scriptMatcher.find()) {
|
||||
foundScripts.add(scriptMatcher.toMatchResult().group(0));
|
||||
}
|
||||
|
||||
Lang[][] langs = new Lang[][]{
|
||||
NetworkPageLang.values(),
|
||||
PlayerPageLang.values(),
|
||||
@ -125,7 +135,18 @@ public class Locale extends HashMap<Lang, Message> {
|
||||
with.add(replacement.toString());
|
||||
}));
|
||||
|
||||
return StringUtils.replaceEach(from, replace.toArray(new String[0]), with.toArray(new String[0]));
|
||||
String translated = StringUtils.replaceEach(from, replace.toArray(new String[0]), with.toArray(new String[0]));
|
||||
StringBuilder complete = new StringBuilder(translated.length());
|
||||
|
||||
String[] parts = scripts.split(translated);
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
complete.append(parts[i]);
|
||||
if (i < parts.length - 1) {
|
||||
complete.append(foundScripts.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
return complete.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user