<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>多國語系 &#8211; 小豬日常</title>
	<atom:link href="https://piglife.tw/tag/%e5%a4%9a%e5%9c%8b%e8%aa%9e%e7%b3%bb/feed/" rel="self" type="application/rss+xml" />
	<link>https://piglife.tw</link>
	<description>Hello World，一個紀錄生活與學習的地方</description>
	<lastBuildDate>Fri, 12 Dec 2025 14:48:11 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://piglife.tw/wp-content/uploads/2017/10/cropped-logo-1-32x32.png</url>
	<title>多國語系 &#8211; 小豬日常</title>
	<link>https://piglife.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>WordPress 實作瀏覽器語言自動轉址功能筆記</title>
		<link>https://piglife.tw/technical-notes/wordpress-browser-language-redirect/</link>
					<comments>https://piglife.tw/technical-notes/wordpress-browser-language-redirect/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Tue, 31 Dec 2024 02:39:25 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[cookie]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[前端]]></category>
		<category><![CDATA[多國語系]]></category>
		<category><![CDATA[瀏覽器語言]]></category>
		<category><![CDATA[自動轉址]]></category>
		<guid isPermaLink="false">https://piglife.tw/?p=435</guid>

					<description><![CDATA[介紹如何在 WordPress 網站中利用 JavaScript 根據瀏覽器語言自動轉址至指定語言頁...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在多國語系網站中，根據用戶瀏覽器語言自動轉址至對應語言頁面是提升用戶體驗的常見需求。本文適合具備基礎程式能力的工程師或自學者，說明如何利用 JavaScript 搭配 Cookie 控制，實現 WordPress 網站的語言自動轉址功能。</p>
<h2 class="wp-block-heading">教學步驟</h2>
<h3 class="wp-block-heading">1. JavaScript 程式碼說明</h3>
<p>將以下程式碼加入 WordPress 主題的 <code>wp_footer</code> 或 JavaScript 檔案中，確保頁面載入時執行：</p>
<pre><code class="lang-javascript language-javascript javascript">document.addEventListener(&quot;DOMContentLoaded&quot;, function () {
  // 設置 Cookie
  function setCookie(name, value, days) {
    const date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    const expires = &quot;expires=&quot; + date.toUTCString();
    document.cookie = name + &quot;=&quot; + value + &quot;;&quot; + expires + &quot;;path=/&quot;;
  }

  // 取得 Cookie
  function getCookie(name) {
    const nameEQ = name + &quot;=&quot;;
    const ca = document.cookie.split(&#039;;&#039;);
    for (let i = 0; i &lt; ca.length; i++) {
      let c = ca[i].trim();
      if (c.indexOf(nameEQ) === 0) {
        return c.substring(nameEQ.length, c.length);
      }
    }
    return null;
  }

  // 僅在首頁執行
  if (window.location.pathname === &#039;/&#039;) {
    if (!getCookie(&#039;redirected&#039;)) {
      const browserLanguage = navigator.language || navigator.userLanguage;
      // 繁體中文判斷
      if (browserLanguage.startsWith(&#039;zh&#039;) &amp;&amp; window.location.pathname !== &#039;/zh-tw/&#039;) {
        setCookie(&#039;redirected&#039;, &#039;true&#039;, 1); // 設置 1 天有效期
        window.location.href = &#039;https://xxxxx.com/zh-tw/&#039;;
      }
    }
  }
});</code></pre>
<h3 class="wp-block-heading">2. 功能解析</h3>
<ul>
<li><strong>Cookie 操作函數</strong>：<code>setCookie</code> 用於建立帶有效期的 Cookie，<code>getCookie</code> 用於檢查 Cookie 是否存在。</li>
<li><strong>語言判斷與轉址</strong>：透過 <code>navigator.language</code> 取得瀏覽器語言，判斷是否為繁體中文（<code>zh-TW</code> 或 <code>zh-HK</code>），並且當前不在繁體中文頁面時執行轉址。</li>
<li><strong>避免重複轉址</strong>：利用名為 <code>redirected</code> 的 Cookie 記錄轉址狀態，避免用戶在有效期內重複被轉址。</li>
</ul>
<h3 class="wp-block-heading">3. 使用方法</h3>
<ul>
<li>將程式碼加入 WordPress 主題或插件中，確保在頁面載入時執行。</li>
<li>測試時可透過瀏覽器語言設定模擬不同語言環境，確認轉址是否正常。</li>
<li>使用瀏覽器開發者工具檢查 <code>redirected</code> Cookie 是否正確設置，確保轉址邏輯生效且不重複。</li>
</ul>
<h3 class="wp-block-heading">4. 注意事項</h3>
<ul>
<li>本程式碼僅在首頁生效，如需其他頁面轉址，需擴充路徑判斷邏輯。</li>
<li>Cookie 有效期可依需求調整。</li>
<li>可擴展語言判斷條件，支持更多語言轉址。</li>
<li>注意 SEO 影響，避免搜尋引擎因轉址影響索引。</li>
</ul>
<h3 class="wp-block-heading">範例應用場景</h3>
<p>假設網站有英文首頁 <code>https://xxxxx.com/</code> 與繁體中文頁面 <code>https://xxxxx.com/zh-tw/</code>，當用戶瀏覽器語言為繁體中文時，會自動導向繁體中文頁面，提升使用者體驗與網站國際化支持。</p>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-text language-text text">document.addEventListener(&quot;DOMContentLoaded&quot;, function () {
  // 設置 Cookie 的函數
  function setCookie(name, value, days) {
    const date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    const expires = &quot;expires=&quot; + date.toUTCString();
    document.cookie = name + &quot;=&quot; + value + &quot;;&quot; + expires + &quot;;path=/&quot;;
  }

  // 獲取 Cookie 的函數
  function getCookie(name) {
    const nameEQ = name + &quot;=&quot;;
    const ca = document.cookie.split(&#039;;&#039;);
    for (let i = 0; i &lt; ca.length; i++) {
      let c = ca[i].trim();
      if (c.indexOf(nameEQ) === 0) {
        return c.substring(nameEQ.length, c.length);
      }
    }
    return null;
  }

  // 確保僅在首頁執行
  if (window.location.pathname === &#039;/&#039;) {
    // 檢查是否已設置轉址 Cookie
    if (!getCookie(&#039;redirected&#039;)) {
      // 獲取瀏覽器語言
      const browserLanguage = navigator.language || navigator.userLanguage;

      // 判斷是否為繁體中文 (zh-TW 或 zh-HK)
      if (browserLanguage.startsWith(&#039;zh&#039;) &amp;&amp; window.location.pathname !== &#039;/zh-tw/&#039;) {
        // 設置轉址 Cookie，有效期為 1 天
        setCookie(&#039;redirected&#039;, &#039;true&#039;, 1);

        // 執行轉址
        window.location.href = &#039;https://xxxxx.com/zh-tw/&#039;;
      }
    }
  }
});</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/wordpress-browser-language-redirect/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
