From 387f977d6ce24ec2f88a20103e29b3464690c201 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 29 Mar 2022 20:16:53 +0400 Subject: [PATCH] Enter ParameterizedMessage --- src/server/i18n.cpp | 5 +++++ src/server/i18n.h | 18 ++++++++++++++++++ src/server/response.cpp | 14 +++++++------- src/server/response.h | 2 ++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/server/i18n.cpp b/src/server/i18n.cpp index 740df7da1..2aecc724d 100644 --- a/src/server/i18n.cpp +++ b/src/server/i18n.cpp @@ -106,4 +106,9 @@ std::string expandParameterizedString(const std::string& lang, } // namespace i18n +std::string ParameterizedMessage::getText(const std::string& lang) const +{ + return i18n::expandParameterizedString(lang, msgId, params); +} + } // namespace kiwix diff --git a/src/server/i18n.h b/src/server/i18n.h index 4f1e645ff..955f13091 100644 --- a/src/server/i18n.h +++ b/src/server/i18n.h @@ -52,6 +52,24 @@ std::string expandParameterizedString(const std::string& lang, } // namespace i18n +struct ParameterizedMessage +{ +public: // types + typedef kainjow::mustache::object Parameters; + +public: // functions + ParameterizedMessage(const std::string& msgId, const Parameters& params) + : msgId(msgId) + , params(params) + {} + + std::string getText(const std::string& lang) const; + +private: // data + const std::string msgId; + const Parameters params; +}; + } // namespace kiwix #endif // KIWIX_SERVER_I18N diff --git a/src/server/response.cpp b/src/server/response.cpp index 795ec7dc4..09a9b4745 100644 --- a/src/server/response.cpp +++ b/src/server/response.cpp @@ -21,7 +21,6 @@ #include "request_context.h" #include "internalServer.h" #include "kiwixlib-resources.h" -#include "i18n.h" #include "tools/regexTools.h" #include "tools/stringTools.h" @@ -132,12 +131,7 @@ HTTP404HtmlResponse::HTTP404HtmlResponse(const InternalServer& server, HTTPErrorHtmlResponse& HTTP404HtmlResponse::operator+(UrlNotFoundMsg /*unused*/) { const std::string requestUrl = m_request.get_full_url(); - const auto urlNotFoundMsg = i18n::expandParameterizedString( - "en", // FIXME: hardcoded language - "url-not-found", - {{"url", requestUrl}} - ); - return *this + urlNotFoundMsg; + return *this + ParameterizedMessage("url-not-found", {{"url", requestUrl}}); } HTTPErrorHtmlResponse& HTTPErrorHtmlResponse::operator+(const std::string& msg) @@ -146,6 +140,12 @@ HTTPErrorHtmlResponse& HTTPErrorHtmlResponse::operator+(const std::string& msg) return *this; } +HTTPErrorHtmlResponse& HTTPErrorHtmlResponse::operator+(const ParameterizedMessage& details) +{ + return *this + details.getText(m_request.get_user_language()); +} + + HTTP400HtmlResponse::HTTP400HtmlResponse(const InternalServer& server, const RequestContext& request) : HTTPErrorHtmlResponse(server, diff --git a/src/server/response.h b/src/server/response.h index 1c93434e3..d9a0ada88 100644 --- a/src/server/response.h +++ b/src/server/response.h @@ -28,6 +28,7 @@ #include "byte_range.h" #include "entry.h" #include "etag.h" +#include "i18n.h" extern "C" { #include "microhttpd_wrapper.h" @@ -189,6 +190,7 @@ struct HTTPErrorHtmlResponse : ContentResponseBlueprint using ContentResponseBlueprint::operator+; HTTPErrorHtmlResponse& operator+(const std::string& msg); + HTTPErrorHtmlResponse& operator+(const ParameterizedMessage& errorDetails); }; class UrlNotFoundMsg {};