English 中文(简体)
Jekyll 的当地具体日期
原标题:Locale specific date in jekyll

我在尝试创建网站的Jekyll 我使用Jekyll的诱杀装置

默认配置有页面归档, 所有职位都按日期的年份和月份分类。 目前月以英文出现。 我查看了代码, 这是用于设定日期的节选 :

{% capture this_month %}{{ post.date | date: "%B" }}{% endcapture %}

我找到了很多信息 < a href=" "http://guides.rubyonrailps.org/i18n.html#adding-date-time-formats" rel="noreferrer">这里 ,所以有办法指定想要的地方。但是你怎么让Jekyll尊重它?

default_locale: "lt"

config.yml 中,自然不能工作。

问题回答

您可以使用“液体日期”格式覆盖当前月份 :

{% 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" }}

如果您的日期是,例如2015-02-20, 输出将是 20 Februar 2015

由于 i18n 无法在 Github 页面上查阅, 我以 < a href=" https://stackoverflow.com/a/29757806/48136" 为基础, 回答@Kleo Petroff 和 < a href=" https://stackoverflow.com/a/329914455/48136" 回答@Falc 的 。

代码几乎完全一样,

{% capture i18n_date %}
{{ page.date | date: "%-d" }}
{% assign m = page.date | date: "%-m" | minus: 1 %}
{{ site.data.fr.months[m] }}
{{ page.date | date: "%Y" }}
{% endcapture %}

我在上述代码中设定了以下数据结构(可以是 ,也可以是 文件),文件是 :

months:
    - Janvier
    - Février
    - Mars
    - Avril
    - Mai
    - Juin
    - Juillet
    - Aout
    - Septembre
    - Octobre
    - Novembre
    - Décembre

Note that page.date | date: "%-m" output the month number as a string, ie June number is actually "6" not 6, liquid silently casts that string to a number when piping the minus filter. During development it was not something I was aware and thus liquid didn t returned anything when passing m with the value "6" to site.data.fr.months[m], I only saw the trick when looking at Falc answer.

My turn to share my solution without plugin inspired by the previous ones: I ve created an include with some parameters like: {% translated_date.html ... %}

想法是使用日期过滤语法( 如 : "% A% - d% B% Y" ) 翻译符合格式的月份和天数名称。 用于翻译的字符串在 中的 amml 文件仓库中。

可在Repo < a href="https://github.com/oncleben31/jekyll-date-basic-i18n/" rel="noreferr">oncleben31/jekyll-date-basic-i18n 上查阅的代码和使用。

我的博客中与Jekyll来源的融合实例,见Repo oncleben31/oncleben31-cc 。看看布局 post.html home.html

我开始使用"https://github.com/gacha/gacha.id.lv/blob/master/_plugins/i18n_filter.rb" rel=“nofollow”>i18n 插件,由@mpictas提出,但当Jekyll重新生成一个页面时,它开始打印“error”而不是本地化日期。所以我删除插件并开始使用这个简单的代码,类似于“case/When”解决方案 :

{% assign months = "Enero|Febrero|Marzo|Abril|Mayo|Junio|Julio|Agosto|Septiembre|Octubre|Noviembre|Diciembre" | split: "|" %}
{% assign m = page.date | date: "%-m" | minus: 1 %}
{% assign day = page.date | date: "%d" %}
{% assign month = months[m] %}
{% assign year = page.date | date: "%Y" %}
<span class="date">{{ day }}/{{ month }}/{{ year }}</span>

您也可以在 Ruby 文件中写入自己的液体过滤器, 如 :

module DateFilter
  MONTHS = %w(Januar Februar März April Mai Juni July August September Oktober November Dezember)

  def german_long_month(input)
    MONTHS[input.strftime("%m").to_i - 1]
  end
end

Liquid::Template.register_filter(DateFilter)

当您将此文件放入您 Jekyll 站点的 plugins 文件夹时, 您可以像其它过滤器一样使用模板文件中的过滤器 。

{{ post.date | german_long_month }}

com/Anthony-Gaudino/jekyll-多重语言-plutin" rel="nofollow">jekyll-多重语言-plutin :

在模板中简单使用 :

{% assign months = "january|february|march|april|may|june|july|august|september|october|november|december" | split: "|" %}
{% assign m = post.date | date: "%-m" | minus: 1 %}
{% assign day = post.date | date: "%d" %}
{% assign month = months[m] %}
{% assign year = post.date | date: "%Y" %}
<span class="post-meta">{{day}} {% t month %} {{year}}</span>

然后在, ./pl.yml , ./ any-language.yml 中:

january: January
february: February
march: March
april: April
may: May
june: June
july: July
august: August
september: September
october: October
november: November
december: December

我正致力于将GitHub页面(没有插件)完全自动化的翻译到当地,

{{ page.date | date: "%-d" }}
{% assign m = page.date | date: "%-m" | minus: 1 %}
{{ site.data.ui-text[site.locale].months[m] }}
{{ page.date | date: "%Y" }}

在 _config.yml 中,您可以设置默认的站点位置,像我的情况一样:

locale: "es-ES"

在 _data 中,我有一个YAML文件(i-text.yaml),其中载有:

# English (default)
# -----------------
en: &DEFAULT_EN
  months: 
    - January 
    - February 
    - March 
    - April 
    - May 
    - June 
    - July
    - August 
    - September 
    - October 
    - November 
    - December

en-US:
  <<: *DEFAULT_EN

# Spanish
# -------
es: &DEFAULT_ES

months:
    - Enero
    - Febrero
    - Marzo
    - Abril
    - Mayo
    - Junio
    - Julio
    - Agosto
    - Septiembre
    - Octubre
    - Noviembre
    - Diciembre

es-ES:
  <<: *DEFAULT_ES

# french
# -------
fr: &DEFAULT_FR
months:
    - Janvier
    - Février
    - Mars
    - Avril
    - Mai
    - Juin
    - Juillet
    - Aout
    - Septembre
    - Octobre
    - Novembre
    - Décembre




相关问题
时间段

我有一个时间储存在2010-01-0101:01。

How do I get my Python date in the format that I want?

I m reading a date from an Excel cell in Python (using .Value on the cell)... the result that I get is: 07/06/10 00:00:00 I thought this was a string, and so went about trying to figure out how to ...

Getting date string from getdate method

I need date string using sql statement like.. select getDate() this will return 2010-06-08 16:31:47.667 but I need in this format 201006081631 = yyyymmddhoursmin How can I get this? Thanks

How To assign null values in MySql using VB.NET

I am not sure why VB makes everything such a pain. I have a fields which stores the date and time in the format required to store in MySQL database Dim AppDate As String = String.Empty If Not ...

Convert to dd/mmm/yyyy

I am retrieving date from MySQL in the format yyyy/mm/dd 00:00:00. I want to convert this date into format dd/MMM/yyyy in PHP.

Oracle s default date format is YYYY-MM-DD, WHY?

Oracle s default date format is YYYY-MM-DD. Which means if I do: select some_date from some_table ...I lose the time portion of my date. Yes, I know you can "fix" this with: alter session set ...

热门标签