mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-11-04 00:11:04 +00:00 
			
		
		
		
	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>
		
			
				
	
	
		
			63 lines
		
	
	
	
		
			3.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			63 lines
		
	
	
	
		
			3.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright 2024 The Forgejo Authors. All rights reserved.
 | 
						|
// SPDX-License-Identifier: MIT
 | 
						|
 | 
						|
package contexttest
 | 
						|
 | 
						|
import (
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"forgejo.org/modules/translation"
 | 
						|
	"forgejo.org/modules/translation/i18n"
 | 
						|
 | 
						|
	"github.com/stretchr/testify/assert"
 | 
						|
)
 | 
						|
 | 
						|
func TestPluralStringsForClient(t *testing.T) {
 | 
						|
	mockLocale := translation.MockLocale{}
 | 
						|
	mockLocale.MockTranslations = map[string]string{
 | 
						|
		"relativetime.mins" + i18n.PluralFormSeparator + "one":     "%d minute ago",
 | 
						|
		"relativetime.hours" + i18n.PluralFormSeparator + "one":    "%d hour ago",
 | 
						|
		"relativetime.days" + i18n.PluralFormSeparator + "one":     "%d day ago",
 | 
						|
		"relativetime.weeks" + i18n.PluralFormSeparator + "one":    "%d week ago",
 | 
						|
		"relativetime.months" + i18n.PluralFormSeparator + "one":   "%d month ago",
 | 
						|
		"relativetime.years" + i18n.PluralFormSeparator + "one":    "%d year ago",
 | 
						|
		"relativetime.mins" + i18n.PluralFormSeparator + "other":   "%d minutes ago",
 | 
						|
		"relativetime.hours" + i18n.PluralFormSeparator + "other":  "%d hours ago",
 | 
						|
		"relativetime.days" + i18n.PluralFormSeparator + "other":   "%d days ago",
 | 
						|
		"relativetime.weeks" + i18n.PluralFormSeparator + "other":  "%d weeks ago",
 | 
						|
		"relativetime.months" + i18n.PluralFormSeparator + "other": "%d months ago",
 | 
						|
		"relativetime.years" + i18n.PluralFormSeparator + "other":  "%d years ago",
 | 
						|
	}
 | 
						|
 | 
						|
	ctx, _ := MockContext(t, "/")
 | 
						|
	ctx.Locale = mockLocale
 | 
						|
	assert.True(t, ctx.Locale.HasKey("relativetime.mins"))
 | 
						|
	assert.True(t, ctx.Locale.HasKey("relativetime.weeks"))
 | 
						|
	assert.Equal(t, "%d minutes ago", ctx.Locale.TrString("relativetime.mins"+i18n.PluralFormSeparator+"other"))
 | 
						|
	assert.Equal(t, "%d week ago", ctx.Locale.TrString("relativetime.weeks"+i18n.PluralFormSeparator+"one"))
 | 
						|
 | 
						|
	assert.Empty(t, ctx.PageData)
 | 
						|
	ctx.PageData["PLURALSTRINGS_LANG"] = map[string][]string{}
 | 
						|
	assert.Empty(t, ctx.PageData["PLURALSTRINGS_LANG"])
 | 
						|
 | 
						|
	ctx.AddPluralStringsToPageData([]string{"relativetime.mins", "relativetime.hours"})
 | 
						|
	assert.Len(t, ctx.PageData["PLURALSTRINGS_LANG"], 2)
 | 
						|
	assert.Len(t, ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.mins"], 2)
 | 
						|
	assert.Len(t, ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.hours"], 2)
 | 
						|
	assert.Equal(t, "%d minute ago", ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.mins"][0])
 | 
						|
	assert.Equal(t, "%d minutes ago", ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.mins"][1])
 | 
						|
	assert.Equal(t, "%d hour ago", ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.hours"][0])
 | 
						|
	assert.Equal(t, "%d hours ago", ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.hours"][1])
 | 
						|
 | 
						|
	ctx.AddPluralStringsToPageData([]string{"relativetime.years", "relativetime.days"})
 | 
						|
	assert.Len(t, ctx.PageData["PLURALSTRINGS_LANG"], 4)
 | 
						|
	assert.Len(t, ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.mins"], 2)
 | 
						|
	assert.Len(t, ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.days"], 2)
 | 
						|
	assert.Len(t, ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.years"], 2)
 | 
						|
	assert.Equal(t, "%d minute ago", ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.mins"][0])
 | 
						|
	assert.Equal(t, "%d minutes ago", ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.mins"][1])
 | 
						|
	assert.Equal(t, "%d day ago", ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.days"][0])
 | 
						|
	assert.Equal(t, "%d days ago", ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.days"][1])
 | 
						|
	assert.Equal(t, "%d year ago", ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.years"][0])
 | 
						|
	assert.Equal(t, "%d years ago", ctx.PageData["PLURALSTRINGS_LANG"].(map[string][]string)["relativetime.years"][1])
 | 
						|
}
 |