mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-06-19 23:50:51 +00:00
Replace the 'relative-time' element scripting with custom, translatable rewrite (#6154)
This is my take to fix #6078 Should also resolve #6111 As far as I can tell, Forgejo uses only a subset of the relative-time functionality, and as far as I can see, this subset can be implemented using browser built-in date conversion and arithmetic. So I wrote a JavaScript to format the relative-time element accordingly, and a Go binding to generate the translated elements. This is my first time writing Go code, and my first time coding for a large-scale server application, so please tell me if I'm doing something wrong, or if the whole approach is not acceptable. --- Screenshot: Localized times in Low German  Screenshot: The same with Forgejo in English  --- ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [x] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] I do not want this change to show in the release notes. - [ ] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6154 Reviewed-by: 0ko <0ko@noreply.codeberg.org> Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Co-authored-by: Benedikt Straub <benedikt-straub@web.de> Co-committed-by: Benedikt Straub <benedikt-straub@web.de>
This commit is contained in:
parent
37d566bdb0
commit
cf03286b5b
19 changed files with 698 additions and 35 deletions
|
@ -121,6 +121,18 @@ func NewWebContext(base *Base, render Render, session session.Store) *Context {
|
|||
return ctx
|
||||
}
|
||||
|
||||
func (ctx *Context) AddPluralStringsToPageData(keys []string) {
|
||||
for _, key := range keys {
|
||||
array, fallback := ctx.Locale.TrPluralStringAllForms(key)
|
||||
|
||||
ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)[key] = array
|
||||
|
||||
if fallback != nil {
|
||||
ctx.PageData["PLURALSTRINGS_FALLBACK"].(map[string][]string)[key] = fallback
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Contexter initializes a classic context for a request.
|
||||
func Contexter() func(next http.Handler) http.Handler {
|
||||
rnd := templates.HTMLRenderer()
|
||||
|
@ -208,6 +220,25 @@ func Contexter() func(next http.Handler) http.Handler {
|
|||
|
||||
ctx.Data["AllLangs"] = translation.AllLangs()
|
||||
|
||||
ctx.PageData["PLURAL_RULE_LANG"] = translation.GetPluralRule(ctx.Locale)
|
||||
ctx.PageData["PLURAL_RULE_FALLBACK"] = translation.GetDefaultPluralRule()
|
||||
ctx.PageData["PLURALSTRINGS_LANG"] = map[string][]string{}
|
||||
ctx.PageData["PLURALSTRINGS_FALLBACK"] = map[string][]string{}
|
||||
|
||||
ctx.AddPluralStringsToPageData([]string{"relativetime.mins", "relativetime.hours", "relativetime.days", "relativetime.weeks", "relativetime.months", "relativetime.years"})
|
||||
|
||||
ctx.PageData["DATETIMESTRINGS"] = map[string]string{
|
||||
"FUTURE": ctx.Locale.TrString("relativetime.future"),
|
||||
"NOW": ctx.Locale.TrString("relativetime.now"),
|
||||
}
|
||||
for _, key := range []string{"relativetime.1day", "relativetime.1week", "relativetime.1month", "relativetime.1year", "relativetime.2days", "relativetime.2weeks", "relativetime.2months", "relativetime.2years"} {
|
||||
// These keys are used for special-casing some time words. We only add keys that are actually translated, so that we
|
||||
// can fall back to the generic pluralized time word in the correct language if the special case is untranslated.
|
||||
if ctx.Locale.HasKey(key) {
|
||||
ctx.PageData["DATETIMESTRINGS"].(map[string]string)[key] = ctx.Locale.TrString(key)
|
||||
}
|
||||
}
|
||||
|
||||
next.ServeHTTP(ctx.Resp, ctx.Req)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue