99 lines
2.0 KiB
Go
99 lines
2.0 KiB
Go
package ersteller_lib
|
|
|
|
type Language string
|
|
|
|
const (
|
|
En Language = "en"
|
|
De Language = "de"
|
|
)
|
|
|
|
func (t Language) GetPath() string {
|
|
return "/" + string(t)
|
|
}
|
|
|
|
type GlobalI18nTexts interface {
|
|
Add(text I18nText) GlobalI18nTexts
|
|
GetAllTexts() []I18nText
|
|
}
|
|
|
|
var GlobalI18n GlobalI18nTexts = GlobalI18nImplementation{i18nTexts: make(map[string]I18nText)}
|
|
|
|
type GlobalI18nImplementation struct {
|
|
i18nTexts map[string]I18nText
|
|
}
|
|
|
|
func (g GlobalI18nImplementation) GetAllTexts() []I18nText {
|
|
var allTexts []I18nText
|
|
for _, value := range g.i18nTexts {
|
|
allTexts = append(allTexts, value)
|
|
}
|
|
return allTexts
|
|
}
|
|
|
|
func (g GlobalI18nImplementation) Add(text I18nText) GlobalI18nTexts {
|
|
if g.i18nTexts == nil {
|
|
g.i18nTexts = make(map[string]I18nText)
|
|
}
|
|
g.i18nTexts[text.GetKey()] = text
|
|
return g
|
|
}
|
|
|
|
type I18nText struct {
|
|
langMap map[Language]string
|
|
}
|
|
|
|
func NewI18nText(langMap map[Language]string) I18nText {
|
|
text := I18nText{langMap: langMap}
|
|
GlobalI18n = GlobalI18n.Add(text)
|
|
return text
|
|
}
|
|
|
|
func (t I18nText) FromLang(lang Language) string {
|
|
return t.langMap[lang]
|
|
}
|
|
|
|
func (t I18nText) From(c HtmxContext) string {
|
|
return t.FromLang(c.GetLanguage())
|
|
}
|
|
|
|
func (t I18nText) GetKey() string {
|
|
if t.langMap[En] != "" {
|
|
return t.langMap[En]
|
|
}
|
|
// Get values of map as an array/slice
|
|
var values []string
|
|
for _, value := range t.langMap {
|
|
if value != "" {
|
|
values = append(values, value)
|
|
}
|
|
}
|
|
// Return first available value
|
|
if len(values) > 0 {
|
|
return values[0]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
type LanguagePaths map[Language]string
|
|
|
|
type ActivePath struct {
|
|
I18nText
|
|
Paths LanguagePaths
|
|
}
|
|
|
|
func NewActivePath(langMap map[Language]string, paths LanguagePaths) ActivePath {
|
|
return ActivePath{
|
|
I18nText: NewI18nText(langMap),
|
|
Paths: paths,
|
|
}
|
|
}
|
|
|
|
func (a ActivePath) GetPath(language Language) string {
|
|
return language.GetPath() + a.Paths[language]
|
|
}
|
|
|
|
func (a ActivePath) IsActive(c HtmxContext) bool {
|
|
contextActivePath := c.GetActivePath()
|
|
return contextActivePath != nil && contextActivePath.From(c) == a.From(c)
|
|
}
|