maiki
March 7, 2022, 9:54pm
1
Continuing the discussion from Human-readable time difference ("3 years ago…") :
In this case I am okay with my solution:
{{ with $profile.created_at }}
{{ $born := time . }}
<p>{{ sub now.Year $born.Year }} years old</p>
{{ end }}
{{ end }}
However, what if I wanted to check if it is 1 year or less, so I produce “1 year old” in that case?
Well, I know how to do that, but I could have swore I once saw a clever way of doing this kind of thing: year/years, Month/Months, etc.
So… what is that pattern called? And is there a function that will assist me in applying it?
i18n/en.toml
[year]
one = "year"
other = "years"
template
{{ T "year" 1 }} --> year
{{ T "year" 2 }} --> years
{{ T "year" 0 }} --> years
1 Like
OR…
i18n/en.toml
[age_in_years]
one = "{{ . }} year old"
other = "{{ . }} years old"
template
{{ T "age_in_years" 1 }} --> 1 year old
{{ T "age_in_years" 2 }} --> 2 years old
{{ T "age_in_years" 0 }} --> 0 years old
1 Like
maiki
March 7, 2022, 10:21pm
4
Yep, that was it: Multilingual Mode | Hugo
Thanks! Now I know where to check when I need that.
1 Like
Also note, that this too is an internal feature of Golang and it’s more powerful than we think for languages that have weird plural formats (some east European languages for instance have a separate syntax for 1, 2, and more instead of 1 and more like english):
const (
Other Form = iota
Zero
One
Two
Few
Many
)
The words above should all work in the i18n-files.
1 Like
system
Closed
March 10, 2022, 7:41am
6
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.