From 610f189839c89c6101dffc58374add3e037c5c15 Mon Sep 17 00:00:00 2001 From: xtexChooser Date: Mon, 23 May 2022 05:26:46 +0800 Subject: [PATCH] feat(wiki): Support custom language for Wikipedia (#505) * sys: support typeless settings * feat(wiki): support custom language for Wikipedia * update --- plugins/builtin/source/content/settings_entries.cpp | 11 +++++++++++ plugins/builtin/source/content/tools_entries.cpp | 11 +++++++---- plugins/builtin/source/lang/de_DE.cpp | 1 + plugins/builtin/source/lang/en_US.cpp | 1 + plugins/builtin/source/lang/it_IT.cpp | 1 + plugins/builtin/source/lang/ja_JP.cpp | 1 + plugins/builtin/source/lang/zh_CN.cpp | 1 + 7 files changed, 23 insertions(+), 4 deletions(-) diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index f573cf161..6ac7f1945 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -124,6 +124,17 @@ namespace hex::plugin::builtin { return false; }); + ContentRegistry::Settings::add("hex.builtin.setting.interface", "hex.builtin.setting.interface.wiki_explain_language", "en", [](auto name, nlohmann::json &setting) { + static auto lang = std::string(setting); + + if (ImGui::InputText(name.data(), lang, ImGuiInputTextFlags_CharsNoBlank)) { + setting = std::string(lang.c_str()); // remove following zero bytes + return true; + } + + return false; + }); + ContentRegistry::Settings::add("hex.builtin.setting.interface", "hex.builtin.setting.interface.fps", 60, [](auto name, nlohmann::json &setting) { static int fps = static_cast(setting); diff --git a/plugins/builtin/source/content/tools_entries.cpp b/plugins/builtin/source/content/tools_entries.cpp index 2da60681c..94e27cf3e 100644 --- a/plugins/builtin/source/content/tools_entries.cpp +++ b/plugins/builtin/source/content/tools_entries.cpp @@ -662,6 +662,11 @@ namespace hex::plugin::builtin { } } + std::string getWikipediaApiUrl() { + auto setting = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.wiki_explain_language"); + return "https://" + std::string(setting) + ".wikipedia.org/w/api.php?format=json&action=query&prop=extracts&explaintext&redirects=10&formatversion=2"; + } + void drawWikiExplainer() { static hex::Net net; @@ -677,8 +682,6 @@ namespace hex::plugin::builtin { return s; }(); - constexpr static auto WikipediaApiUrl = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&explaintext&redirects=10&formatversion=2"; - ImGui::Header("hex.builtin.tools.wiki_explain.control"_lang, true); bool startSearch; @@ -691,7 +694,7 @@ namespace hex::plugin::builtin { ImGui::EndDisabled(); if (startSearch && !searchString.empty()) { - searchProcess = net.getString(WikipediaApiUrl + "&exintro"s + "&titles="s + net.encode(searchString)); + searchProcess = net.getString(getWikipediaApiUrl() + "&exintro"s + "&titles="s + net.encode(searchString)); } ImGui::Header("hex.builtin.tools.wiki_explain.results"_lang); @@ -716,7 +719,7 @@ namespace hex::plugin::builtin { if (!extendedSearch && resultExtract.ends_with(':')) { extendedSearch = true; - searchProcess = net.getString(WikipediaApiUrl + "&titles="s + net.encode(searchString)); + searchProcess = net.getString(getWikipediaApiUrl() + "&titles="s + net.encode(searchString)); resultTitle.clear(); resultExtract.clear(); } else { diff --git a/plugins/builtin/source/lang/de_DE.cpp b/plugins/builtin/source/lang/de_DE.cpp index 2df9b0b4b..3658942a7 100644 --- a/plugins/builtin/source/lang/de_DE.cpp +++ b/plugins/builtin/source/lang/de_DE.cpp @@ -666,6 +666,7 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.interface.scaling.x1_5", "x1.5" }, { "hex.builtin.setting.interface.scaling.x2_0", "x2.0" }, { "hex.builtin.setting.interface.language", "Sprache" }, + //{ "hex.builtin.setting.interface.wiki_explain_language", "Wikipedia Language" }, { "hex.builtin.setting.interface.fps", "FPS Limite" }, { "hex.builtin.setting.interface.fps.unlocked", "Unbegrenzt" }, { "hex.builtin.setting.interface.highlight_alpha", "Markierungssichtbarkeit" }, diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index f88f80ab9..8e5e2aabe 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -670,6 +670,7 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.interface.scaling.x1_5", "x1.5" }, { "hex.builtin.setting.interface.scaling.x2_0", "x2.0" }, { "hex.builtin.setting.interface.language", "Language" }, + { "hex.builtin.setting.interface.wiki_explain_language", "Wikipedia Language" }, { "hex.builtin.setting.interface.fps", "FPS Limit" }, { "hex.builtin.setting.interface.fps.unlocked", "Unlocked" }, { "hex.builtin.setting.interface.highlight_alpha", "Highlighting opacity" }, diff --git a/plugins/builtin/source/lang/it_IT.cpp b/plugins/builtin/source/lang/it_IT.cpp index 246ab4510..e0c25218a 100644 --- a/plugins/builtin/source/lang/it_IT.cpp +++ b/plugins/builtin/source/lang/it_IT.cpp @@ -662,6 +662,7 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.interface.color.light", "Chiaro" }, { "hex.builtin.setting.interface.color.classic", "Classico" }, { "hex.builtin.setting.interface.language", "Lingua" }, + //{ "hex.builtin.setting.interface.wiki_explain_language", "Wikipedia Language" }, { "hex.builtin.setting.interface.scaling", "Scale" }, { "hex.builtin.setting.interface.scaling.native", "Nativo" }, { "hex.builtin.setting.interface.scaling.x0_5", "x0.5" }, diff --git a/plugins/builtin/source/lang/ja_JP.cpp b/plugins/builtin/source/lang/ja_JP.cpp index cd420d05e..f110c9358 100644 --- a/plugins/builtin/source/lang/ja_JP.cpp +++ b/plugins/builtin/source/lang/ja_JP.cpp @@ -668,6 +668,7 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.interface.scaling.x1_5", "x1.5" }, { "hex.builtin.setting.interface.scaling.x2_0", "x2.0" }, { "hex.builtin.setting.interface.language", "言語" }, + //{ "hex.builtin.setting.interface.wiki_explain_language", "Wikipedia Language" }, { "hex.builtin.setting.interface.fps", "FPS制限" }, { "hex.builtin.setting.interface.fps.unlocked", "無制限" }, { "hex.builtin.setting.interface.highlight_alpha", "ハイライトの不透明度" }, diff --git a/plugins/builtin/source/lang/zh_CN.cpp b/plugins/builtin/source/lang/zh_CN.cpp index 3ef9894f1..e8894eef3 100644 --- a/plugins/builtin/source/lang/zh_CN.cpp +++ b/plugins/builtin/source/lang/zh_CN.cpp @@ -662,6 +662,7 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.interface.scaling.x1_5", "x1.5" }, { "hex.builtin.setting.interface.scaling.x2_0", "x2.0" }, { "hex.builtin.setting.interface.language", "语言" }, + { "hex.builtin.setting.interface.wiki_explain_language", "维基百科语种" }, { "hex.builtin.setting.interface.fps", "FPS限制" }, { "hex.builtin.setting.interface.fps.unlocked", "无限制" }, { "hex.builtin.setting.interface.highlight_alpha", "高亮不透明度" },