初始提交: Gitea 项目代码
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Format formats provided arguments for a given translated message
|
||||
func Format(format string, args ...any) (msg string, err error) {
|
||||
if len(args) == 0 {
|
||||
return format, nil
|
||||
}
|
||||
|
||||
fmtArgs := make([]any, 0, len(args))
|
||||
for _, arg := range args {
|
||||
val := reflect.ValueOf(arg)
|
||||
if val.Kind() == reflect.Slice {
|
||||
// Previously, we would accept Tr(lang, key, a, [b, c], d, [e, f]) as Sprintf(msg, a, b, c, d, e, f)
|
||||
// but this is an unstable behavior.
|
||||
//
|
||||
// So we restrict the accepted arguments to either:
|
||||
//
|
||||
// 1. Tr(lang, key, [slice-items]) as Sprintf(msg, items...)
|
||||
// 2. Tr(lang, key, args...) as Sprintf(msg, args...)
|
||||
if len(args) == 1 {
|
||||
for i := 0; i < val.Len(); i++ {
|
||||
fmtArgs = append(fmtArgs, val.Index(i).Interface())
|
||||
}
|
||||
} else {
|
||||
err = errors.New("arguments to i18n should not contain uncertain slices")
|
||||
break
|
||||
}
|
||||
} else {
|
||||
fmtArgs = append(fmtArgs, arg)
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf(format, fmtArgs...), err
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io"
|
||||
)
|
||||
|
||||
var DefaultLocales = NewLocaleStore()
|
||||
|
||||
type Locale interface {
|
||||
// TrString translates a given key and arguments for a language
|
||||
TrString(trKey string, trArgs ...any) string
|
||||
// TrHTML translates a given key and arguments for a language, string arguments are escaped to HTML
|
||||
TrHTML(trKey string, trArgs ...any) template.HTML
|
||||
// HasKey reports if a locale has a translation for a given key
|
||||
HasKey(trKey string) bool
|
||||
}
|
||||
|
||||
// LocaleStore provides the functions common to all locale stores
|
||||
type LocaleStore interface {
|
||||
io.Closer
|
||||
|
||||
// SetDefaultLang sets the default language to fall back to
|
||||
SetDefaultLang(lang string)
|
||||
// ListLangNameDesc provides paired slices of language names to descriptors
|
||||
ListLangNameDesc() (names, desc []string)
|
||||
// Locale return the locale for the provided language or the default language if not found
|
||||
Locale(langName string) (Locale, bool)
|
||||
// HasLang returns whether a given language is present in the store
|
||||
HasLang(langName string) bool
|
||||
// AddLocaleByIni adds a new language to the store
|
||||
AddLocaleByJSON(langName, langDesc string, source, moreSource []byte) error
|
||||
}
|
||||
|
||||
// ResetDefaultLocales resets the current default locales
|
||||
// NOTE: this is not synchronized
|
||||
func ResetDefaultLocales() {
|
||||
if DefaultLocales != nil {
|
||||
_ = DefaultLocales.Close()
|
||||
}
|
||||
DefaultLocales = NewLocaleStore()
|
||||
}
|
||||
|
||||
// GetLocale returns the locale from the default locales
|
||||
func GetLocale(lang string) (Locale, bool) {
|
||||
return DefaultLocales.Locale(lang)
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLocaleStore(t *testing.T) {
|
||||
testData1 := []byte(`
|
||||
{
|
||||
".dot.name": "Dot Name",
|
||||
"fmt": "%[1]s %[2]s",
|
||||
|
||||
"section.sub": "Sub String",
|
||||
"section.mixed": "test value; <span style=\"color: red; background: none;\">%s</span>"
|
||||
}`)
|
||||
|
||||
testData2 := []byte(`
|
||||
{
|
||||
"fmt": "%[2]s %[1]s",
|
||||
"section.sub": "Changed Sub String"
|
||||
}`)
|
||||
|
||||
ls := NewLocaleStore()
|
||||
assert.NoError(t, ls.AddLocaleByJSON("lang1", "Lang1", testData1, nil))
|
||||
assert.NoError(t, ls.AddLocaleByJSON("lang2", "Lang2", testData2, nil))
|
||||
ls.SetDefaultLang("lang1")
|
||||
|
||||
lang1, _ := ls.Locale("lang1")
|
||||
lang2, _ := ls.Locale("lang2")
|
||||
|
||||
result := lang1.TrString("fmt", "a", "b")
|
||||
assert.Equal(t, "a b", result)
|
||||
|
||||
result = lang2.TrString("fmt", "a", "b")
|
||||
assert.Equal(t, "b a", result)
|
||||
|
||||
result = lang1.TrString("section.sub")
|
||||
assert.Equal(t, "Sub String", result)
|
||||
|
||||
result = lang2.TrString("section.sub")
|
||||
assert.Equal(t, "Changed Sub String", result)
|
||||
|
||||
langNone, _ := ls.Locale("none")
|
||||
result = langNone.TrString(".dot.name")
|
||||
assert.Equal(t, "Dot Name", result)
|
||||
|
||||
result2 := lang2.TrHTML("section.mixed", "a&b")
|
||||
assert.EqualValues(t, `test value; <span style="color: red; background: none;">a&b</span>`, result2)
|
||||
|
||||
langs, descs := ls.ListLangNameDesc()
|
||||
assert.ElementsMatch(t, []string{"lang1", "lang2"}, langs)
|
||||
assert.ElementsMatch(t, []string{"Lang1", "Lang2"}, descs)
|
||||
|
||||
found := lang1.HasKey("no-such")
|
||||
assert.False(t, found)
|
||||
assert.NoError(t, ls.Close())
|
||||
|
||||
res := lang1.TrHTML("<no-such>")
|
||||
assert.Equal(t, "<no-such>", string(res))
|
||||
}
|
||||
|
||||
func TestLocaleStoreMoreSource(t *testing.T) {
|
||||
testData1 := []byte(`
|
||||
{
|
||||
"a": "11",
|
||||
"b": "12"
|
||||
}
|
||||
`)
|
||||
|
||||
testData2 := []byte(`
|
||||
{
|
||||
"b": "21",
|
||||
"c": "22"
|
||||
}
|
||||
`)
|
||||
|
||||
ls := NewLocaleStore()
|
||||
assert.NoError(t, ls.AddLocaleByJSON("lang1", "Lang1", testData1, testData2))
|
||||
lang1, _ := ls.Locale("lang1")
|
||||
assert.Equal(t, "11", lang1.TrString("a"))
|
||||
assert.Equal(t, "21", lang1.TrString("b"))
|
||||
assert.Equal(t, "22", lang1.TrString("c"))
|
||||
}
|
||||
|
||||
type stringerPointerReceiver struct {
|
||||
s string
|
||||
}
|
||||
|
||||
func (s *stringerPointerReceiver) String() string {
|
||||
return s.s
|
||||
}
|
||||
|
||||
type stringerStructReceiver struct {
|
||||
s string
|
||||
}
|
||||
|
||||
func (s stringerStructReceiver) String() string {
|
||||
return s.s
|
||||
}
|
||||
|
||||
type errorStructReceiver struct {
|
||||
s string
|
||||
}
|
||||
|
||||
func (e errorStructReceiver) Error() string {
|
||||
return e.s
|
||||
}
|
||||
|
||||
type errorPointerReceiver struct {
|
||||
s string
|
||||
}
|
||||
|
||||
func (e *errorPointerReceiver) Error() string {
|
||||
return e.s
|
||||
}
|
||||
|
||||
func TestLocaleWithTemplate(t *testing.T) {
|
||||
ls := NewLocaleStore()
|
||||
assert.NoError(t, ls.AddLocaleByJSON("lang1", "Lang1", []byte(`{"key":"<a>%s</a>"}`), nil))
|
||||
lang1, _ := ls.Locale("lang1")
|
||||
|
||||
tmpl := template.New("test").Funcs(template.FuncMap{"tr": lang1.TrHTML})
|
||||
tmpl = template.Must(tmpl.Parse(`{{tr "key" .var}}`))
|
||||
|
||||
cases := []struct {
|
||||
in any
|
||||
want string
|
||||
}{
|
||||
{"<str>", "<a><str></a>"},
|
||||
{[]byte("<bytes>"), "<a>[60 98 121 116 101 115 62]</a>"},
|
||||
{template.HTML("<html>"), "<a><html></a>"},
|
||||
{stringerPointerReceiver{"<stringerPointerReceiver>"}, "<a>{<stringerPointerReceiver>}</a>"},
|
||||
{&stringerPointerReceiver{"<stringerPointerReceiver ptr>"}, "<a><stringerPointerReceiver ptr></a>"},
|
||||
{stringerStructReceiver{"<stringerStructReceiver>"}, "<a><stringerStructReceiver></a>"},
|
||||
{&stringerStructReceiver{"<stringerStructReceiver ptr>"}, "<a><stringerStructReceiver ptr></a>"},
|
||||
{errorStructReceiver{"<errorStructReceiver>"}, "<a><errorStructReceiver></a>"},
|
||||
{&errorStructReceiver{"<errorStructReceiver ptr>"}, "<a><errorStructReceiver ptr></a>"},
|
||||
{errorPointerReceiver{"<errorPointerReceiver>"}, "<a>{<errorPointerReceiver>}</a>"},
|
||||
{&errorPointerReceiver{"<errorPointerReceiver ptr>"}, "<a><errorPointerReceiver ptr></a>"},
|
||||
}
|
||||
|
||||
buf := &strings.Builder{}
|
||||
for _, c := range cases {
|
||||
buf.Reset()
|
||||
assert.NoError(t, tmpl.Execute(buf, map[string]any{"var": c.in}))
|
||||
assert.Equal(t, c.want, buf.String())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
"html/template"
|
||||
"slices"
|
||||
|
||||
"gitea.dev/modules/json"
|
||||
"gitea.dev/modules/log"
|
||||
)
|
||||
|
||||
// This file implements the static LocaleStore that will not watch for changes
|
||||
|
||||
type locale struct {
|
||||
store *localeStore
|
||||
langName string
|
||||
idxToMsgMap map[int]string // the map idx is generated by store's trKeyToIdxMap
|
||||
}
|
||||
|
||||
var _ Locale = (*locale)(nil)
|
||||
|
||||
type localeStore struct {
|
||||
// After initializing has finished, these fields are read-only.
|
||||
langNames []string
|
||||
langDescs []string
|
||||
|
||||
localeMap map[string]*locale
|
||||
trKeyToIdxMap map[string]int
|
||||
|
||||
defaultLang string
|
||||
}
|
||||
|
||||
// NewLocaleStore creates a static locale store
|
||||
func NewLocaleStore() LocaleStore {
|
||||
return &localeStore{localeMap: make(map[string]*locale), trKeyToIdxMap: make(map[string]int)}
|
||||
}
|
||||
|
||||
// AddLocaleByJSON adds locale by JSON into the store
|
||||
func (store *localeStore) AddLocaleByJSON(langName, langDesc string, source, moreSource []byte) error {
|
||||
if _, ok := store.localeMap[langName]; ok {
|
||||
return errors.New("lang has already been added")
|
||||
}
|
||||
|
||||
store.langNames = append(store.langNames, langName)
|
||||
store.langDescs = append(store.langDescs, langDesc)
|
||||
|
||||
l := &locale{store: store, langName: langName, idxToMsgMap: make(map[int]string)}
|
||||
store.localeMap[l.langName] = l
|
||||
|
||||
addFunc := func(source []byte) error {
|
||||
if len(source) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
values := make(map[string]any)
|
||||
if err := json.Unmarshal(source, &values); err != nil {
|
||||
return fmt.Errorf("unable to load json: %w", err)
|
||||
}
|
||||
for trKey, value := range values {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
idx, ok := store.trKeyToIdxMap[trKey]
|
||||
if !ok {
|
||||
idx = len(store.trKeyToIdxMap)
|
||||
store.trKeyToIdxMap[trKey] = idx
|
||||
}
|
||||
l.idxToMsgMap[idx] = v
|
||||
case map[string]any:
|
||||
for key, val := range v {
|
||||
idx, ok := store.trKeyToIdxMap[trKey+"."+key]
|
||||
if !ok {
|
||||
idx = len(store.trKeyToIdxMap)
|
||||
store.trKeyToIdxMap[trKey+"."+key] = idx
|
||||
}
|
||||
l.idxToMsgMap[idx] = val.(string)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("unsupported value type %T for key %q", v, trKey)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := addFunc(source); err != nil {
|
||||
return fmt.Errorf("unable to load json: %w", err)
|
||||
}
|
||||
if err := addFunc(moreSource); err != nil {
|
||||
return fmt.Errorf("unable to load json: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *localeStore) HasLang(langName string) bool {
|
||||
_, ok := store.localeMap[langName]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (store *localeStore) ListLangNameDesc() (names, desc []string) {
|
||||
return store.langNames, store.langDescs
|
||||
}
|
||||
|
||||
// SetDefaultLang sets default language as a fallback
|
||||
func (store *localeStore) SetDefaultLang(lang string) {
|
||||
store.defaultLang = lang
|
||||
}
|
||||
|
||||
// Locale returns the locale for the lang or the default language
|
||||
func (store *localeStore) Locale(lang string) (Locale, bool) {
|
||||
l, found := store.localeMap[lang]
|
||||
if !found {
|
||||
var ok bool
|
||||
l, ok = store.localeMap[store.defaultLang]
|
||||
if !ok {
|
||||
// no default - return an empty locale
|
||||
l = &locale{store: store, idxToMsgMap: make(map[int]string)}
|
||||
}
|
||||
}
|
||||
return l, found
|
||||
}
|
||||
|
||||
func (store *localeStore) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *locale) TrString(trKey string, trArgs ...any) string {
|
||||
var format string
|
||||
idx, ok := l.store.trKeyToIdxMap[trKey]
|
||||
if ok {
|
||||
format = l.idxToMsgMap[idx]
|
||||
if format == "" { // missing translation in this locale, fallback to default
|
||||
if def, ok := l.store.localeMap[l.store.defaultLang]; ok {
|
||||
// try to use default locale's translation
|
||||
format = def.idxToMsgMap[idx]
|
||||
}
|
||||
}
|
||||
}
|
||||
if format == "" { // still missing, use the key itself
|
||||
format = html.EscapeString(trKey)
|
||||
}
|
||||
msg, err := Format(format, trArgs...)
|
||||
if err != nil {
|
||||
log.Error("Error whilst formatting %q in %s: %v", trKey, l.langName, err)
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
func (l *locale) TrHTML(trKey string, trArgs ...any) template.HTML {
|
||||
args := slices.Clone(trArgs)
|
||||
for i, v := range args {
|
||||
switch v := v.(type) {
|
||||
case nil, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, template.HTML:
|
||||
// for most basic types (including template.HTML which is safe), just do nothing and use it
|
||||
case string:
|
||||
args[i] = template.HTMLEscapeString(v)
|
||||
case fmt.Stringer:
|
||||
args[i] = template.HTMLEscapeString(v.String())
|
||||
default:
|
||||
args[i] = template.HTMLEscapeString(fmt.Sprint(v))
|
||||
}
|
||||
}
|
||||
return template.HTML(l.TrString(trKey, args...))
|
||||
}
|
||||
|
||||
// HasKey returns whether a key is present in this locale or not
|
||||
func (l *locale) HasKey(trKey string) bool {
|
||||
idx, ok := l.store.trKeyToIdxMap[trKey]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
_, ok = l.idxToMsgMap[idx]
|
||||
return ok
|
||||
}
|
||||
Reference in New Issue
Block a user