2017-05-06 89 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>