2017-05-06 76 views
0

我在Squarespace上使用Foundry template,我需要將帖子頁面上的日期格式從英語更改爲葡萄牙語。而不是「5月6日」,我需要「6邁」。在巴西,我們使用模式dd/mm/yyyy。在這種情況下,我只想每天和每月,並翻譯所有月份(到:Jan,Fev,Mar,Abr,Mai,Jun,Jul,Ago,Set,Out,Nov,Dez)。在模板上更改日期格式

我已經看到人們正在爲其他語言解決這個問題。但不適用於葡萄牙語或鑄造模板。可以在Squarespace的頭部或頁腳上進行代碼注入。我只需要一個可以做到的Javascript,覆蓋這個主題的默認日期格式。

回答

1

我會通過下面的Javascript通過代碼注入插入它。請注意,雖然某些月份的縮寫是相同的,但爲了清晰起見,我將它們包括在內,以便其他人可以更加重用。此外,我用於鍵的縮寫(即原始月份縮寫)可能不是Squarespace實際使用的縮寫,因此可能需要更新它們。

<script> 
    (function() { 
     var dates = document.getElementsByClassName("dt-published date-highlight"); 
     var newDate; 
     var i,I; 
     // Create object with 'source' keys on the left, and 'output' values on the right. 
     var months = { 
      "Jan":"Jan", 
      "Feb":"Fev", 
      "Mar":"Mar", 
      "Apr":"Abr", 
      "May":"Mai", 
      "Jun":"Jun", 
      "Jul":"Jul", 
      "Aug":"Ago", 
      "Sep":"Set", 
      "Oct":"Out", 
      "Nov":"Nov", 
      "Dec":"Dez" 
     }; 
     // Loop through all dates, replacing months and reordering display. 
     // - Trim extra white space from beginning and end of date. 
     // - Replace multiple consecutive spaces with a single space. 
     // - Split by space into an array. 
     // - Replace month text based on 'months' object key:value pairs. 
     // - Convert array to string, rearranging display order of elements. 
     // - Set new date HTML. 
     for (i=0, I=dates.length; i<I; i++) { 
      newDate = dates[i].innerHTML.trim(); 
      newDate = newDate = newDate.replace(/ +/g, ' '); 
      newDate = newDate.split(" "); 
      newDate[0] = months[newDate[0]]; 
      newDate = newDate[1] + " " + newDate[0]; 
      dates[i].innerHTML = newDate; 
     } 
    })(); 
</script> 
+1

嗨布蘭登,代碼工作得很好。感謝您的幫助和澄清!您剛在結束