Use explicit widget language code

This commit is contained in:
rajchales-monito
2026-07-30 08:24:13 +02:00
parent d49c672feb
commit fb33bd3dd6
2 changed files with 7 additions and 28 deletions
+2 -15
View File
@@ -2,7 +2,7 @@
const boot = window.MaalFlows || {}; const boot = window.MaalFlows || {};
const apiBase = (boot.apiBase || "").replace(/\/$/, "") || new URL(document.currentScript.src).origin; const apiBase = (boot.apiBase || "").replace(/\/$/, "") || new URL(document.currentScript.src).origin;
const siteKey = boot.siteKey || "9b-plus"; const siteKey = boot.siteKey || "9b-plus";
const lang = detectPageLanguage(boot); const lang = normalizeLanguage(boot.lang);
const storageKey = `maalflows:${siteKey}`; const storageKey = `maalflows:${siteKey}`;
const pendingStorageKey = `${storageKey}:pending`; const pendingStorageKey = `${storageKey}:pending`;
const session = loadSession(); const session = loadSession();
@@ -13,10 +13,7 @@
fetch(`${apiBase}/api/widget/config?${new URLSearchParams({ fetch(`${apiBase}/api/widget/config?${new URLSearchParams({
siteKey, siteKey,
lang, lang
pageLang: document.documentElement.lang || "",
currentUrl: location.href,
browserLang: navigator.language || ""
})}`) })}`)
.then((res) => res.json()) .then((res) => res.json())
.then((config) => mount(config)) .then((config) => mount(config))
@@ -506,16 +503,6 @@
}; };
} }
function detectPageLanguage(boot) {
const fromBoot = normalizeLanguage(boot.lang);
if (fromBoot) return fromBoot;
const fromUrl = normalizeLanguage(location.pathname.split("/").filter(Boolean)[0]);
if (fromUrl) return fromUrl;
const fromHtml = normalizeLanguage(document.documentElement.lang);
if (fromHtml) return fromHtml;
return normalizeLanguage(navigator.language) || "cs";
}
function normalizeLanguage(value) { function normalizeLanguage(value) {
const text = String(value || "").trim().toLowerCase().replace("_", "-"); const text = String(value || "").trim().toLowerCase().replace("_", "-");
const match = text.match(/^[a-z]{2,3}/); const match = text.match(/^[a-z]{2,3}/);
+5 -13
View File
@@ -1163,11 +1163,7 @@ function widgetLanguageForRequest(site, url) {
const settings = parseJson(site.settings_json, defaultSettings()); const settings = parseJson(site.settings_json, defaultSettings());
const languageSettings = normalizeWidgetLanguageSettings(settings.widgetLanguage); const languageSettings = normalizeWidgetLanguageSettings(settings.widgetLanguage);
if (languageSettings.mode === "fixed") return languageSettings.fixedLanguage; if (languageSettings.mode === "fixed") return languageSettings.fixedLanguage;
return allowedWidgetLanguage(url.searchParams.get("lang")) return allowedWidgetLanguage(url.searchParams.get("lang")) || languageSettings.defaultLanguage;
|| allowedWidgetLanguage(url.searchParams.get("pageLang"))
|| languageFromUrl(url.searchParams.get("currentUrl"))
|| allowedWidgetLanguage(url.searchParams.get("browserLang"))
|| languageSettings.fallbackLanguage;
} }
async function widgetCopyForLanguage(site, requestedLanguage) { async function widgetCopyForLanguage(site, requestedLanguage) {
@@ -1250,14 +1246,6 @@ function syncLegacyWidgetCopy(settings) {
} }
} }
function languageFromUrl(value) {
try {
return allowedWidgetLanguage(new URL(value).pathname.split("/").filter(Boolean)[0]);
} catch {
return "";
}
}
function allowedWidgetLanguage(value) { function allowedWidgetLanguage(value) {
const language = normalizeLanguage(value); const language = normalizeLanguage(value);
return EUROPEAN_WIDGET_LANGUAGES.has(language) ? language : ""; return EUROPEAN_WIDGET_LANGUAGES.has(language) ? language : "";
@@ -1517,9 +1505,13 @@ function formatSite(site) {
} }
function snippetFor(site) { function snippetFor(site) {
const settings = parseJson(site.settings_json, defaultSettings());
const languageSettings = normalizeWidgetLanguageSettings(settings.widgetLanguage);
const snippetLanguage = languageSettings.defaultLanguage || "cs";
return `<script> return `<script>
window.MaalFlows = { window.MaalFlows = {
siteKey: "${site.site_key}", siteKey: "${site.site_key}",
lang: "${snippetLanguage}",
apiBase: "${PUBLIC_BASE_URL}" apiBase: "${PUBLIC_BASE_URL}"
}; };
</script> </script>