From 1546d7495e085b907d2f94081c115cf59d69769f Mon Sep 17 00:00:00 2001 From: Jan Gottschick Date: Fri, 20 Sep 2024 12:15:56 +0200 Subject: [PATCH] * move localize --- generator/frontend.go | 15 +++++++++------ templates/core/config.go.tmpl | 4 ++-- templates/core/configSvc.go.tmpl | 1 + templates/core/info.go.tmpl | 2 +- .../{web/pages => core}/locales/locale.de.toml | 0 .../{web/pages => core}/locales/locale.en.toml | 0 templates/{web/pages => core}/localize.go.tmpl | 11 +++++------ templates/core/log/log.go.tmpl | 4 ++-- templates/main.go.tmpl | 3 ++- templates/rest/getContent.go.tmpl | 3 ++- templates/rest/getIndex.go.tmpl | 3 ++- templates/rest/getRoot.go.tmpl | 3 ++- templates/rest/pageHandlerFunc.go.tmpl | 3 ++- templates/web/pages/languages.templ.tmpl | 2 +- 14 files changed, 31 insertions(+), 23 deletions(-) rename templates/{web/pages => core}/locales/locale.de.toml (100%) rename templates/{web/pages => core}/locales/locale.en.toml (100%) rename templates/{web/pages => core}/localize.go.tmpl (87%) diff --git a/generator/frontend.go b/generator/frontend.go index 43b2563..d659f64 100644 --- a/generator/frontend.go +++ b/generator/frontend.go @@ -25,6 +25,7 @@ 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") @@ -32,7 +33,7 @@ func generateFrontend(spec *openapi3.T, conf GeneratorConfig) { 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) @@ -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) } @@ -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) diff --git a/templates/core/config.go.tmpl b/templates/core/config.go.tmpl index d8dc9b5..e899a90 100644 --- a/templates/core/config.go.tmpl +++ b/templates/core/config.go.tmpl @@ -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 { @@ -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 } diff --git a/templates/core/configSvc.go.tmpl b/templates/core/configSvc.go.tmpl index 6f351ad..aefe1e8 100644 --- a/templates/core/configSvc.go.tmpl +++ b/templates/core/configSvc.go.tmpl @@ -3,6 +3,7 @@ package core // use tags as defined by envconfig type ConfigExt struct { + // attention: use split_words! -> environment name: _REQUIRED_AND_AUTO_SPLIT_VAR // RequiredAndAutoSplitVar string `default:"bar" required:"true" split_words:"true"` } diff --git a/templates/core/info.go.tmpl b/templates/core/info.go.tmpl index 15e02b7..3c2d817 100644 --- a/templates/core/info.go.tmpl +++ b/templates/core/info.go.tmpl @@ -24,5 +24,5 @@ var ( ) func init() { - AppInfo = Info{Service: service, Version: Version, Name: ""} + AppInfo = Info{Service: service, Version: Version, Name: AppInfo.Name} } diff --git a/templates/web/pages/locales/locale.de.toml b/templates/core/locales/locale.de.toml similarity index 100% rename from templates/web/pages/locales/locale.de.toml rename to templates/core/locales/locale.de.toml diff --git a/templates/web/pages/locales/locale.en.toml b/templates/core/locales/locale.en.toml similarity index 100% rename from templates/web/pages/locales/locale.en.toml rename to templates/core/locales/locale.en.toml diff --git a/templates/web/pages/localize.go.tmpl b/templates/core/localize.go.tmpl similarity index 87% rename from templates/web/pages/localize.go.tmpl rename to templates/core/localize.go.tmpl index dd9f535..574229d 100644 --- a/templates/web/pages/localize.go.tmpl +++ b/templates/core/localize.go.tmpl @@ -1,5 +1,5 @@ // Don't edit this file, as it is generated by dredger -package pages +package core import ( "embed" @@ -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" ) @@ -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") @@ -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 != "" { @@ -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 } @@ -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") diff --git a/templates/core/log/log.go.tmpl b/templates/core/log/log.go.tmpl index a94c84b..a4c35fa 100644 --- a/templates/core/log/log.go.tmpl +++ b/templates/core/log/log.go.tmpl @@ -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() @@ -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 { diff --git a/templates/main.go.tmpl b/templates/main.go.tmpl index 08f666c..b14403e 100644 --- a/templates/main.go.tmpl +++ b/templates/main.go.tmpl @@ -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" @@ -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 { diff --git a/templates/rest/getContent.go.tmpl b/templates/rest/getContent.go.tmpl index 305ba87..0e8f40f 100644 --- a/templates/rest/getContent.go.tmpl +++ b/templates/rest/getContent.go.tmpl @@ -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" @@ -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)) } diff --git a/templates/rest/getIndex.go.tmpl b/templates/rest/getIndex.go.tmpl index 76e3141..7204572 100644 --- a/templates/rest/getIndex.go.tmpl +++ b/templates/rest/getIndex.go.tmpl @@ -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" @@ -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)) } diff --git a/templates/rest/getRoot.go.tmpl b/templates/rest/getRoot.go.tmpl index 1d4b03a..48dbe97 100644 --- a/templates/rest/getRoot.go.tmpl +++ b/templates/rest/getRoot.go.tmpl @@ -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" @@ -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)) } diff --git a/templates/rest/pageHandlerFunc.go.tmpl b/templates/rest/pageHandlerFunc.go.tmpl index d4106dd..6854019 100644 --- a/templates/rest/pageHandlerFunc.go.tmpl +++ b/templates/rest/pageHandlerFunc.go.tmpl @@ -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" @@ -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)) } diff --git a/templates/web/pages/languages.templ.tmpl b/templates/web/pages/languages.templ.tmpl index d8b7999..11022ce 100644 --- a/templates/web/pages/languages.templ.tmpl +++ b/templates/web/pages/languages.templ.tmpl @@ -13,7 +13,7 @@ templ Languages(lzr *i18n.Localizer) {