!function(){"use strict";const t="liveSubtitleUiLocale",e="zh_TW",n=new Set(["zh_TW","en"]),o=window.setTimeout((()=>{document.documentElement?.classList?.remove?.("i18n-pending")}),1200),a=Object.freeze({"data-i18n-aria-label":"aria-label","data-i18n-placeholder":"placeholder","data-i18n-title":"title","data-i18n-label":"label","data-i18n-href":"href","data-i18n-data-price":"data-price"}),r=Object.freeze([{pattern:/Chrome 尚未授予這個影片網站的存取權/u,key:"siteAccessNotGranted"},{pattern:/請先切到要翻譯的影片分頁/u,key:"openExtensionOnVideoTab"},{pattern:/(?:找不到目前分頁|缺少目前分頁 ID)/u,key:"activeTabNotFound"},{pattern:/影片分頁已關閉/u,key:"videoTabClosed"},{pattern:/(?:offscreen 音訊擷取啟動逾時|offscreen 音訊擷取啟動失敗)/u,key:"audioCaptureStartFailed"},{pattern:/(?:影片分頁持續重新載入|content script 注入逾時|字幕框未掛載|字幕匡未掛載|content script 無回應)/u,key:"subtitleOverlayMountFailed"},{pattern:/(?:登入已過期|Firebase 登入已失效|缺少 refresh token|登入資料不完整|token 格式錯誤)/u,key:"authExpiredSignInAgain"},{pattern:/Email 尚未驗證/u,key:"emailVerificationRequired"},{pattern:/(?:免登入試用暫時無法啟用|後端尚未開啟免登入試用|免登入試用建立過於頻繁)/u,key:"trialTemporarilyUnavailable"}]);let i=e,c={},l=!1;function s(t,e=1200){return Promise.race([Promise.resolve(t),new Promise(((t,n)=>{window.setTimeout((()=>n(new Error("Popup localization timed out"))),e)}))])}function u(t){const n=String(t||"").trim().replace("-","_").toLowerCase();return"en"===n||n.startsWith("en_")?"en":"zh_tw"===n||"zh_hant"===n?"zh_TW":e}function f(){try{const t=new URLSearchParams(window.location.search).get("lang");return t?u(t):""}catch{return""}}function d(t,e={},n=""){const o=c?.[t];return function(t,e={}){return String(t||"").replace(/\{\{([a-zA-Z0-9_]+)\}\}/g,((t,n)=>Object.prototype.hasOwnProperty.call(e,n)?String(e[n]):t))}(("string"==typeof o?o:o?.message)||n,e)}function g(t=document){if(t?.querySelectorAll){document.documentElement.lang="en"===i?"en":"zh-Hant",document.documentElement.dataset.locale=i;for(const e of t.querySelectorAll("[data-i18n]")){const t=d(e.getAttribute("data-i18n"),{},"");t&&(e.textContent=t)}for(const[e,n]of Object.entries(a))for(const o of t.querySelectorAll(`[${e}]`)){const t=d(o.getAttribute(e),{},"");t&&o.setAttribute(n,t)}!function(t){if("en"===i)for(const e of t.querySelectorAll("#sourceLang, #targetLang"))for(const t of e.querySelectorAll("option:not([data-i18n])")){const e=String(t.textContent||"").trim(),n=e.indexOf(" / ");n>=0&&(t.textContent=e.slice(n+3).trim())}}(t),function(t){const e=t.getElementById?.("uiLanguageToggle");if(!e)return;const n="en"!==i,o=n?"EN":"中文",a=n?d("switchToEnglish",{},"Switch to English"):d("switchToChinese",{},"切換為繁體中文");e.textContent=o,e.setAttribute("aria-label",a),e.setAttribute("title",a),e.setAttribute("lang",n?"en":"zh-Hant")}(t)}}async function h(){const e="en"===i?"zh_TW":"en";if(await async function(e){try{const n=globalThis.chrome?.storage?.local;if("function"==typeof n?.set)return void await n.set({[t]:e})}catch{}try{window.localStorage?.setItem(t,e)}catch{}}(e),f())try{const t=new URL(window.location.href);t.searchParams.delete("lang"),window.history?.replaceState?.(null,"",`${t.pathname}${t.search}${t.hash}`)}catch{}window.location.reload()}window.PopupI18n={STORAGE_KEY:t,apply:g,getLocale:()=>i,isEnglish:()=>"en"===i,localizeExternalError:function(t,e="genericError",n="發生錯誤，請稍後再試。"){const o=String(t||"").trim();if(!o)return d(e,{},n);if("en"===i&&/[\u3400-\u9fff]/u.test(o)){const t=o.match(/目前最多同時開啟\s*(\d+)\s*個字幕分頁/u);if(t)return d("tooManySubtitleSessions",{max:t[1]},n);const a=o.match(/找不到可播放的\s*(Instagram|Twitch|YouTube)\s*直播/u);if(a)return d("liveStreamNotPlayable",{platform:a[1]},n);for(const t of r)if(t.pattern.test(o))return d(t.key,{},n);return d(e,{},n)}return o},normalizeLocale:u,t:d,toggleLocale:h},window.PopupI18n.ready=async function(){const a=f()||await async function(){try{const e=globalThis.chrome?.storage?.local;if("function"==typeof e?.get){const o=await s(e.get(t)),a=o?.[t];if("auto"===a)return"";if(n.has(a))return a}}catch{}try{const e=window.localStorage?.getItem(t);return n.has(e)?e:""}catch{return""}}();i=a||function(){try{return u(globalThis.chrome?.i18n?.getUILanguage?.()||navigator.language||e)}catch{return e}}();try{c=await async function(t){const e=await s(fetch(function(t){const e=`_locales/${t}/messages.json`;try{return globalThis.chrome?.runtime?.getURL?.(e)||e}catch{return e}}(t),{cache:"no-store"}));if(!e.ok)throw new Error(`Unable to load popup locale: ${t}`);return e.json()}(i)}catch(t){console.warn("[popup-i18n] Locale load failed; using source-language fallbacks:",t),i=e,c={}}try{return g(document),function(){if(l)return;const t=document.getElementById("uiLanguageToggle");t&&(l=!0,t.addEventListener("click",(()=>{h().catch((t=>{console.warn("[popup-i18n] Unable to switch locale:",t)}))})))}(),i}finally{window.clearTimeout(o),document.documentElement?.classList?.remove?.("i18n-pending")}}()}();
