From 7e67160d53c9c08c29a40f337bafa4b4823aa051 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 10 Dec 2024 22:29:12 +0100 Subject: [PATCH] Implement locale helper methods to WebServerRequest. --- modules/web/http/web_server_request.cpp | 38 +++++++++++++++++++++++++ modules/web/http/web_server_request.h | 10 +++++++ 2 files changed, 48 insertions(+) diff --git a/modules/web/http/web_server_request.cpp b/modules/web/http/web_server_request.cpp index d45624868..fae9634d5 100644 --- a/modules/web/http/web_server_request.cpp +++ b/modules/web/http/web_server_request.cpp @@ -32,6 +32,7 @@ #include "web_server_request.h" #include "core/object/object.h" +#include "core/string/translation.h" #include "core/variant/variant.h" #include "web_server.h" #include "web_server_cookie.h" @@ -173,6 +174,35 @@ bool WebServerRequest::validate_csrf_token() { return param_token == token; } +bool WebServerRequest::has_locale() const { + return _locale != StringName(); +} +StringName WebServerRequest::get_locale() const { + if (_locale == StringName()) { + return OS::get_singleton()->get_locale(); + } + + return _locale; +} +void WebServerRequest::set_locale(const StringName &p_locale) { + _locale = p_locale; +} + +void WebServerRequest::save_locale_as_cookie() { + Ref cookie; + cookie.instance(); + + cookie->set_data("locale", _locale); + + response_add_cookie(cookie); +} +void WebServerRequest::load_locale_from_cookie() { + _locale = get_cookie("locale"); +} +void WebServerRequest::clear_locale_from_cookie() { + response_remove_cookie_simple("locale"); +} + String WebServerRequest::get_cookie(const String &key) { return String(); } @@ -631,6 +661,14 @@ void WebServerRequest::_bind_methods() { ClassDB::bind_method(D_METHOD("set_csrf_token", "value"), &WebServerRequest::set_csrf_token); ClassDB::bind_method(D_METHOD("validate_csrf_token"), &WebServerRequest::validate_csrf_token); + ClassDB::bind_method(D_METHOD("has_locale"), &WebServerRequest::has_locale); + ClassDB::bind_method(D_METHOD("get_locale"), &WebServerRequest::get_locale); + ClassDB::bind_method(D_METHOD("set_locale", "locale"), &WebServerRequest::set_locale); + + ClassDB::bind_method(D_METHOD("save_locale_as_cookie"), &WebServerRequest::save_locale_as_cookie); + ClassDB::bind_method(D_METHOD("load_locale_from_cookie"), &WebServerRequest::load_locale_from_cookie); + ClassDB::bind_method(D_METHOD("clear_locale_from_cookie"), &WebServerRequest::clear_locale_from_cookie); + ClassDB::bind_method(D_METHOD("get_cookie", "key"), &WebServerRequest::get_cookie); ClassDB::bind_method(D_METHOD("response_add_cookie", "cookie"), &WebServerRequest::response_add_cookie); diff --git a/modules/web/http/web_server_request.h b/modules/web/http/web_server_request.h index 1748b581d..eaf6ac9e4 100644 --- a/modules/web/http/web_server_request.h +++ b/modules/web/http/web_server_request.h @@ -91,6 +91,14 @@ public: void set_csrf_token(const String &value); bool validate_csrf_token(); + bool has_locale() const; + StringName get_locale() const; + void set_locale(const StringName &p_locale); + + void save_locale_as_cookie(); + void load_locale_from_cookie(); + void clear_locale_from_cookie(); + virtual String get_cookie(const String &key); void response_add_cookie(const Ref &cookie); @@ -200,6 +208,8 @@ protected: Ref _session; + StringName _locale; + bool _connection_closed; WebServer *_server;