|
| 1 | +{% extends "base.html" %} |
| 2 | + |
| 3 | +{% block extrahead %} |
| 4 | +<script> |
| 5 | + (function () { |
| 6 | + const langMap = { |
| 7 | + 'zh': '/zh/' |
| 8 | + }; |
| 9 | + |
| 10 | + const storageKey = 'user-language-set'; |
| 11 | + |
| 12 | + // safe storage access |
| 13 | + function storageGet(key) { |
| 14 | + try { |
| 15 | + return sessionStorage.getItem(key); |
| 16 | + } catch (e) { |
| 17 | + return null; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + function storageSet(key, val) { |
| 22 | + try { |
| 23 | + sessionStorage.setItem(key, val); |
| 24 | + return true; |
| 25 | + } catch (e) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + if (storageGet(storageKey)) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + const userLang = (navigator.language || navigator.userLanguage || '').split('-')[0].toLowerCase(); |
| 35 | + if (!userLang || !langMap[userLang]) { |
| 36 | + return; |
| 37 | + } |
| 38 | + const targetPath = langMap[userLang]; // e.g. '/zh/' |
| 39 | + |
| 40 | + const pathname = window.location.pathname || '/'; |
| 41 | + const search = window.location.search || ''; |
| 42 | + const hash = window.location.hash || ''; |
| 43 | + |
| 44 | + // ensure targetPath ends with single slash and starts with slash |
| 45 | + const normalize = (p) => ('/' + p.replace(/^\/+|\/+$/g, '') + '/'); |
| 46 | + const targetNorm = normalize(targetPath); |
| 47 | + |
| 48 | + // check if pathname already has language prefix as a leading segment |
| 49 | + // match ^/zh(/|$) |
| 50 | + const langPrefixRegex = new RegExp('^' + targetNorm.replace(/\/$/, '') + '(?:\\/|$)'); |
| 51 | + if (langPrefixRegex.test(pathname)) { |
| 52 | + return; // already in the right language prefix |
| 53 | + } |
| 54 | + |
| 55 | + // strip any known language prefixes (values of langMap) from the start if present |
| 56 | + let cleanPath = pathname; |
| 57 | + for (const p of Object.values(langMap)) { |
| 58 | + const pNorm = normalize(p); |
| 59 | + const stripRegex = new RegExp('^' + pNorm.replace(/\/$/, '') + '(?:\\/|$)'); |
| 60 | + if (stripRegex.test(cleanPath)) { |
| 61 | + cleanPath = cleanPath.replace(stripRegex, '/'); |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // make sure cleanPath begins with single slash |
| 67 | + if (!cleanPath.startsWith('/')) cleanPath = '/' + cleanPath; |
| 68 | + // avoid double slashes when building new path |
| 69 | + let newPath = targetNorm.replace(/\/+$/, '') + cleanPath.replace(/^\/+/, '/'); |
| 70 | + // normalize multiple slashes |
| 71 | + newPath = newPath.replace(/\/+/g, '/'); |
| 72 | + |
| 73 | + // append search/hash back |
| 74 | + newPath = newPath + search + hash; |
| 75 | + |
| 76 | + // set storage (best effort) before redirect to avoid repeated redirects |
| 77 | + storageSet(storageKey, userLang); |
| 78 | + |
| 79 | + // use replace to avoid leaving the previous (un-prefixed) URL in history; change to href if you prefer |
| 80 | + window.location.replace(newPath); |
| 81 | + })(); |
| 82 | +</script> |
| 83 | +{% endblock %} |
0 commit comments