Skip to content

Commit

Permalink
* move localize
Browse files Browse the repository at this point in the history
  • Loading branch information
JGottschick committed Sep 20, 2024
1 parent 823e25e commit 1546d74
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 23 deletions.
15 changes: 9 additions & 6 deletions generator/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ func generateFrontend(spec *openapi3.T, conf GeneratorConfig) {
generateOpenAPIDoc(conf)

// create folders
corePath := filepath.Join(conf.OutputPath, "core")
restPath := filepath.Join(conf.OutputPath, "rest")
frontendPath := filepath.Join(conf.OutputPath, "web")
javascriptPath := filepath.Join(frontendPath, "js")
stylesheetPath := filepath.Join(frontendPath, "css")
imagesPath := filepath.Join(frontendPath, "images")
fontsPath := filepath.Join(stylesheetPath, "fonts")
pagesPath := filepath.Join(frontendPath, "pages")
localesPath := filepath.Join(pagesPath, "locales")
localesPath := filepath.Join(corePath, "locales")
publicPath := filepath.Join(frontendPath, "public")

fs.GenerateFolder(frontendPath)
Expand Down Expand Up @@ -70,9 +71,15 @@ func generateFrontend(spec *openapi3.T, conf GeneratorConfig) {
// files in web directory
fs.CopyWebFile("web", frontendPath, "web.go", true)

// files in core directory
createFileFromTemplate(filepath.Join(corePath, "localize.go"), "templates/core/localize.go.tmpl", conf)
if _, err := os.Stat(filepath.Join(localesPath, "locale.de.toml")); errors.Is(err, os.ErrNotExist) {
createFileFromTemplate(filepath.Join(localesPath, "locale.de.toml"), "templates/core/locales/locale.de.toml", conf)
createFileFromTemplate(filepath.Join(localesPath, "locale.en.toml"), "templates/core/locales/locale.en.toml", conf)
}

// files in pages directory
fs.CopyWebFile("web/pages", restPath, "render.go", true)
createFileFromTemplate(filepath.Join(pagesPath, "localize.go"), "templates/web/pages/localize.go.tmpl", conf)
if _, err := os.Stat(filepath.Join(pagesPath, "languages.templ")); errors.Is(err, os.ErrNotExist) {
createFileFromTemplate(filepath.Join(pagesPath, "languages.templ"), "templates/web/pages/languages.templ.tmpl", conf)
}
Expand Down Expand Up @@ -100,10 +107,6 @@ func generateFrontend(spec *openapi3.T, conf GeneratorConfig) {
updateOAPIOperation(op, "GetContent", "successfully deliver content page", "200")
spec.AddOperation("/content.html", http.MethodGet, op)
}
if _, err := os.Stat(filepath.Join(localesPath, "locale.de.toml")); errors.Is(err, os.ErrNotExist) {
createFileFromTemplate(filepath.Join(localesPath, "locale.de.toml"), "templates/web/pages/locales/locale.de.toml", conf)
createFileFromTemplate(filepath.Join(localesPath, "locale.en.toml"), "templates/web/pages/locales/locale.en.toml", conf)
}

// files in public directory
fs.CopyWebFile("web", publicPath, "README-public.md", false)
Expand Down
4 changes: 2 additions & 2 deletions templates/core/config.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func init() {
AppConfig.Title = AppConfig.Name
}

log.Setup(AppConfig.Name, AppInfo.Service, AppConfig.LogFile, AppConfig.Debug)
log.Setup(AppConfig.Name, AppConfig.Service, AppConfig.LogFile, AppConfig.Debug)

err := godotenv.Load()
if err != nil {
Expand All @@ -92,7 +92,7 @@ func init() {
os.Exit(0)
}

log.Setup(AppConfig.Name, AppInfo.Service, AppConfig.LogFile, AppConfig.Debug)
log.Setup(AppConfig.Name, AppConfig.Service, AppConfig.LogFile, AppConfig.Debug)
if AppConfig.SessionKey == "" {
AppConfig.SessionKey = AppConfig.Name
}
Expand Down
1 change: 1 addition & 0 deletions templates/core/configSvc.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core

// use tags as defined by envconfig
type ConfigExt struct {
// attention: use split_words! -> environment name: <your prefix / library>_REQUIRED_AND_AUTO_SPLIT_VAR
// RequiredAndAutoSplitVar string `default:"bar" required:"true" split_words:"true"`
}

Expand Down
2 changes: 1 addition & 1 deletion templates/core/info.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ var (
)

func init() {
AppInfo = Info{Service: service, Version: Version, Name: ""}
AppInfo = Info{Service: service, Version: Version, Name: AppInfo.Name}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Don't edit this file, as it is generated by dredger
package pages
package core

import (
"embed"
Expand All @@ -11,7 +11,6 @@ import (
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"

"{{ lcfirst ( camelcase .ModuleName ) }}/core"
"{{ lcfirst ( camelcase .ModuleName ) }}/core/log"
)

Expand All @@ -23,7 +22,7 @@ var Bundle *i18n.Bundle
func init() {
Bundle = i18n.NewBundle(language.English)
Bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
for _, l := range core.AppConfig.Languages {
for _, l := range AppConfig.Languages {
_, err := Bundle.LoadMessageFileFS(LocaleFS, "locales/locale."+l+".toml")
if err != nil {
log.Error().Str("filename", "locales/locale."+l+".toml").Err(err).Msg("Loading catalog failed")
Expand All @@ -37,7 +36,7 @@ func init() {
// extract language from the HTTP header
// If x-language is set it will override the browsers preferred languages
func Language(c echo.Context) string {
lang := core.AppConfig.Language
lang := AppConfig.Language
if l := c.Request().Header.Get("x-language"); l != "" {
lang = l
} else if l := c.Request().URL.Query().Get("language"); l != "" {
Expand All @@ -53,7 +52,7 @@ func Language(c echo.Context) string {
newPrio = f
}
} else {
for _, l := range core.AppConfig.Languages {
for _, l := range AppConfig.Languages {
if l == nl {
newLang = nl
}
Expand All @@ -69,7 +68,7 @@ func Language(c echo.Context) string {
return lang
}

func localize(localizer *i18n.Localizer, msg string) string {
func Localize(localizer *i18n.Localizer, msg string) string {
lcMsg, err := localizer.LocalizeMessage(&i18n.Message{ID: msg})
if err != nil {
log.Error().Err(err).Str("ID", msg).Msg("Localizing string failed")
Expand Down
4 changes: 2 additions & 2 deletions templates/core/log/log.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Setup(name string, service string, logFilename string, debug bool) {
console := zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}).
With().
Timestamp().
Str("service", service).
Str("svc", service).
Str("name", name).
Logger()

Expand All @@ -58,7 +58,7 @@ func Setup(name string, service string, logFilename string, debug bool) {
logger := zerolog.New(logfile).
With().
Timestamp().
Str("service", service).
Str("svc", service).
Str("name", name).
Logger()
if debug {
Expand Down
3 changes: 2 additions & 1 deletion templates/main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"{{ lcfirst ( camelcase .ModuleName ) }}/core/tracing"
"{{ lcfirst ( camelcase .ModuleName ) }}/rest"
"{{ lcfirst ( camelcase .ModuleName ) }}/rest/middleware"
"{{ lcfirst ( camelcase .ModuleName ) }}/web"
_ "embed"
"context"

Expand All @@ -28,7 +29,7 @@ var embeddedFS embed.FS
func main() {
// ToDo: Add basic command line options as _-n_/_-name_ and _-d_/_-debug_ using flags

log.Info().Str("svc", core.AppConfig.Service).Msg("Starting service")
log.Info().Msg("Starting service")

// init Opentelemetry
if core.AppConfig.Tracing {
Expand Down
3 changes: 2 additions & 1 deletion templates/rest/getContent.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package rest

import (
"{{ lcfirst ( camelcase .ModuleName ) }}/core"
"{{ lcfirst ( camelcase .ModuleName ) }}/core/log"
"{{ lcfirst ( camelcase .ModuleName ) }}/core/tracing"
"{{ lcfirst ( camelcase .ModuleName ) }}/web/pages"
Expand All @@ -26,6 +27,6 @@ func GetContent(c echo.Context) error {
// return c.NoContent(http.StatusInternalServerError)
// }

lzr := i18n.NewLocalizer(pages.Bundle, pages.Language(c))
lzr := i18n.NewLocalizer(core.Bundle, core.Language(c))
return Render(c, http.StatusOK, pages.Content(lzr))
}
3 changes: 2 additions & 1 deletion templates/rest/getIndex.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package rest

import (
"{{ lcfirst ( camelcase .ModuleName ) }}/core"
"{{ lcfirst ( camelcase .ModuleName ) }}/core/log"
"{{ lcfirst ( camelcase .ModuleName ) }}/core/tracing"
"{{ lcfirst ( camelcase .ModuleName ) }}/web/pages"
Expand All @@ -26,6 +27,6 @@ func GetIndex(c echo.Context) error {
// return c.NoContent(http.StatusInternalServerError)
// }

lzr := i18n.NewLocalizer(pages.Bundle, pages.Language(c))
lzr := i18n.NewLocalizer(core.Bundle, core.Language(c))
return Render(c, http.StatusOK, pages.Index(lzr))
}
3 changes: 2 additions & 1 deletion templates/rest/getRoot.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package rest

import (
"{{ lcfirst ( camelcase .ModuleName ) }}/core"
"{{ lcfirst ( camelcase .ModuleName ) }}/core/log"
"{{ lcfirst ( camelcase .ModuleName ) }}/core/tracing"
"{{ lcfirst ( camelcase .ModuleName ) }}/web/pages"
Expand All @@ -26,6 +27,6 @@ func GetRoot(c echo.Context) error {
// return c.NoContent(http.StatusInternalServerError)
// }

lzr := i18n.NewLocalizer(pages.Bundle, pages.Language(c))
lzr := i18n.NewLocalizer(core.Bundle, core.Language(c))
return Render(c, http.StatusOK, pages.Index(lzr))
}
3 changes: 2 additions & 1 deletion templates/rest/pageHandlerFunc.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package rest

import (
"{{ lcfirst ( camelcase .ModuleName ) }}/core"
"{{ lcfirst ( camelcase .ModuleName ) }}/core/tracing"
"{{ lcfirst ( camelcase .ModuleName ) }}/core/log"
"{{ lcfirst ( camelcase .ModuleName ) }}/entities"
Expand Down Expand Up @@ -113,6 +114,6 @@ func {{ .OperationID }}(c echo.Context) error {
{{- end }}


lzr := i18n.NewLocalizer(pages.Bundle, pages.Language(c))
lzr := i18n.NewLocalizer(core.Bundle, core.Language(c))
return Render(c, http.StatusOK, pages.{{ .OperationID }}(lzr))
}
2 changes: 1 addition & 1 deletion templates/web/pages/languages.templ.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ templ Languages(lzr *i18n.Localizer) {
<a class="nav-link"
_={ "on click call sessionStorage.setItem('pd_language','"+language+"')"}
href={ templ.URL("index.html?language=" + language) }>
{ localize(lzr,language) }
{ core.Localize(lzr,language) }
</a>
</li>
}
Expand Down

0 comments on commit 1546d74

Please sign in to comment.