PageSourceSearch

https://www.k-viewhotel.jp/js/translate.js

js k-viewhotel.jp collected 2026-10-06 17:03:32 UTC 2,455 bytes, 79 lines download raw bytes

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.