1function setLanguage(lang) { 2 localStorage.setItem("preferredLanguage", lang); 3 location.reload(); 4} 5 6document.addEventListener("DOMContentLoaded", () => { 7 const lang = localStorage.getItem("preferredLanguage"); 8 const excludedDirs = ["/wedding"]; 9 const currentPath = window.location.pathname; 10 11 const shouldTranslate = lang && !excludedDirs.some(dir => currentPath.startsWith(dir)); 12 if (shouldTranslate && lang !== "ja") { 13 translatePage(lang); 14 } 15}); 16 17async function translatePage(targetLang) { 18 const textNodes = []; 19 const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false); 20 while (walker.nextNode()) { 21 const node = walker.currentNode; 22 if ( 23 node.nodeValue.trim() && 24 !node.parentElement.closest('[data-no-translate]') 25 ) { 26 textNodes.push(node); 27 } 28 } 29 const originalTexts = textNodes.map(n => n.nodeValue.trim()); 30 31 if (originalTexts.length === 0) { 32 console.warn("翻訳対象ã®ããã¹ããè¦ã¤ããã¾ããã§ããã"); 33 return; 34 } 35 36 // 128ä»¶ãã¤ã«åå² 37 const maxText = 128;// 1åã®ãªã¯ã¨ã¹ãã§ç¿»è¨³ã§ããæå¤§ããã¹ãæ°(â»æç¤ºã«ã¯é¢ä¿ãªããæåæ°ã®æ¨å¥¨ä¸é:5,000æå) 38 const chunks = []; 39 for (let i = 0; i < originalTexts.length; i += maxText) { 40 chunks.push(originalTexts.slice(i, i + maxText)); 41 } 42 43 const allTranslatedTexts = []; 44 45 for (const chunk of chunks) { 46 try { 47 const res = await fetch("/api/translate.php", { 48 method: "POST", 49 headers: { "Content-Type": "application/json" }, 50 body: JSON.stringify({ 51 text: chunk, 52 target: targetLang 53 }) 54 }); 55 56 const data = await res.json(); 57 const translated = data.data?.translations; 58 59 if (!translated || translated.length !== chunk.length) { 60 console.warn("ç¿»è¨³çµææ°ãä¸è´ããªããåå¾ã§ãã¾ããã§ããã"); 61 return; 62 } 63 64 allTranslatedTexts.push(...translated.map(t => t.translatedText)); 65 } catch (err) { 66 console.error("翻訳ã¨ã©ã¼:", err); 67 return; 68 } 69 } 70 71 // ç¿»è¨³çµæãåæ 72 if (allTranslatedTexts.length === textNodes.length) { 73 textNodes.forEach((node, i) => { 74 node.nodeValue = allTranslatedTexts[i]; 75 }); 76 } else { 77 console.warn("æçµçãªç¿»è¨³çµææ°ãä¸è´ãã¾ãã"); 78 } 79}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.