【发布时间】:2017-03-12 23:22:21
【问题描述】:
我是 Jekyll 的新手,我目前正在创建一个用西班牙语写的博客。我想将 XML 时间模式转换为西班牙语的字符串。例如,我想要“Actualizado el 01 de enero de 2017”或类似的东西,而不是“2017 年 1 月 1 日更新”。有没有办法将date_to_string 转换为适合我需要的东西?谢谢。
【问题讨论】:
我是 Jekyll 的新手,我目前正在创建一个用西班牙语写的博客。我想将 XML 时间模式转换为西班牙语的字符串。例如,我想要“Actualizado el 01 de enero de 2017”或类似的东西,而不是“2017 年 1 月 1 日更新”。有没有办法将date_to_string 转换为适合我需要的东西?谢谢。
【问题讨论】:
目前,Jekyll 中没有任何东西可以开箱即用地做到这一点。您必须编写一些代码才能以您的语言获取月份的名称等。
这是一个例子:
<!-- Whitespace added for readability -->
{% assign m = page.date | date: "%-m" %}
{{ page.date | date: "%-d" }}
{% case m %}
{% when '1' %}Januar
{% when '2' %}Februar
{% when '3' %}März
{% when '4' %}April
{% when '5' %}Mai
{% when '6' %}Juni
{% when '7' %}Juli
{% when '8' %}August
{% when '9' %}September
{% when '10' %}Oktober
{% when '11' %}November
{% when '12' %}Dezember
{% endcase %}
{{ page.date | date: "%Y" }}
您可以在此处查看有关 Jekyll 日期格式的其他一些示例: http://alanwsmith.com/jekyll-liquid-date-formatting-examples
【讨论】:
虽然您没有要求提供多种语言,但我认为在此处添加它会很好。下面是我如何在博客文章等中添加多种语言。
_includes/date.html:
{% capture hide %}
{% if include.mode != 'month' %}
{% assign day = include.date | date: "%-d" %}
{% endif %}
{% if page.language == 'th' %}
{% assign m = include.date | date: "%-m" %}
{% case m %}
{% when '1' %}
{% capture month %}มกราคม{% endcapture %}
{% when '2' %}
{% capture month %}กุมภาพันธ์{% endcapture %}
{% when '3' %}
{% capture month %}มีนาคม{% endcapture %}
{% when '4' %}
{% capture month %}เมษายน{% endcapture %}
{% when '5' %}
{% capture month %}พฤษภาคม{% endcapture %}
{% when '6' %}
{% capture month %}มิถุนายน{% endcapture %}
{% when '7' %}
{% capture month %}กรกฎาคม{% endcapture %}
{% when '8' %}
{% capture month %}สิงหาคม{% endcapture %}
{% when '9' %}
{% capture month %}กันยายน{% endcapture %}
{% when '10' %}
{% capture month %}ตุลาคม{% endcapture %}
{% when '11' %}
{% capture month %}พฤศจิกายน{% endcapture %}
{% when '12' %}
{% capture month %}ธันวาคม{% endcapture %}
{% endcase %}
{% else %}
{% capture month %}{{ include.date | date: "%B" }}{% endcapture %}
{% endif %}
{% capture year %}{{ include.date | date: "%Y" }}{% endcapture %}
{% endcapture %}
_includes/post-time.html:
{% include date.html date=post.date %}
<time class="entry-time" itemprop="datePublished" datetime="{{ post.date | date_to_xmlschema }}">{% if page.language == 'th' %}{{ day }} {{ month }} {{ year }}{% else %}{{ month }} {{ day }}, {{ year }}{% endif %}</time>
然后我可以在任何需要上述 HTML 输出的地方使用{% include post-time.html %}。
如果您需要三种语言,还可以在 _includes/date.html 文件中添加额外的条件。
【讨论】: