Files
upstream-corteza/server/pkg/locale/locale.go
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

127 lines
2.8 KiB
Go

package locale
import (
"fmt"
"io"
"io/fs"
"strings"
"sync"
"golang.org/x/text/language"
)
type (
// Internal translations (application == corteza-server)
// map contains namespace => key => text
internal map[string]map[string]string
// External translations (webapps)
// map structure:
// application => namespace => buffered JSON docs
external map[string]io.ReadSeeker
// resource => key => ResourceTranslation
resources map[string]map[string]*ResourceTranslation
Language struct {
l sync.RWMutex
// location of the language files
// this is mainly for logging/debugging purposes
//
// This value is empty when loading
// embedded languages
src string
// pointer to the place we loaded files from
fs fs.FS
Tag language.Tag `json:"tag,string"`
Name string `json:"name"`
LocalizedName string `json:"localizedName"`
// Language (tag) that this language is extending
// @todo
Extends language.Tag `json:"-"`
// Points to extended language
// @todo
extends *Language
// Internal translations (application == corteza-server)
internal internal
// External translations (webapps)
external external
resources resources
}
ErrorMetaNamespace struct{}
ErrorMetaKey struct{}
)
// t returns the translated string for internal content
func (l *Language) t(ns, key string, rr ...string) string {
l.l.RLock()
defer l.l.RUnlock()
msg, has := l.internal[ns][key]
if has {
for r := 0; r < len(rr); r += 2 {
rr[r] = fmt.Sprintf("{{%s}}", rr[r])
}
return strings.NewReplacer(rr...).Replace(msg)
}
// If translation is not found, try with language
// this language extends
if l.extends != nil {
return l.extends.t(ns, key, rr...)
}
// In case of untranslated string / missing translations,
// translation key is returned
return key
}
// tResource returns the translated string for resource translations
func (l *Language) tResource(ns, key string, rr ...string) string {
l.l.RLock()
defer l.l.RUnlock()
rt, has := l.resources[ns][key]
if has {
for r := 0; r < len(rr); r += 2 {
rr[r] = fmt.Sprintf("{{%s}}", rr[r])
}
return strings.NewReplacer(rr...).Replace(rt.Msg)
}
// When translating resources we do not utilize
// language extensions like with static translations
// In case of untranslated string / missing translations,
// only empty string is returned (in contrast to static translations
// where a string with key is returned
return ""
}
// resourceTranslations returns all resource translations for the specified resource
func (l *Language) resourceTranslations(resource string) ResourceTranslationIndex {
l.l.RLock()
defer l.l.RUnlock()
out := make(ResourceTranslationIndex)
if l.resources == nil {
return out
}
for k, rt := range l.resources[resource] {
out[k] = rt
}
return out
}