diff --git a/modules/context/context.go b/modules/context/context.go index 887cffdbf..7ae37208e 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -38,7 +38,6 @@ import ( "gitea.com/go-chi/session" chi "github.com/go-chi/chi/v5" "github.com/unknwon/com" - "github.com/unknwon/i18n" "github.com/unrolled/render" "golang.org/x/crypto/pbkdf2" ) @@ -738,15 +737,7 @@ func Contexter() func(next http.Handler) http.Handler { ctx.Data["UnitProjectsGlobalDisabled"] = unit.TypeProjects.UnitGlobalDisabled() ctx.Data["i18n"] = locale - ctx.Data["Tr"] = i18n.Tr - ctx.Data["Lang"] = locale.Language() ctx.Data["AllLangs"] = translation.AllLangs() - for _, lang := range translation.AllLangs() { - if lang.Lang == locale.Language() { - ctx.Data["LangName"] = lang.Name - break - } - } next.ServeHTTP(ctx.Resp, ctx.Req) diff --git a/modules/translation/translation.go b/modules/translation/translation.go index 977f2cdc2..fd38e4d51 100644 --- a/modules/translation/translation.go +++ b/modules/translation/translation.go @@ -25,17 +25,18 @@ type Locale interface { // LangType represents a lang type type LangType struct { - Lang, Name string + Lang, Name string // these fields are used directly in templates: {{range .AllLangs}}{{.Lang}}{{.Name}}{{end}} } var ( matcher language.Matcher - allLangs []LangType + allLangs []*LangType + allLangMap map[string]*LangType supportedTags []language.Tag ) // AllLangs returns all supported languages sorted by name -func AllLangs() []LangType { +func AllLangs() []*LangType { return allLangs } @@ -81,14 +82,17 @@ func InitLocales() { } i18n.SetDefaultLang("en-US") - allLangs = make([]LangType, 0, i18n.Count()-1) + allLangs = make([]*LangType, 0, i18n.Count()) + allLangMap = map[string]*LangType{} langs := i18n.ListLangs() - names := i18n.ListLangDescs() + descs := i18n.ListLangDescs() for i, v := range langs { - allLangs = append(allLangs, LangType{v, names[i]}) + l := &LangType{v, descs[i]} + allLangs = append(allLangs, l) + allLangMap[v] = l } - // Sort languages case insensitive according to their name - needed for the user settings + // Sort languages case-insensitive according to their name - needed for the user settings sort.Slice(allLangs, func(i, j int) bool { return strings.ToLower(allLangs[i].Name) < strings.ToLower(allLangs[j].Name) }) @@ -102,13 +106,18 @@ func Match(tags ...language.Tag) language.Tag { // locale represents the information of localization. type locale struct { - Lang string + Lang, LangName string // these fields are used directly in templates: .i18n.Lang } // NewLocale return a locale func NewLocale(lang string) Locale { + langName := "unknown" + if l, ok := allLangMap[lang]; ok { + langName = l.Name + } return &locale{ - Lang: lang, + Lang: lang, + LangName: langName, } } @@ -118,7 +127,16 @@ func (l *locale) Language() string { // Tr translates content to target language. func (l *locale) Tr(format string, args ...interface{}) string { - return i18n.Tr(l.Lang, format, args...) + if setting.IsProd { + return i18n.Tr(l.Lang, format, args...) + } + + // in development, we should show an error if a translation key is missing + s, ok := TryTr(l.Lang, format, args...) + if !ok { + log.Error("missing i18n translation key: %q", format) + } + return s } // Language specific rules for translating plural texts diff --git a/routers/install/install.go b/routers/install/install.go index eb2cd2346..98eeb5f8a 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -71,25 +71,16 @@ func Init(next http.Handler) http.Handler { Render: rnd, Session: session.GetSession(req), Data: map[string]interface{}{ + "i18n": locale, "Title": locale.Tr("install.install"), "PageIsInstall": true, "DbTypeNames": getDbTypeNames(), - "i18n": locale, - "Language": locale.Language(), - "Lang": locale.Language(), "AllLangs": translation.AllLangs(), - "CurrentURL": setting.AppSubURL + req.URL.RequestURI(), "PageStartTime": startTime, "PasswordHashAlgorithms": user_model.AvailableHashAlgorithms, }, } - for _, lang := range translation.AllLangs() { - if lang.Lang == locale.Language() { - ctx.Data["LangName"] = lang.Name - break - } - } ctx.Req = context.WithContext(req, &ctx) next.ServeHTTP(resp, ctx.Req) }) diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go index 588e432e3..e96e2142d 100644 --- a/routers/web/repo/blame.go +++ b/routers/web/repo/blame.go @@ -255,7 +255,7 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m commitCnt++ // User avatar image - commitSince := timeutil.TimeSinceUnix(timeutil.TimeStamp(commit.Author.When.Unix()), ctx.Data["Lang"].(string)) + commitSince := timeutil.TimeSinceUnix(timeutil.TimeStamp(commit.Author.When.Unix()), ctx.Locale.Language()) var avatar string if commit.User != nil { diff --git a/routers/web/repo/issue_content_history.go b/routers/web/repo/issue_content_history.go index 75951ca25..5b5aced6e 100644 --- a/routers/web/repo/issue_content_history.go +++ b/routers/web/repo/issue_content_history.go @@ -29,7 +29,7 @@ func GetContentHistoryOverview(ctx *context.Context) { return } - lang := ctx.Data["Lang"].(string) + lang := ctx.Locale.Language() editedHistoryCountMap, _ := issuesModel.QueryIssueContentHistoryEditedCountMap(db.DefaultContext, issue.ID) ctx.JSON(http.StatusOK, map[string]interface{}{ "i18n": map[string]interface{}{ @@ -55,17 +55,17 @@ func GetContentHistoryList(ctx *context.Context) { // render history list to HTML for frontend dropdown items: (name, value) // name is HTML of "avatar + userName + userAction + timeSince" // value is historyId - lang := ctx.Data["Lang"].(string) + lang := ctx.Locale.Language() var results []map[string]interface{} for _, item := range items { var actionText string if item.IsDeleted { - actionTextDeleted := i18n.Tr(lang, "repo.issues.content_history.deleted") + actionTextDeleted := ctx.Locale.Tr("repo.issues.content_history.deleted") actionText = "" + actionTextDeleted + "" } else if item.IsFirstCreated { - actionText = i18n.Tr(lang, "repo.issues.content_history.created") + actionText = ctx.Locale.Tr("repo.issues.content_history.created") } else { - actionText = i18n.Tr(lang, "repo.issues.content_history.edited") + actionText = ctx.Locale.Tr("repo.issues.content_history.edited") } timeSinceText := timeutil.TimeSinceUnix(item.EditedUnix, lang) results = append(results, map[string]interface{}{ diff --git a/templates/admin/process-row.tmpl b/templates/admin/process-row.tmpl index 814727e7f..146ecc7b2 100644 --- a/templates/admin/process-row.tmpl +++ b/templates/admin/process-row.tmpl @@ -2,7 +2,7 @@
{{$.i18n.Tr "repo.activity.merged_prs_label"}} #{{.Index}} {{.Issue.Title | RenderEmoji}} - {{TimeSinceUnix .MergedUnix $.Lang}} + {{TimeSinceUnix .MergedUnix $.i18n.Lang}}
{{end}}{{$.i18n.Tr "repo.activity.opened_prs_label"}} #{{.Index}} {{.Issue.Title | RenderEmoji}} - {{TimeSinceUnix .Issue.CreatedUnix $.Lang}} + {{TimeSinceUnix .Issue.CreatedUnix $.i18n.Lang}}
{{end}} @@ -188,7 +188,7 @@{{$.i18n.Tr "repo.activity.closed_issue_label"}} #{{.Index}} {{.Title | RenderEmoji}} - {{TimeSinceUnix .ClosedUnix $.Lang}} + {{TimeSinceUnix .ClosedUnix $.i18n.Lang}}
{{end}} @@ -207,7 +207,7 @@{{$.i18n.Tr "repo.activity.new_issue_label"}} #{{.Index}} {{.Title | RenderEmoji}} - {{TimeSinceUnix .CreatedUnix $.Lang}} + {{TimeSinceUnix .CreatedUnix $.i18n.Lang}}
{{end}} @@ -231,7 +231,7 @@ {{else}} {{.Title | RenderEmoji}} {{end}} - {{TimeSinceUnix .UpdatedUnix $.Lang}} + {{TimeSinceUnix .UpdatedUnix $.i18n.Lang}} {{end}} diff --git a/templates/repo/commit_page.tmpl b/templates/repo/commit_page.tmpl index 931201fed..331c439c0 100644 --- a/templates/repo/commit_page.tmpl +++ b/templates/repo/commit_page.tmpl @@ -47,7 +47,7 @@ {{avatarByEmail .Commit.Author.Email .Commit.Author.Email 28 "mr-3"}} {{.Commit.Author.Name}} {{end}} - {{TimeSince .Commit.Author.When $.Lang}} + {{TimeSince .Commit.Author.When $.i18n.Lang}} {{if or (ne .Commit.Committer.Name .Commit.Author.Name) (ne .Commit.Committer.Email .Commit.Author.Email)}} {{.i18n.Tr "repo.diff.committed_by"}} {{if ne .Verification.CommittingUser.ID 0}} @@ -169,7 +169,7 @@ {{else}} {{.NoteCommit.Author.Name}} {{end}} - {{TimeSince .NoteCommit.Author.When $.Lang}} + {{TimeSince .NoteCommit.Author.When $.i18n.Lang}}{{RenderNote $.Context .Note $.RepoLink $.Repository.ComposeMetas}}diff --git a/templates/repo/commits_list.tmpl b/templates/repo/commits_list.tmpl index e8ac4020f..86ad83520 100644 --- a/templates/repo/commits_list.tmpl +++ b/templates/repo/commits_list.tmpl @@ -76,9 +76,9 @@ {{end}} {{if .Committer}} -