Initial support for localization and pluralization with go-i18n-JSON-v2 format

This commit is contained in:
Benedikt Straub 2024-12-30 20:03:37 +01:00
parent 376a2e19ea
commit a2787bb09e
No known key found for this signature in database
GPG key ID: E5C73DDA51986AEC
61 changed files with 1317 additions and 51 deletions

View file

@ -8,8 +8,10 @@ import (
"html/template"
"slices"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
)
// This file implements the static LocaleStore that will not watch for changes
@ -18,6 +20,9 @@ type locale struct {
store *localeStore
langName string
idxToMsgMap map[int]string // the map idx is generated by store's trKeyToIdxMap
newStyleMessages map[string]string
pluralRule PluralFormRule
}
var _ Locale = (*locale)(nil)
@ -38,8 +43,19 @@ func NewLocaleStore() LocaleStore {
return &localeStore{localeMap: make(map[string]*locale), trKeyToIdxMap: make(map[string]int)}
}
const (
PluralFormSeparator string = "\036"
)
// A note about pluralization rules.
// go-i18n supports plural rules in theory.
// In practice, it relies on another library that hardcodes a list of common languages
// and their plural rules, and does not support languages not hardcoded there.
// So we pretend that all languages are English and use our own function to extract
// the correct plural form for a given count and language.
// AddLocaleByIni adds locale by ini into the store
func (store *localeStore) AddLocaleByIni(langName, langDesc string, source, moreSource []byte) error {
func (store *localeStore) AddLocaleByIni(langName, langDesc string, pluralRule PluralFormRule, source, moreSource []byte) error {
if _, ok := store.localeMap[langName]; ok {
return ErrLocaleAlreadyExist
}
@ -47,7 +63,7 @@ func (store *localeStore) AddLocaleByIni(langName, langDesc string, source, more
store.langNames = append(store.langNames, langName)
store.langDescs = append(store.langDescs, langDesc)
l := &locale{store: store, langName: langName, idxToMsgMap: make(map[int]string)}
l := &locale{store: store, langName: langName, idxToMsgMap: make(map[int]string), pluralRule: pluralRule, newStyleMessages: make(map[string]string)}
store.localeMap[l.langName] = l
iniFile, err := setting.NewConfigProviderForLocale(source, moreSource)
@ -78,6 +94,98 @@ func (store *localeStore) AddLocaleByIni(langName, langDesc string, source, more
return nil
}
func RecursivelyAddTranslationsFromJSON(locale *locale, object map[string]any, prefix string) error {
for key, value := range object {
var fullkey string
if prefix != "" {
fullkey = prefix + "." + key
} else {
fullkey = key
}
switch v := value.(type) {
case string:
// Check whether we are adding a plural form to the parent object, or a new nested JSON object.
if key == "zero" || key == "one" || key == "two" || key == "few" || key == "many" {
locale.newStyleMessages[prefix+PluralFormSeparator+key] = v
} else if key == "other" {
locale.newStyleMessages[prefix] = v
} else {
locale.newStyleMessages[fullkey] = v
}
case map[string]any:
err := RecursivelyAddTranslationsFromJSON(locale, v, fullkey)
if err != nil {
return err
}
case nil:
default:
return fmt.Errorf("Unrecognized JSON value '%s'", value)
}
}
return nil
}
func (store *localeStore) AddToLocaleFromJSON(langName string, source []byte) error {
locale, ok := store.localeMap[langName]
if !ok {
return ErrLocaleDoesNotExist
}
var result map[string]any
if err := json.Unmarshal(source, &result); err != nil {
return err
}
return RecursivelyAddTranslationsFromJSON(locale, result, "")
}
func (l *locale) LookupNewStyleMessage(trKey string) string {
if msg, ok := l.newStyleMessages[trKey]; ok {
return msg
}
return ""
}
func (l *locale) LookupPlural(trKey string, count any) string {
n, err := util.ToInt64(count)
if err != nil {
log.Error("Invalid plural count '%s'", count)
return ""
}
pluralForm := l.pluralRule(n)
suffix := ""
switch pluralForm {
case PluralFormZero:
suffix = PluralFormSeparator + "zero"
case PluralFormOne:
suffix = PluralFormSeparator + "one"
case PluralFormTwo:
suffix = PluralFormSeparator + "two"
case PluralFormFew:
suffix = PluralFormSeparator + "few"
case PluralFormMany:
suffix = PluralFormSeparator + "many"
case PluralFormOther:
// No suffix for the "other" string.
default:
log.Error("Invalid plural form index %d for count %d", pluralForm, count)
return ""
}
if result, ok := l.newStyleMessages[trKey+suffix]; ok {
return result
}
log.Error("Missing translation for plural form index %d for count %d", pluralForm, count)
return ""
}
func (store *localeStore) HasLang(langName string) bool {
_, ok := store.localeMap[langName]
return ok
@ -113,22 +221,37 @@ func (store *localeStore) Close() error {
func (l *locale) TrString(trKey string, trArgs ...any) string {
format := trKey
idx, ok := l.store.trKeyToIdxMap[trKey]
found := false
if ok {
if msg, ok := l.idxToMsgMap[idx]; ok {
format = msg // use the found translation
found = true
} else if def, ok := l.store.localeMap[l.store.defaultLang]; ok {
// try to use default locale's translation
if msg, ok := def.idxToMsgMap[idx]; ok {
format = msg
if msg := l.LookupNewStyleMessage(trKey); msg != "" {
format = msg
} else {
// First fallback: old-style translation
idx, ok := l.store.trKeyToIdxMap[trKey]
found := false
if ok {
if msg, ok := l.idxToMsgMap[idx]; ok {
format = msg // use the found translation
found = true
}
}
}
if !found {
log.Error("Missing translation %q", trKey)
if !found {
// Second fallback: new-style default language
if defaultLang, ok := l.store.localeMap[l.store.defaultLang]; ok {
if msg := defaultLang.LookupNewStyleMessage(trKey); msg != "" {
format = msg
} else {
// Third fallback: old-style default language
if msg, ok := defaultLang.idxToMsgMap[idx]; ok {
format = msg
found = true
}
}
}
if !found {
log.Error("Missing translation %q", trKey)
}
}
}
msg, err := Format(format, trArgs...)
@ -138,7 +261,7 @@ func (l *locale) TrString(trKey string, trArgs ...any) string {
return msg
}
func (l *locale) TrHTML(trKey string, trArgs ...any) template.HTML {
func PrepareArgsForHTML(trArgs ...any) []any {
args := slices.Clone(trArgs)
for i, v := range args {
switch v := v.(type) {
@ -152,7 +275,30 @@ func (l *locale) TrHTML(trKey string, trArgs ...any) template.HTML {
args[i] = template.HTMLEscapeString(fmt.Sprint(v))
}
}
return template.HTML(l.TrString(trKey, args...))
return args
}
func (l *locale) TrHTML(trKey string, trArgs ...any) template.HTML {
return template.HTML(l.TrString(trKey, PrepareArgsForHTML(trArgs...)...))
}
func (l *locale) TrPluralString(count any, trKey string, trArgs ...any) template.HTML {
message := l.LookupPlural(trKey, count)
if message == "" {
if defaultLang, ok := l.store.localeMap[l.store.defaultLang]; ok {
message = defaultLang.LookupPlural(trKey, count)
}
if message == "" {
message = trKey
}
}
message, err := Format(message, PrepareArgsForHTML(trArgs...)...)
if err != nil {
log.Error("Error whilst formatting %q in %s: %v", trKey, l.langName, err)
}
return template.HTML(message)
}
// HasKey returns whether a key is present in this locale or not