From fe80290bed1f376e28ca3675e61cefb0161250fb Mon Sep 17 00:00:00 2001 From: ZanyRain <53324118+ZanyRain@users.noreply.github.com> Date: Tue, 27 Dec 2022 03:09:21 +0800 Subject: [PATCH] Output the handbook based off the client language (#1993) --- .../documentation/HandbookRequestHandler.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/emu/grasscutter/server/http/documentation/HandbookRequestHandler.java b/src/main/java/emu/grasscutter/server/http/documentation/HandbookRequestHandler.java index 04fa1b929..4ddde4265 100644 --- a/src/main/java/emu/grasscutter/server/http/documentation/HandbookRequestHandler.java +++ b/src/main/java/emu/grasscutter/server/http/documentation/HandbookRequestHandler.java @@ -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 handbookHtmls; @@ -33,12 +35,24 @@ 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 { - ctx.contentType(ContentType.TEXT_HTML); - ctx.result(this.handbookHtmls.get(langIdx)); + if (langIdx <= this.handbookHtmls.size() - 1) { + ctx.contentType(ContentType.TEXT_HTML); + ctx.result(this.handbookHtmls.get(langIdx)); + } } }