2022-04-03 17:46:48 +08:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package i18n
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-06-26 22:19:22 +08:00
|
|
|
"os"
|
2022-04-03 17:46:48 +08:00
|
|
|
"reflect"
|
2022-06-26 22:19:22 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
2022-04-03 17:46:48 +08:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-06-26 22:19:22 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-04-03 17:46:48 +08:00
|
|
|
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrLocaleAlreadyExist = errors.New("lang already exists")
|
|
|
|
|
2022-06-26 22:19:22 +08:00
|
|
|
DefaultLocales = NewLocaleStore(true)
|
2022-04-03 17:46:48 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type locale struct {
|
2022-07-04 18:17:09 +08:00
|
|
|
// This mutex will be set if we have live-reload enabled (e.g. dev mode)
|
|
|
|
reloadMu *sync.RWMutex
|
|
|
|
|
2022-04-03 17:46:48 +08:00
|
|
|
store *LocaleStore
|
|
|
|
langName string
|
2022-07-04 18:17:09 +08:00
|
|
|
|
|
|
|
idxToMsgMap map[int]string // the map idx is generated by store's trKeyToIdxMap
|
2022-06-26 22:19:22 +08:00
|
|
|
|
|
|
|
sourceFileName string
|
|
|
|
sourceFileInfo os.FileInfo
|
|
|
|
lastReloadCheckTime time.Time
|
2022-04-03 17:46:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type LocaleStore struct {
|
2022-07-04 18:17:09 +08:00
|
|
|
// This mutex will be set if we have live-reload enabled (e.g. dev mode)
|
|
|
|
reloadMu *sync.RWMutex
|
2022-06-26 22:19:22 +08:00
|
|
|
|
|
|
|
langNames []string
|
|
|
|
langDescs []string
|
2022-07-04 18:17:09 +08:00
|
|
|
localeMap map[string]*locale
|
2022-06-26 22:19:22 +08:00
|
|
|
|
2022-07-04 18:17:09 +08:00
|
|
|
// this needs to be locked when live-reloading
|
|
|
|
trKeyToIdxMap map[string]int
|
2022-06-26 22:19:22 +08:00
|
|
|
|
2022-04-03 17:46:48 +08:00
|
|
|
defaultLang string
|
|
|
|
}
|
|
|
|
|
2022-06-26 22:19:22 +08:00
|
|
|
func NewLocaleStore(isProd bool) *LocaleStore {
|
2022-07-04 18:17:09 +08:00
|
|
|
store := &LocaleStore{localeMap: make(map[string]*locale), trKeyToIdxMap: make(map[string]int)}
|
2022-06-26 22:19:22 +08:00
|
|
|
if !isProd {
|
2022-07-04 18:17:09 +08:00
|
|
|
store.reloadMu = &sync.RWMutex{}
|
2022-06-26 22:19:22 +08:00
|
|
|
}
|
2022-07-04 18:17:09 +08:00
|
|
|
return store
|
2022-04-03 17:46:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddLocaleByIni adds locale by ini into the store
|
2022-07-04 18:17:09 +08:00
|
|
|
// if source is a string, then the file is loaded. In dev mode, this file will be checked for live-reloading
|
2022-06-26 22:19:22 +08:00
|
|
|
// if source is a []byte, then the content is used
|
2022-07-04 18:17:09 +08:00
|
|
|
// Note: this is not concurrent safe
|
|
|
|
func (store *LocaleStore) AddLocaleByIni(langName, langDesc string, source interface{}) error {
|
|
|
|
if _, ok := store.localeMap[langName]; ok {
|
2022-04-03 17:46:48 +08:00
|
|
|
return ErrLocaleAlreadyExist
|
|
|
|
}
|
2022-06-26 22:19:22 +08:00
|
|
|
|
2022-07-04 18:17:09 +08:00
|
|
|
l := &locale{store: store, langName: langName}
|
|
|
|
if store.reloadMu != nil {
|
|
|
|
l.reloadMu = &sync.RWMutex{}
|
|
|
|
l.reloadMu.Lock() // Arguably this is not necessary as AddLocaleByIni isn't concurrent safe - but for consistency we do this
|
|
|
|
defer l.reloadMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-06-26 22:19:22 +08:00
|
|
|
if fileName, ok := source.(string); ok {
|
2022-07-04 18:17:09 +08:00
|
|
|
l.sourceFileName = fileName
|
|
|
|
l.sourceFileInfo, _ = os.Stat(fileName) // live-reload only works for regular files. the error can be ignored
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
l.idxToMsgMap, err = store.readIniToIdxToMsgMap(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-06-26 22:19:22 +08:00
|
|
|
}
|
|
|
|
|
2022-07-04 18:17:09 +08:00
|
|
|
store.langNames = append(store.langNames, langName)
|
|
|
|
store.langDescs = append(store.langDescs, langDesc)
|
|
|
|
|
|
|
|
store.localeMap[l.langName] = l
|
2022-06-26 22:19:22 +08:00
|
|
|
|
2022-07-04 18:17:09 +08:00
|
|
|
return nil
|
2022-06-26 22:19:22 +08:00
|
|
|
}
|
|
|
|
|
2022-07-04 18:17:09 +08:00
|
|
|
// readIniToIdxToMsgMap will read a provided ini and creates an idxToMsgMap
|
|
|
|
func (store *LocaleStore) readIniToIdxToMsgMap(source interface{}) (map[int]string, error) {
|
2022-04-03 17:46:48 +08:00
|
|
|
iniFile, err := ini.LoadSources(ini.LoadOptions{
|
|
|
|
IgnoreInlineComment: true,
|
|
|
|
UnescapeValueCommentSymbols: true,
|
2022-06-26 22:19:22 +08:00
|
|
|
}, source)
|
|
|
|
if err != nil {
|
2022-07-04 18:17:09 +08:00
|
|
|
return nil, fmt.Errorf("unable to load ini: %w", err)
|
2022-04-03 17:46:48 +08:00
|
|
|
}
|
2022-06-26 22:19:22 +08:00
|
|
|
iniFile.BlockMode = false
|
|
|
|
|
2022-07-04 18:17:09 +08:00
|
|
|
idxToMsgMap := make(map[int]string)
|
|
|
|
|
|
|
|
if store.reloadMu != nil {
|
|
|
|
store.reloadMu.Lock()
|
|
|
|
defer store.reloadMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-06-26 22:19:22 +08:00
|
|
|
for _, section := range iniFile.Sections() {
|
|
|
|
for _, key := range section.Keys() {
|
2022-07-04 18:17:09 +08:00
|
|
|
|
2022-06-26 22:19:22 +08:00
|
|
|
var trKey string
|
|
|
|
if section.Name() == "" || section.Name() == "DEFAULT" {
|
|
|
|
trKey = key.Name()
|
|
|
|
} else {
|
|
|
|
trKey = section.Name() + "." + key.Name()
|
|
|
|
}
|
2022-07-04 18:17:09 +08:00
|
|
|
|
|
|
|
// Instead of storing the key strings in multiple different maps we compute a idx which will act as numeric code for key
|
|
|
|
// This reduces the size of the locale idxToMsgMaps
|
|
|
|
idx, ok := store.trKeyToIdxMap[trKey]
|
2022-06-26 22:19:22 +08:00
|
|
|
if !ok {
|
2022-07-04 18:17:09 +08:00
|
|
|
idx = len(store.trKeyToIdxMap)
|
|
|
|
store.trKeyToIdxMap[trKey] = idx
|
2022-06-26 22:19:22 +08:00
|
|
|
}
|
2022-07-04 18:17:09 +08:00
|
|
|
idxToMsgMap[idx] = key.Value()
|
2022-06-26 22:19:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
iniFile = nil
|
2022-07-04 18:17:09 +08:00
|
|
|
return idxToMsgMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *LocaleStore) idxForTrKey(trKey string) (int, bool) {
|
|
|
|
if store.reloadMu != nil {
|
|
|
|
store.reloadMu.RLock()
|
|
|
|
defer store.reloadMu.RUnlock()
|
|
|
|
}
|
|
|
|
idx, ok := store.trKeyToIdxMap[trKey]
|
|
|
|
return idx, ok
|
2022-04-03 17:46:48 +08:00
|
|
|
}
|
|
|
|
|
2022-07-04 18:17:09 +08:00
|
|
|
// HasLang reports if a language is available in the store
|
|
|
|
func (store *LocaleStore) HasLang(langName string) bool {
|
|
|
|
_, ok := store.localeMap[langName]
|
2022-04-03 17:46:48 +08:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2022-07-04 18:17:09 +08:00
|
|
|
// ListLangNameDesc reports if a language available in the store
|
|
|
|
func (store *LocaleStore) ListLangNameDesc() (names, desc []string) {
|
|
|
|
return store.langNames, store.langDescs
|
2022-04-03 17:46:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetDefaultLang sets default language as a fallback
|
2022-07-04 18:17:09 +08:00
|
|
|
func (store *LocaleStore) SetDefaultLang(lang string) {
|
|
|
|
store.defaultLang = lang
|
2022-04-03 17:46:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tr translates content to target language. fall back to default language.
|
2022-07-04 18:17:09 +08:00
|
|
|
func (store *LocaleStore) Tr(lang, trKey string, trArgs ...interface{}) string {
|
|
|
|
l, ok := store.localeMap[lang]
|
2022-04-03 17:46:48 +08:00
|
|
|
if !ok {
|
2022-07-04 18:17:09 +08:00
|
|
|
l, ok = store.localeMap[store.defaultLang]
|
2022-04-03 17:46:48 +08:00
|
|
|
}
|
2022-07-04 18:17:09 +08:00
|
|
|
|
2022-04-03 17:46:48 +08:00
|
|
|
if ok {
|
|
|
|
return l.Tr(trKey, trArgs...)
|
|
|
|
}
|
|
|
|
return trKey
|
|
|
|
}
|
|
|
|
|
2022-07-04 18:17:09 +08:00
|
|
|
// reloadIfNeeded will check if the locale needs to be reloaded
|
|
|
|
// this function will assume that the l.reloadMu has been RLocked if it already exists
|
|
|
|
func (l *locale) reloadIfNeeded() {
|
|
|
|
if l.reloadMu == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
if now.Sub(l.lastReloadCheckTime) < time.Second || l.sourceFileInfo == nil || l.sourceFileName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.reloadMu.RUnlock()
|
|
|
|
l.reloadMu.Lock() // (NOTE: a pre-emption can occur between these two locks so we need to recheck)
|
|
|
|
defer l.reloadMu.RLock()
|
|
|
|
defer l.reloadMu.Unlock()
|
|
|
|
|
|
|
|
if now.Sub(l.lastReloadCheckTime) < time.Second || l.sourceFileInfo == nil || l.sourceFileName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.lastReloadCheckTime = now
|
|
|
|
sourceFileInfo, err := os.Stat(l.sourceFileName)
|
|
|
|
if err != nil || sourceFileInfo.ModTime().Equal(l.sourceFileInfo.ModTime()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
idxToMsgMap, err := l.store.readIniToIdxToMsgMap(l.sourceFileName)
|
|
|
|
if err == nil {
|
|
|
|
l.idxToMsgMap = idxToMsgMap
|
|
|
|
} else {
|
|
|
|
log.Error("Unable to live-reload the locale file %q, err: %v", l.sourceFileName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We will set the sourceFileInfo to this file to prevent repeated attempts to re-load this broken file
|
|
|
|
l.sourceFileInfo = sourceFileInfo
|
|
|
|
}
|
|
|
|
|
2022-04-03 17:46:48 +08:00
|
|
|
// Tr translates content to locale language. fall back to default language.
|
|
|
|
func (l *locale) Tr(trKey string, trArgs ...interface{}) string {
|
2022-07-04 18:17:09 +08:00
|
|
|
if l.reloadMu != nil {
|
|
|
|
l.reloadMu.RLock()
|
|
|
|
defer l.reloadMu.RUnlock()
|
|
|
|
l.reloadIfNeeded()
|
2022-04-03 17:46:48 +08:00
|
|
|
}
|
2022-07-04 18:17:09 +08:00
|
|
|
|
2022-06-26 22:19:22 +08:00
|
|
|
msg, _ := l.tryTr(trKey, trArgs...)
|
|
|
|
return msg
|
|
|
|
}
|
2022-04-03 17:46:48 +08:00
|
|
|
|
2022-06-26 22:19:22 +08:00
|
|
|
func (l *locale) tryTr(trKey string, trArgs ...interface{}) (msg string, found bool) {
|
2022-04-03 17:46:48 +08:00
|
|
|
trMsg := trKey
|
2022-07-04 18:17:09 +08:00
|
|
|
|
|
|
|
// convert the provided trKey to a common idx from the store
|
|
|
|
idx, ok := l.store.idxForTrKey(trKey)
|
|
|
|
|
2022-06-26 22:19:22 +08:00
|
|
|
if ok {
|
2022-07-04 18:17:09 +08:00
|
|
|
if msg, found = l.idxToMsgMap[idx]; found {
|
|
|
|
trMsg = msg // use the translation that we have found
|
2022-06-26 22:19:22 +08:00
|
|
|
} else if l.langName != l.store.defaultLang {
|
2022-07-04 18:17:09 +08:00
|
|
|
// No translation available in our current language... fallback to the default language
|
|
|
|
|
|
|
|
// Attempt to get the default language from the locale store
|
2022-06-26 22:19:22 +08:00
|
|
|
if def, ok := l.store.localeMap[l.store.defaultLang]; ok {
|
2022-07-04 18:17:09 +08:00
|
|
|
|
|
|
|
if def.reloadMu != nil {
|
|
|
|
def.reloadMu.RLock()
|
|
|
|
def.reloadIfNeeded()
|
|
|
|
}
|
|
|
|
if msg, found = def.idxToMsgMap[idx]; found {
|
|
|
|
trMsg = msg // use the translation that we have found
|
|
|
|
}
|
|
|
|
if def.reloadMu != nil {
|
|
|
|
def.reloadMu.RUnlock()
|
|
|
|
}
|
2022-04-03 17:46:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 18:17:09 +08:00
|
|
|
if !found && !setting.IsProd {
|
|
|
|
log.Error("missing i18n translation key: %q", trKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(trArgs) == 0 {
|
|
|
|
return trMsg, found
|
|
|
|
}
|
|
|
|
|
|
|
|
fmtArgs := make([]interface{}, 0, len(trArgs))
|
|
|
|
for _, arg := range trArgs {
|
|
|
|
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(trArgs) == 1 {
|
|
|
|
for i := 0; i < val.Len(); i++ {
|
|
|
|
fmtArgs = append(fmtArgs, val.Index(i).Interface())
|
2022-04-03 17:46:48 +08:00
|
|
|
}
|
|
|
|
} else {
|
2022-07-04 18:17:09 +08:00
|
|
|
log.Error("the args for i18n shouldn't contain uncertain slices, key=%q, args=%v", trKey, trArgs)
|
|
|
|
break
|
2022-04-03 17:46:48 +08:00
|
|
|
}
|
2022-07-04 18:17:09 +08:00
|
|
|
} else {
|
|
|
|
fmtArgs = append(fmtArgs, arg)
|
2022-04-03 17:46:48 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-04 18:17:09 +08:00
|
|
|
|
|
|
|
return fmt.Sprintf(trMsg, fmtArgs...), found
|
2022-04-03 17:46:48 +08:00
|
|
|
}
|
|
|
|
|
2022-06-26 22:19:22 +08:00
|
|
|
// ResetDefaultLocales resets the current default locales
|
|
|
|
// NOTE: this is not synchronized
|
|
|
|
func ResetDefaultLocales(isProd bool) {
|
|
|
|
DefaultLocales = NewLocaleStore(isProd)
|
2022-04-03 17:46:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tr use default locales to translate content to target language.
|
|
|
|
func Tr(lang, trKey string, trArgs ...interface{}) string {
|
|
|
|
return DefaultLocales.Tr(lang, trKey, trArgs...)
|
|
|
|
}
|