From 6bad54c4d2378bf4323ee3adcb69052f64b22f0e Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 10 Dec 2024 22:13:56 +0100 Subject: [PATCH] Created a HTMLTemplateMultilang class. It can automatically select between multiple HTMLTemplates. --- modules/web/SCsub | 1 + modules/web/html/html_template_multilang.cpp | 211 +++++++++++++++++++ modules/web/html/html_template_multilang.h | 68 ++++++ modules/web/register_types.cpp | 2 + 4 files changed, 282 insertions(+) create mode 100644 modules/web/html/html_template_multilang.cpp create mode 100644 modules/web/html/html_template_multilang.h diff --git a/modules/web/SCsub b/modules/web/SCsub index bf263b2e1..5a247e6f2 100644 --- a/modules/web/SCsub +++ b/modules/web/SCsub @@ -30,6 +30,7 @@ sources = [ "html/bbcode_parser.cpp", "html/form_validator.cpp", "html/html_template.cpp", + "html/html_template_multilang.cpp", "html/html_template_data.cpp", "html/libs/hoedown/autolink.c", diff --git a/modules/web/html/html_template_multilang.cpp b/modules/web/html/html_template_multilang.cpp new file mode 100644 index 000000000..7b1719732 --- /dev/null +++ b/modules/web/html/html_template_multilang.cpp @@ -0,0 +1,211 @@ +/*************************************************************************/ +/* html_template_multilang.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* PANDEMONIUM ENGINE */ +/* https://github.com/Relintai/pandemonium_engine */ +/*************************************************************************/ +/* Copyright (c) 2022-present Péter Magyar. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "html_template_multilang.h" +#include "core/object/object.h" + +Ref HTMLTemplateMultilang::get_template_for_locale(const StringName &p_locale) { + Ref *t = _locales.getptr(p_locale); + Ref ht; + + if (t) { + ht = *t; + } + + return ht; +} +void HTMLTemplateMultilang::set_template_for_locale(const StringName &p_locale, const Ref &p_template) { + if (!p_template.is_valid()) { + _locales.erase(p_locale); + return; + } + + _locales[p_locale] = p_template; +} + +HashMap> HTMLTemplateMultilang::get_all_locales() const { + return _locales; +} + +Dictionary HTMLTemplateMultilang::get_locales() const { + Dictionary d; + + for (const HashMap>::Element *E = _locales.front(); E; E = E->next) { + d[E->key()] = E->value(); + } + + return d; +} +void HTMLTemplateMultilang::set_locales(const Dictionary &p_dict) { + _locales.clear(); + + const Variant *next = p_dict.next(); + + while (next) { + Variant n = *next; + + _locales[n] = p_dict[n]; + + next = p_dict.next(next); + } +} + +String HTMLTemplateMultilang::_render(const Ref &p_request, const Dictionary &p_data) { + const Variant *pp = p_data.getptr("locale"); + if (pp) { + StringName locale = *pp; + + Ref *tp = _locales.getptr(locale); + + if (tp) { + return (*tp)->render(p_request, p_data); + } + } + + return HTMLTemplate::_render(p_request, p_data); +} + +HTMLTemplateMultilang::HTMLTemplateMultilang() { +} + +HTMLTemplateMultilang::~HTMLTemplateMultilang() { +} + +void HTMLTemplateMultilang::_on_editor_new_locale_button_pressed(const StringName &p_property) { + String name = p_property; + + if (name.begins_with("locales/")) { + int scount = name.get_slice_count("/"); + + String key = name.get_slicec('/', 1); + + if (scount == 2) { + // This way add_key can also be used as a key + if (key == "add_key_button") { + if (!_editor_new_locale_key.empty()) { + _locales[_editor_new_locale_key] = Ref(); + + _editor_new_locale_key = ""; + + property_list_changed_notify(); + } + } + + return; + } + + String property = name.get_slicec('/', 2); + + if (property == "delete_key_button") { + _locales.erase(key); + property_list_changed_notify(); + } + + return; + } +} + +bool HTMLTemplateMultilang::_set(const StringName &p_name, const Variant &p_value) { + String name = p_name; + + if (name.begins_with("locales/")) { + int scount = name.get_slice_count("/"); + + String key = name.get_slicec('/', 1); + + if (scount == 2) { + // This way add_key can also be used as a key + if (key == "add_key") { + _editor_new_locale_key = p_value; + return true; + } else if (key == "add_key_button") { + return true; + } + } + + String property = name.get_slicec('/', 2); + + if (property == "value") { + _locales[key] = p_value; + } + + return true; + } + + return false; +} + +bool HTMLTemplateMultilang::_get(const StringName &p_name, Variant &r_ret) const { + String name = p_name; + + if (name.begins_with("locales/")) { + int scount = name.get_slice_count("/"); + + String key = name.get_slicec('/', 1); + + if (scount == 2) { + // This way add_key can also be used as a key + if (key == "add_key") { + r_ret = _editor_new_locale_key; + return true; + } + } + + String property = name.get_slicec('/', 2); + + if (property == "value") { + if (!_locales.has(key)) { + return false; + } + + r_ret = _locales[key]; + return true; + } + + return true; + } + + return false; +} + +void HTMLTemplateMultilang::_get_property_list(List *p_list) const { + for (const HashMap>::Element *E = _locales.front(); E; E = E->next) { + p_list->push_back(PropertyInfo(Variant::OBJECT, "locales/" + E->key() + "/value", PROPERTY_HINT_RESOURCE_TYPE, "HTMLTemplate")); + p_list->push_back(PropertyInfo(Variant::NIL, "locales/" + E->key() + "/delete_key_button", PROPERTY_HINT_BUTTON, "_on_editor_new_locale_button_pressed:Close/EditorIcons")); + } + + p_list->push_back(PropertyInfo(Variant::STRING, "locales/add_key")); + p_list->push_back(PropertyInfo(Variant::NIL, "locales/add_key_button", PROPERTY_HINT_BUTTON, "_on_editor_new_locale_button_pressed:Add/EditorIcons")); +} + +void HTMLTemplateMultilang::_bind_methods() { + ClassDB::bind_method(D_METHOD("_on_editor_new_locale_button_pressed"), &HTMLTemplateMultilang::_on_editor_new_locale_button_pressed); +} diff --git a/modules/web/html/html_template_multilang.h b/modules/web/html/html_template_multilang.h new file mode 100644 index 000000000..6e28d3897 --- /dev/null +++ b/modules/web/html/html_template_multilang.h @@ -0,0 +1,68 @@ +#ifndef HTML_TEMPLATE_MULTILANG_H +#define HTML_TEMPLATE_MULTILANG_H + +/*************************************************************************/ +/* html_template_multilang.h */ +/*************************************************************************/ +/* This file is part of: */ +/* PANDEMONIUM ENGINE */ +/* https://github.com/Relintai/pandemonium_engine */ +/*************************************************************************/ +/* Copyright (c) 2022-present Péter Magyar. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "html_template.h" + +class HTMLTemplateMultilang : public HTMLTemplate { + GDCLASS(HTMLTemplateMultilang, HTMLTemplate); + +public: + Ref get_template_for_locale(const StringName &p_locale); + void set_template_for_locale(const StringName &p_locale, const Ref &p_template); + + HashMap> get_all_locales() const; + + Dictionary get_locales() const; + void set_locales(const Dictionary &p_dict); + + virtual String _render(const Ref &p_request, const Dictionary &p_data); + + HTMLTemplateMultilang(); + ~HTMLTemplateMultilang(); + +protected: + void _on_editor_new_locale_button_pressed(const StringName &p_property); + + bool _set(const StringName &p_name, const Variant &p_value); + bool _get(const StringName &p_name, Variant &r_ret) const; + void _get_property_list(List *p_list) const; + + static void _bind_methods(); + + HashMap> _locales; + + String _editor_new_locale_key; +}; + +#endif diff --git a/modules/web/register_types.cpp b/modules/web/register_types.cpp index 3c3c26512..5646dbcbe 100644 --- a/modules/web/register_types.cpp +++ b/modules/web/register_types.cpp @@ -40,6 +40,7 @@ #include "html/html_builder_bind.h" #include "html/html_parser.h" #include "html/html_template.h" +#include "html/html_template_multilang.h" #include "html/html_template_data.h" #include "html/markdown_renderer.h" #include "html/paginator.h" @@ -93,6 +94,7 @@ void register_web_types(ModuleRegistrationLevel p_level) { ClassDB::register_class<_HTMLTag>(); ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class();