Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make time.Format localized #8797

Closed
bep opened this issue Jul 26, 2021 · 13 comments · Fixed by #8804
Closed

Make time.Format localized #8797

bep opened this issue Jul 26, 2021 · 13 comments · Fixed by #8804

Comments

@bep
Copy link
Member

bep commented Jul 26, 2021

https://github.com/go-playground/locales

It looks beautifully complete, and has a lot of the stuff that many people ask about, esp. date formatting:

https://github.com/go-playground/locales/blob/master/rules.go#L45

Not sure how to go about with this (esp. considering go-i18n, but I suspect we can take this in 2 round), but one should be able to do something ala:

Week day: {{ site.Language.Translator.WeekDayShort .Date }}

Translator ...?

/cc @jmooring @regisphilibert

@bep bep added the Proposal label Jul 26, 2021
@jmooring
Copy link
Member

jmooring commented Jul 26, 2021

Given date = 2021-02-26T05:22:00-07:00 in frontmatter, I am hopeful that something similar to this would be possible:

Content Code Result
post/foo.en.md {{ .Date.Format "Mon, January 2, 2006"}} Fri, February 26, 2021
post/foo.es.md {{ .Date.Format "Mon, January 2, 2006"}} V, febrero 26, 2021

That's just a translation of two strings, and I'm not really sure how helpful it would be. Perhaps a better example would be:

Content Code Result
post/foo.en.md {{ .Date.Localize.Long }} Friday, February 26, 2021
post/foo.es.md {{ .Date.Localize.Long }} el viernes 26 de febrero de 2021

@regisphilibert
Copy link
Member

regisphilibert commented Jul 26, 2021

I think it's great to wrap this in .Date.Localize so we can have: .Date.Localize.MonthAbbreviated, .Date.Localize.MonthWide etc...

Though I think we'll have to document what those wordings means (WeekdayNarrow, WeekdayShort, WeekdayWide etc...) I couldn't find any examples in the two links shared in this thread.

I educated myself: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat

@jmooring
Copy link
Member

@regisphilibert

Examples:
https://github.com/go-playground/locales/blob/master/en_US/en_US.go#L61-L74

@bep
Copy link
Member Author

bep commented Jul 26, 2021

post/foo.es.md | {{ .Date.Localize.Long }} | el viernes 26 de febrero de 2021

What if I want to list the Spanish posts on the English site?

@regisphilibert
Copy link
Member

regisphilibert commented Jul 26, 2021

What if I want to list the Spanish posts on the English site?

It's an edge case and you should work for it :)

People will need to understand that .Date.Localize is a page method and as such it relies on the page's data to find the proper language to use.

But we could also introduce a way to create a localized date. Maybe timeLocalized which would take a second parameter for the language:

{{ $EsDate := timeLocalized (now.Unix) "es" }}
hoy dia: {{ $EsDate.Weekday.Long }}

@jmooring
Copy link
Member

@bep You are making my head hurt.

@bep
Copy link
Member Author

bep commented Jul 26, 2021

It's an edge case and you should work for it :)

You, maybe, but why should I work for it.

Week day: {{ site.Language.Translator.WeekDayShort .Date }}

The above would solve it for all cases without any magic. And it's much simpler to implement and also more efficient.

@regisphilibert
Copy link
Member

Now, I do have a few questions, number 3 being the most important (You can find them better formatted on the page shared above)

As you I mean the users who want to print spanish content on their english pages.

I can live with you suggestion and it does improve the way people have dealt with localized date before (data file + i18n) but you will still have to work with several methods to build one date.

A simple example to write: Tuesday: February 4 2021 in engish and Mardi: 3 février 2006 in french (I'm not even considering the 2nd, 3rd issue.

<time datetime="{{ .Date.Format "2006-01-02T15:04:05-0700" }}">
{{ if eq site.Language.Lang "fr" }}
 {{ site.Language.Translator.WeekDayLong .Date }}: {{ .Date.Format "2" }} {{ site.Language.Translator.MonthWide .Date }}  {{ .Date.Year }}
{{ else }}
{{ .Date.Format "Monday: January 2 2006  }}
</time>

I guess what most users would be thrilled about is an easy way to print a localized date similar to .Date.Format without the gymnastic illustrated above.

@bep
Copy link
Member Author

bep commented Jul 26, 2021

We could try to make a site.Language.Translator.FormatDate method that works the same as time.Format.

@bep
Copy link
Member Author

bep commented Jul 26, 2021

Or we could do {{ time.Format .Date "Monday: January 2 2006" }}.

@regisphilibert
Copy link
Member

It's all in the perspective of UX/DX.

If you're working with english date you can rely on the page .Date object which sports its own .Format but if using another language, you should use site.Language.Translator.FormatDate .Date.

Which means theme creators will need to use systematically that translator method on the site.Language which seems a bit remote. As people are used to using .Date.Format from the page context and dateFormat from the rest, I wonder how we can introduce something similar or even the same with an optional third argument for the language code.

@bep bep changed the title Somehow integrate github.com/go-playground/locales Make time.Format localized Jul 27, 2021
@bep
Copy link
Member Author

bep commented Jul 27, 2021

@regisphilibert I'm scoping down this particular issue into making time.Format (which we already have, which also supports a string input) localized.

@bep bep added Enhancement and removed Proposal labels Jul 27, 2021
bep added a commit to bep/hugo that referenced this issue Jul 27, 2021
bep added a commit to bep/hugo that referenced this issue Jul 27, 2021
bep added a commit to bep/hugo that referenced this issue Jul 27, 2021
bep added a commit to bep/hugo that referenced this issue Jul 27, 2021
@bep bep closed this as completed in #8804 Jul 27, 2021
bep added a commit that referenced this issue Jul 27, 2021
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants