]> git.earlybird.gay Git - today/commitdiff
Extend language functionality to parts
authorearly <me@earlybird.gay>
Mon, 25 Nov 2024 12:04:09 +0000 (05:04 -0700)
committerearly <me@earlybird.gay>
Mon, 25 Nov 2024 12:04:09 +0000 (05:04 -0700)
web/page/page.go
web/part/part.go

index c718511d23724df10a3719485502c705f9bd2d15..608c714420f2482994d82c14f308a2da1c1d83bb 100644 (file)
@@ -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
index 5b6733d04267c8ab039d6c48d1fc07ebf67256c3..48b11cf189574a911f165132c9497523c18c5f04 100644 (file)
@@ -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