Use i18n text struct for pagination
This commit is contained in:
+3
-3
@@ -25,14 +25,14 @@ func NewHtmxContext(req *http.Request, res http.ResponseWriter) HtmxContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *HtmxContextImpl) Render(node gomponents.Node) {
|
func (c *HtmxContextImpl) Render(node gomponents.Node) {
|
||||||
err := node.Render(w.res)
|
err := node.Render(c.res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error("failed to render", err)
|
Error("failed to render", err)
|
||||||
_, writeErr := w.res.Write([]byte(err.Error()))
|
_, writeErr := c.res.Write([]byte(err.Error()))
|
||||||
if writeErr != nil {
|
if writeErr != nil {
|
||||||
Error("failed to write error", writeErr)
|
Error("failed to write error", writeErr)
|
||||||
}
|
}
|
||||||
w.res.WriteHeader(500)
|
c.res.WriteHeader(500)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package ersteller_lib
|
||||||
|
|
||||||
|
type Language string
|
||||||
|
|
||||||
|
const (
|
||||||
|
En Language = "en"
|
||||||
|
De Language = "de"
|
||||||
|
)
|
||||||
|
|
||||||
|
type GlobalI18nTexts interface {
|
||||||
|
Add(text I18nText) GlobalI18nTexts
|
||||||
|
GetAllTexts() []I18nText
|
||||||
|
}
|
||||||
|
|
||||||
|
var GlobalI18n GlobalI18nTexts
|
||||||
|
|
||||||
|
type GlobalI18nImplementation struct {
|
||||||
|
i18nTexts map[string]I18nText
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g GlobalI18nImplementation) GetAllTexts() []I18nText {
|
||||||
|
allTexts := make([]I18nText, len(g.i18nTexts))
|
||||||
|
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 ""
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user