diff --git a/internal/domain/config/entity/language.go b/internal/domain/config/entity/language.go index 89c15c3..e6c3611 100644 --- a/internal/domain/config/entity/language.go +++ b/internal/domain/config/entity/language.go @@ -85,6 +85,14 @@ func (l *Language) LanguageKeys() []string { return l.indices } +func (l *Language) LanguageIndexes() []int { + var indexes []int + for i, _ := range l.indices { + indexes = append(indexes, i) + } + return indexes +} + func (l *Language) GetLanguageIndex(lang string) (int, error) { for i, v := range l.indices { if v == lang { @@ -94,6 +102,10 @@ func (l *Language) GetLanguageIndex(lang string) (int, error) { return -1, errors.New("language not found in indices") } +func (l *Language) GetLanguageByIndex(idx int) string { + return l.indices[idx] +} + func (l *Language) GetLanguageName(lang string) string { for c, v := range l.Configs { if c == lang { diff --git a/internal/domain/config/entity/service.go b/internal/domain/config/entity/service.go index d6ddab2..ba39de5 100644 --- a/internal/domain/config/entity/service.go +++ b/internal/domain/config/entity/service.go @@ -1,6 +1,8 @@ package entity -import "github.com/gohugonet/hugoverse/internal/domain/config/valueobject" +import ( + "github.com/gohugonet/hugoverse/internal/domain/config/valueobject" +) type Service struct { valueobject.ServiceConfig diff --git a/internal/domain/content/valueobject/site.go b/internal/domain/content/valueobject/site.go index 1fa7f73..21806f2 100644 --- a/internal/domain/content/valueobject/site.go +++ b/internal/domain/content/valueobject/site.go @@ -14,15 +14,17 @@ import ( type Site struct { Item - Title string `json:"title"` - Description string `json:"description"` - BaseURL string `json:"base_url"` - Theme string `json:"theme"` - Params string `json:"params"` - Owner string `json:"owner"` - WorkingDir string `json:"working_dir"` - Languages []string `json:"languages"` - Menus []string `json:"menus"` + Title string `json:"title"` + Description string `json:"description"` + BaseURL string `json:"base_url"` + Theme string `json:"theme"` + Params string `json:"params"` + Owner string `json:"owner"` + WorkingDir string `json:"working_dir"` + GoogleAnalytics string `json:"google_analytics"` + DefaultContentLanguage string `json:"default_content_language"` + Languages []string `json:"languages"` + Menus []string `json:"menus"` } // MarshalEditor writes a buffer of html to edit a Song within the CMS @@ -78,6 +80,20 @@ func (s *Site) MarshalEditor() ([]byte, error) { "placeholder": "Enter the project file system dir here", }), }, + editor.Field{ + View: editor.Input("GoogleAnalytics", s, map[string]string{ + "label": "GoogleAnalytics", + "type": "text", + "placeholder": "Enter the GoogleAnalytics here", + }), + }, + editor.Field{ + View: editor.Input("DefaultLanguage", s, map[string]string{ + "label": "DefaultLanguage", + "type": "text", + "placeholder": "Enter the DefaultLanguage here", + }), + }, editor.Field{ View: editor.Input("Languages", s, map[string]string{ "label": "Languages", @@ -201,6 +217,9 @@ description = "{{.Description}}" baseURL = "{{.BaseURL}}" owner = "{{.Owner}}" +defaultContentLanguage = "{{.DefaultContentLanguage}}" +googleAnalytics = "{{.GoogleAnalytics}}" + [module] [[module.imports]] path = "{{.Theme}}" diff --git a/internal/domain/contenthub/entity/pagebuilder.go b/internal/domain/contenthub/entity/pagebuilder.go index 51847f7..f0b2500 100644 --- a/internal/domain/contenthub/entity/pagebuilder.go +++ b/internal/domain/contenthub/entity/pagebuilder.go @@ -32,6 +32,7 @@ type PageBuilder struct { kind string singular string term string + langIdx int fm *valueobject.FrontMatter fmParser *valueobject.FrontMatterParser @@ -50,9 +51,16 @@ func (b *PageBuilder) WithSource(source *Source) *PageBuilder { return &cloneBuilder } +func (b *PageBuilder) WithLangIdx(idx int) *PageBuilder { + b.langIdx = idx + + return b +} + func (b *PageBuilder) reset() { b.c = nil b.kind = "" + b.langIdx = -1 } func (b *PageBuilder) Build() (contenthub.Page, error) { @@ -82,8 +90,14 @@ func (b *PageBuilder) KindBuild() (contenthub.Page, error) { return nil, err } - if err := b.parseLanguageByDefault(); err != nil { - return nil, err + if b.langIdx == -1 { + if err := b.parseLanguageByDefault(); err != nil { + return nil, err + } + } else { + if err := b.parseLanguageByIdx(b.langIdx); err != nil { + return nil, err + } } b.fm = &valueobject.FrontMatter{} @@ -392,6 +406,14 @@ func (b *PageBuilder) parseLanguageByDefault() error { return nil } +func (b *PageBuilder) parseLanguageByIdx(langIdx int) error { + dl := b.LangSvc.GetLanguageByIndex(langIdx) + + b.source.Identity.Lang = dl + b.source.Identity.LangIdx = langIdx + return nil +} + func (b *PageBuilder) parseLanguage() error { l, ok := b.LangSvc.GetSourceLang(b.source.File.Root()) if ok { diff --git a/internal/domain/contenthub/entity/pagemap.go b/internal/domain/contenthub/entity/pagemap.go index 2801d08..3c22f5c 100644 --- a/internal/domain/contenthub/entity/pagemap.go +++ b/internal/domain/contenthub/entity/pagemap.go @@ -187,8 +187,11 @@ func (m *PageMap) assembleStructurePages() error { return err } - if err := m.PageBuilder.Section.Assemble(m.TreePages, m.PageBuilder); err != nil { - return err + for _, idx := range m.PageBuilder.LangSvc.LanguageIndexes() { + tree := m.TreePages.Shape(0, idx) + if err := m.PageBuilder.Section.Assemble(tree, m.PageBuilder, idx); err != nil { + return err + } } if err := m.addMissingStandalone(); err != nil { diff --git a/internal/domain/contenthub/entity/section.go b/internal/domain/contenthub/entity/section.go index d77c61d..2582321 100644 --- a/internal/domain/contenthub/entity/section.go +++ b/internal/domain/contenthub/entity/section.go @@ -30,7 +30,7 @@ func (s *Section) isSectionExist(section string) bool { return false } -func (s *Section) Assemble(pages *doctree.NodeShiftTree[*PageTreesNode], pb *PageBuilder) error { +func (s *Section) Assemble(pages *doctree.NodeShiftTree[*PageTreesNode], pb *PageBuilder, langIdx int) error { s.seen = make(map[string]bool) var w *doctree.NodeShiftTreeWalker[*PageTreesNode] @@ -85,7 +85,7 @@ func (s *Section) Assemble(pages *doctree.NodeShiftTree[*PageTreesNode], pb *Pag nn := w.Tree.Get(sectionBase) if nn == nil { - sectionPage, err := pb.WithSource(sectionSource).KindBuild() + sectionPage, err := pb.WithSource(sectionSource).WithLangIdx(langIdx).KindBuild() if err != nil { return false, err } diff --git a/internal/domain/contenthub/type.go b/internal/domain/contenthub/type.go index 5fd699f..05e60cb 100644 --- a/internal/domain/contenthub/type.go +++ b/internal/domain/contenthub/type.go @@ -81,7 +81,9 @@ type LangService interface { IsLanguageValid(lang string) bool GetSourceLang(source string) (string, bool) GetLanguageIndex(lang string) (int, error) + GetLanguageByIndex(idx int) string DefaultLanguage() string + LanguageIndexes() []int } type Taxonomy interface { diff --git a/internal/domain/site/entity/navigation.go b/internal/domain/site/entity/navigation.go index 7a8be80..c563804 100644 --- a/internal/domain/site/entity/navigation.go +++ b/internal/domain/site/entity/navigation.go @@ -2,6 +2,7 @@ package entity import ( "fmt" + "github.com/gohugonet/hugoverse/internal/domain/site" "github.com/gohugonet/hugoverse/internal/domain/site/valueobject" "github.com/gohugonet/hugoverse/pkg/compare" "sort" @@ -9,7 +10,18 @@ import ( type Navigation struct { taxonomies TaxonomyList - menus valueobject.Menus + menus map[string]valueobject.Menus +} + +func NewNavigation(langSvc site.LanguageService) *Navigation { + n := &Navigation{ + taxonomies: make(TaxonomyList), + menus: make(map[string]valueobject.Menus), + } + for _, l := range langSvc.LanguageKeys() { + n.menus[l] = valueobject.NewEmptyMenus() + } + return n } // The TaxonomyList is a list of all taxonomies and their values diff --git a/internal/domain/site/entity/siteinit.go b/internal/domain/site/entity/siteinit.go index 89fa9f2..551f72f 100644 --- a/internal/domain/site/entity/siteinit.go +++ b/internal/domain/site/entity/siteinit.go @@ -10,23 +10,21 @@ import ( type siteInit struct { prevNext *lazy.Init prevNextInSection *lazy.Init - menus *lazy.Init + menus map[string]*lazy.Init taxonomies *lazy.Init } func (init *siteInit) Reset() { init.prevNext.Reset() init.prevNextInSection.Reset() - init.menus.Reset() + for k := range init.menus { + init.menus[k].Reset() + } init.taxonomies.Reset() } func (s *Site) PrepareLazyLoads() { - s.lazy = &siteInit{} - - var init lazy.Init - - s.lazy.menus = init.Branch(func() (any, error) { + initMenu := func() (any, error) { menus := valueobject.NewEmptyMenus() menusConf := s.ConfigSvc.Menus() @@ -69,10 +67,18 @@ func (s *Site) PrepareLazyLoads() { s.Log.Errorf("Reserved links Menus: %v", err) } - s.Navigation.menus = menus - + s.Navigation.menus[s.Language.currentLanguage] = menus return nil, nil - }) + } + + s.lazy = &siteInit{ + menus: map[string]*lazy.Init{}, + } + + var init lazy.Init + for _, lang := range s.LanguageSvc.LanguageKeys() { + s.lazy.menus[lang] = init.Branch(initMenu) + } s.lazy.taxonomies = init.Branch(func() (any, error) { s.Navigation.taxonomies = make(TaxonomyList) @@ -115,9 +121,14 @@ func (s *Site) Taxonomies() TaxonomyList { } func (s *Site) Menus() valueobject.Menus { - if _, err := s.lazy.menus.Do(); err != nil { - s.Log.Errorf("Menus: %v", err) + init, ok := s.lazy.menus[s.Language.currentLanguage] + if ok { + if _, err := init.Do(); err != nil { + s.Log.Errorf("Menus: %v", err) + } + } else { + s.Log.Errorf("Menus: no init for %s", s.Language.currentLanguage) } - return s.Navigation.menus + return s.Navigation.menus[s.Language.currentLanguage] } diff --git a/internal/domain/site/factory/site.go b/internal/domain/site/factory/site.go index 4ed76e2..0e308a4 100644 --- a/internal/domain/site/factory/site.go +++ b/internal/domain/site/factory/site.go @@ -47,7 +47,7 @@ func New(services site.Services) *entity.Site { Language: &entity.Language{ LangSvc: services, }, - Navigation: &entity.Navigation{}, + Navigation: entity.NewNavigation(services), Log: loggers.NewDefault(), } diff --git a/internal/interfaces/cli/vercurr.go b/internal/interfaces/cli/vercurr.go index ef0bbea..f4fe9d6 100644 --- a/internal/interfaces/cli/vercurr.go +++ b/internal/interfaces/cli/vercurr.go @@ -3,6 +3,6 @@ package cli var CurrentVersion = Version{ Major: 0, Minor: 1, - PatchLevel: 3, + PatchLevel: 4, Suffix: "", } diff --git a/manifest.json b/manifest.json index 1b93c25..028a27b 100644 --- a/manifest.json +++ b/manifest.json @@ -1,5 +1,5 @@ { - "version": "0.1.3", + "version": "0.1.4", "name": "Hugoverse", "description": "Headless CMS for Hugo", "author": "sunwei",