Hugo,如何将.groupByDate本地化?

huangapple go评论80阅读模式
英文:

Hugo, how to localized .groupByDate

问题

在我的模板中,我按月份对我的帖子进行排序,就像这样:

{{ range (where site.RegularPages "Type" "in" site.Params.mainSections).GroupByDate "January, 2006" -}}
  <h1>{{ .Key }}</h1> // 输出:March, 2022
  {{ range (where .Pages ".Params.unlisted" "!=" "true") }}
    <div>{{ time.Format "02 January" .Date }} - {{ .Title }}</div> // 输出:01 Mars - This is the title of my post
  {{ end }}
{{ end }}

对于每篇帖子,time.Format会将我的日期本地化为我的语言(这里是法语)。

但是按月份分组的标题仍然是英文的(这里是h1)。我该如何格式化我的{{ .Key }},以便它可以本地化为我的语言,并显示为“Mars, 2022”而不是“March, 2022”?

英文:

In my template I sort by months my post like this:

{{ range (where site.RegularPages &quot;Type&quot; &quot;in&quot; site.Params.mainSections).GroupByDate &quot;January, 2006&quot; -}}
  &lt;h1&gt;{{ .Key }}&lt;/h1&gt; // output: March, 2022
  {{ range (where .Pages &quot;.Params.unlisted&quot; &quot;!=&quot; &quot;true&quot;) }}
    &lt;div&gt;{{ time.Format &quot;02 January&quot; .Date }} - {{ .Title }}&lt;/div&gt; // output: 01 Mars - This is the title of my post
  {{ end }}
{{ end }}

For every post the time.Format localized my date in my language (here in French).

But the title witch is group by month is still in English (here it's the h1). How can I format my {{ .Key }} so it can be localized in my own language and show "Mars, 2022" instead of "March, 2022" ?

答案1

得分: 1

如何这样处理:

{{ time.Format "02 January" .Key }}

或者我有什么遗漏的地方吗?

更新:你可以像这样按月份排序:

{{ range (where site.RegularPages "Type" "in" site.Params.mainSections).GroupByDate "1 2006" -}}

然后你可以通过空格拆分 .Key,并为本地化的月份创建一个手动查找。虽然不太美观,但可以完成工作。

英文:

How about:

{{ time.Format &quot;02 January&quot; .Key }}

Or is there something I am missing?

Update: You can sort by months like this:

{{ range (where site.RegularPages &quot;Type&quot; &quot;in&quot; site.Params.mainSections).GroupByDate &quot;1 2006&quot; -}}

Then you can split your .Key by a space and create a manual lookup for your localized month. Not very pretty, but it gets the job done.

答案2

得分: 0

自从Hugo版本0.97以后,它按预期工作,我们不再需要一些丑陋的技巧来使文本显示为相应的语言。

现在你可以使用.GroupByDate,它将使用你配置文件中的语言设置。

英文:

Since hugo version 0.97 it works as expected, we don't need some ugly hack to have the text in the corresponding language.

Now you can use .GroupByDate and it will use your language settings in your config file.

huangapple
  • 本文由 发表于 2022年4月2日 01:43:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/71710900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定