From c44dfd73a5297ce237680b6266a6e7be5ce47167 Mon Sep 17 00:00:00 2001 From: early Date: Mon, 25 Nov 2024 05:04:09 -0700 Subject: [PATCH] Extend language functionality to parts --- web/page/page.go | 6 +++++- web/part/part.go | 35 ++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/web/page/page.go b/web/page/page.go index c718511..608c714 100644 --- a/web/page/page.go +++ b/web/page/page.go @@ -246,7 +246,11 @@ func (p *Page) DefaultLanguage() language.Tag { } func (p *Page) Source(lang language.Tag) include.Opener { - return p.sources[lang] + if source, ok := p.sources[lang]; ok { + return source + } else { + return p.sources[p.defaultLanguage] + } } // FileDependencies returns a list of absolute paths to files that are used in diff --git a/web/part/part.go b/web/part/part.go index 5b6733d..48b11cf 100644 --- a/web/part/part.go +++ b/web/part/part.go @@ -7,15 +7,18 @@ import ( "text/template" "git.earlybird.gay/today/include" + "git.earlybird.gay/today/localization/po" "git.earlybird.gay/today/web/internal/compile" "git.earlybird.gay/today/web/render" "golang.org/x/text/language" ) type Part struct { - name string - languages []language.Tag - sources map[language.Tag]include.Opener + name string + languages []language.Tag + translations map[language.Tag]po.Entries + defaultLanguage language.Tag + sources map[language.Tag]include.Opener noTag bool includes []compile.Source @@ -77,9 +80,15 @@ func New(name string, source string, optional ...Config) *Part { p := new(Part) // Assign basic parameters p.name = name - p.languages = []language.Tag{language.English} - p.sources = make(map[language.Tag]include.Opener) - p.sources[language.English], p.err = include.File(source, "git.earlybird.gay/today/web/part") + sources, localeMeta, err := include.LocalizedFiles(source, "git.earlybird.gay/today/web/page") + if err != nil { + p.err = err + return p + } + p.languages = localeMeta.Languages + p.translations = localeMeta.Translations + p.defaultLanguage = localeMeta.DefaultLanguage + p.sources = sources if p.err != nil { return p } @@ -126,8 +135,20 @@ func (p *Part) Languages() []language.Tag { return p.languages } +func (p *Part) Translations() map[language.Tag]po.Entries { + return p.translations +} + +func (p *Part) DefaultLanguage() language.Tag { + return p.defaultLanguage +} + func (p *Part) Source(lang language.Tag) include.Opener { - return p.sources[lang] + if source, ok := p.sources[lang]; ok { + return source + } else { + return p.sources[p.defaultLanguage] + } } // FileDependencies returns a list of absolute paths to files that are used in -- 2.39.5