Fixed Locale breaking the page

This commit is contained in:
Rsl1122 2019-09-06 08:56:01 +03:00
parent d3ec409ea3
commit b1579036a7
5 changed files with 62 additions and 9 deletions

View File

@ -16,7 +16,10 @@
*/ */
package com.djrapitops.plan.delivery.webserver.response; 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.djrapitops.plan.storage.file.PlanFiles;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException; import java.io.IOException;
@ -30,4 +33,10 @@ public class CSSResponse extends FileResponse {
super.setType(ResponseType.CSS); super.setType(ResponseType.CSS);
setContent(getContent()); setContent(getContent());
} }
@Override
public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException {
fixThemeColors(theme);
super.send(exchange, locale, theme);
}
} }

View File

@ -115,21 +115,22 @@ public abstract class Response {
this.responseHeaders = responseHeaders; 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 { public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException {
responseHeaders.set("Content-Type", type); responseHeaders.set("Content-Type", type);
responseHeaders.set("Content-Encoding", "gzip"); responseHeaders.set("Content-Encoding", "gzip");
exchange.sendResponseHeaders(getCode(), 0); exchange.sendResponseHeaders(getCode(), 0);
String sentContent = getContent();
// TODO Smell
if (!(this instanceof JavaScriptResponse)) {
sentContent = locale.replaceMatchingLanguage(sentContent);
}
sentContent = theme.replaceThemeColors(sentContent);
try ( try (
GZIPOutputStream out = new GZIPOutputStream(exchange.getResponseBody()); 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]; byte[] buffer = new byte[2048];
int count; int count;

View File

@ -17,8 +17,11 @@
package com.djrapitops.plan.delivery.webserver.response.errors; package com.djrapitops.plan.delivery.webserver.response.errors;
import com.djrapitops.plan.delivery.webserver.response.pages.PageResponse; 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.storage.file.PlanFiles;
import com.djrapitops.plan.version.VersionCheckSystem; import com.djrapitops.plan.version.VersionCheckSystem;
import com.sun.net.httpserver.HttpExchange;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor; import org.apache.commons.text.StringSubstitutor;
@ -68,6 +71,13 @@ public class ErrorResponse extends PageResponse {
this.paragraph = paragraph; 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 @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View File

@ -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.Response;
import com.djrapitops.plan.delivery.webserver.response.ResponseType; import com.djrapitops.plan.delivery.webserver.response.ResponseType;
import com.djrapitops.plan.exceptions.ParseException; 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.googlecode.htmlcompressor.compressor.HtmlCompressor;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
/** /**
* Response for all HTML Page responses. * Response for all HTML Page responses.
@ -48,6 +53,13 @@ public class PageResponse extends Response {
public PageResponse() { public PageResponse() {
} }
@Override
public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException {
translate(locale);
fixThemeColors(theme);
super.send(exchange, locale, theme);
}
@Override @Override
public void setContent(String content) { public void setContent(String content) {
super.setContent(HTML_COMPRESSOR.compress(content)); super.setContent(HTML_COMPRESSOR.compress(content));

View File

@ -25,6 +25,8 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.util.*; import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* Represents loaded language information. * Represents loaded language information.
@ -104,6 +106,14 @@ public class Locale extends HashMap<Lang, Message> {
return from; 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[][]{ Lang[][] langs = new Lang[][]{
NetworkPageLang.values(), NetworkPageLang.values(),
PlayerPageLang.values(), PlayerPageLang.values(),
@ -125,7 +135,18 @@ public class Locale extends HashMap<Lang, Message> {
with.add(replacement.toString()); 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 @Override