Use explicit widget language code
This commit is contained in:
+2
-15
@@ -2,7 +2,7 @@
|
||||
const boot = window.MaalFlows || {};
|
||||
const apiBase = (boot.apiBase || "").replace(/\/$/, "") || new URL(document.currentScript.src).origin;
|
||||
const siteKey = boot.siteKey || "9b-plus";
|
||||
const lang = detectPageLanguage(boot);
|
||||
const lang = normalizeLanguage(boot.lang);
|
||||
const storageKey = `maalflows:${siteKey}`;
|
||||
const pendingStorageKey = `${storageKey}:pending`;
|
||||
const session = loadSession();
|
||||
@@ -13,10 +13,7 @@
|
||||
|
||||
fetch(`${apiBase}/api/widget/config?${new URLSearchParams({
|
||||
siteKey,
|
||||
lang,
|
||||
pageLang: document.documentElement.lang || "",
|
||||
currentUrl: location.href,
|
||||
browserLang: navigator.language || ""
|
||||
lang
|
||||
})}`)
|
||||
.then((res) => res.json())
|
||||
.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) {
|
||||
const text = String(value || "").trim().toLowerCase().replace("_", "-");
|
||||
const match = text.match(/^[a-z]{2,3}/);
|
||||
|
||||
@@ -1163,11 +1163,7 @@ function widgetLanguageForRequest(site, url) {
|
||||
const settings = parseJson(site.settings_json, defaultSettings());
|
||||
const languageSettings = normalizeWidgetLanguageSettings(settings.widgetLanguage);
|
||||
if (languageSettings.mode === "fixed") return languageSettings.fixedLanguage;
|
||||
return allowedWidgetLanguage(url.searchParams.get("lang"))
|
||||
|| allowedWidgetLanguage(url.searchParams.get("pageLang"))
|
||||
|| languageFromUrl(url.searchParams.get("currentUrl"))
|
||||
|| allowedWidgetLanguage(url.searchParams.get("browserLang"))
|
||||
|| languageSettings.fallbackLanguage;
|
||||
return allowedWidgetLanguage(url.searchParams.get("lang")) || languageSettings.defaultLanguage;
|
||||
}
|
||||
|
||||
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) {
|
||||
const language = normalizeLanguage(value);
|
||||
return EUROPEAN_WIDGET_LANGUAGES.has(language) ? language : "";
|
||||
@@ -1517,9 +1505,13 @@ function formatSite(site) {
|
||||
}
|
||||
|
||||
function snippetFor(site) {
|
||||
const settings = parseJson(site.settings_json, defaultSettings());
|
||||
const languageSettings = normalizeWidgetLanguageSettings(settings.widgetLanguage);
|
||||
const snippetLanguage = languageSettings.defaultLanguage || "cs";
|
||||
return `<script>
|
||||
window.MaalFlows = {
|
||||
siteKey: "${site.site_key}",
|
||||
lang: "${snippetLanguage}",
|
||||
apiBase: "${PUBLIC_BASE_URL}"
|
||||
};
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user