Output the handbook based off the client language (#1993)

This commit is contained in:
ZanyRain 2022-12-27 03:09:21 +08:00 committed by GitHub
parent 92fbaa8670
commit fe80290bed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,6 +18,8 @@ import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final class HandbookRequestHandler implements DocumentationHandler {
private List<String> handbookHtmls;
@ -33,14 +35,26 @@ final class HandbookRequestHandler implements DocumentationHandler {
@Override
public void handle(Context ctx) {
final int langIdx = Language.TextStrings.MAP_LANGUAGES.getOrDefault(DOCUMENT_LANGUAGE, 0); // TODO: This should really be based off the client language somehow
int langIdx = 0;
String acceptLanguage = ctx.header("Accept-Language");
if (acceptLanguage != null) {
Pattern localePattern = Pattern.compile("[a-z]+-[A-Z]+");
Matcher matcher = localePattern.matcher(acceptLanguage);
if (matcher.find()) {
String lang = matcher.group(0);
langIdx = Language.TextStrings.MAP_GC_LANGUAGES.getOrDefault(lang,0);
}
}
if (this.handbookHtmls == null) {
ctx.status(500);
} else {
if (langIdx <= this.handbookHtmls.size() - 1) {
ctx.contentType(ContentType.TEXT_HTML);
ctx.result(this.handbookHtmls.get(langIdx));
}
}
}
private List<String> generateHandbookHtmls(String template) {
final int NUM_LANGUAGES = Language.TextStrings.NUM_LANGUAGES;