Implemented locale initialization based on browser settings, added methods to determine locale from URL parameters and Accept-Language header, and updated app initialization to include locale setup.

This commit is contained in:
Torsten Schulz (local)
2025-11-16 13:18:45 +01:00
parent e869abde5d
commit 5288f21e52
12 changed files with 443 additions and 0 deletions

View File

@@ -41,12 +41,14 @@
#include <Wt/WCheckBox.h>
#include <Wt/WTimer.h>
#include <Wt/WResource.h>
#include <Wt/WLocale.h>
App::App(const Wt::WEnvironment &env, Broadcast &server):
Wt::WApplication(env),
env_(env),
server_(server),
updateLocationSignal_(this, "updateLocationSignal") {
initLocale();
initApp();
updateLocation();
enableUpdates(true);
@@ -66,6 +68,95 @@ void App::setMetaTags() {
addMetaHeader(Wt::MetaHeaderType::Meta, "google-adsense-account", "ca-pub-1104166651501135");
}
void App::initLocale() {
auto locale = determineLocaleFromBrowser();
setLocale(locale);
}
Wt::WLocale App::determineLocaleFromBrowser() const {
auto makeLocale = [](const std::string &code) -> Wt::WLocale {
return Wt::WLocale(code);
};
auto mapLanguageTag = [&makeLocale](std::string tag) -> std::string {
// trim
auto begin = tag.find_first_not_of(" \t");
auto end = tag.find_last_not_of(" \t");
if (begin == std::string::npos) {
return "";
}
tag = tag.substr(begin, end - begin + 1);
// nur Sprach-/Regionscode ohne q-Wert
auto semicolonPos = tag.find(';');
if (semicolonPos != std::string::npos) {
tag = tag.substr(0, semicolonPos);
}
std::string lower = tag;
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
// Sprache und Region trennen
std::string lang = lower;
std::string region;
auto dashPos = lower.find('-');
if (dashPos != std::string::npos) {
lang = lower.substr(0, dashPos);
region = lower.substr(dashPos + 1);
}
if (lang == "en") {
if (region == "us") {
return "en_US";
}
// Standard: britisches Englisch
return "en_GB";
} else if (lang == "de") {
return "de_DE";
} else if (lang == "fr") {
return "fr_FR";
} else if (lang == "es") {
return "es_ES";
} else if (lang == "it") {
return "it_IT";
} else if (lang == "tl" || lang == "fil") {
return "tl_PH";
} else if (lang == "ja") {
return "ja_JP";
} else if (lang == "zh") {
return "zh_CN";
} else if (lang == "th") {
return "th_TH";
}
return "";
};
// 1. Expliziter URL-Parameter ?lang=... hat Vorrang (z.B. de, de-DE, en, en-US, fr, es, it, tl, ja, zh, th)
if (const auto *langParam = env_.getParameter("lang")) {
auto localeName = mapLanguageTag(langParam->toUTF8());
if (!localeName.empty()) {
return makeLocale(localeName);
}
}
// 2. Sonst Accept-Language Header des Browsers auswerten
std::string accept = env_.headerValue("Accept-Language");
if (!accept.empty()) {
std::stringstream ss(accept);
std::string part;
while (std::getline(ss, part, ',')) {
auto localeName = mapLanguageTag(part);
if (!localeName.empty()) {
return makeLocale(localeName);
}
}
}
// Fallback: britisches Englisch
return makeLocale("en_GB");
}
void App::initApp() {
setMetaTags();
setTitle("YP Direct Chat");

View File

@@ -99,6 +99,8 @@ private:
Wt::WTimer *timeoutRemainingTimer_;
bool isLoggedInAsAdmin {false};
void setMetaTags();
void initLocale();
Wt::WLocale determineLocaleFromBrowser() const;
void initApp();
void reSetUser();
Wt::WVBoxLayout *createVerticalLayout();