docs(phrasebook,sieve,towel,wire,wristband): compile README Go examples via src= (quick-261118-qsd)

This commit is contained in:
Jakub Zych
2026-11-18 20:01:05 +01:00
parent c517db564c
commit 6409837b0c
12 changed files with 509 additions and 148 deletions

View File

@@ -3,8 +3,11 @@ package phrasebook_test
import (
"context"
"fmt"
"io/fs"
"testing/fstest"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/compass"
"git.golem15.com/golem15/summercms/modules/phrasebook"
"git.golem15.com/golem15/summercms/modules/towel"
)
@@ -76,3 +79,33 @@ func ExampleTranslator_Choice() {
// One draft
// 7 drafts
}
// title reads the translator published at boot inside a running application.
func title(ctx context.Context, app *backpack.App) string {
tr, ok := app.Lookup[*phrasebook.Translator]()
if !ok {
return "acme.blog::posts.title"
}
return tr.Get(ctx, "acme.blog::posts.title", nil)
}
// blogPlugin ships its lang tree through pact.HasLang.
type blogPlugin struct{}
func (blogPlugin) ID() string { return "acme.blog" }
func (blogPlugin) LangFS() fs.FS { return langFS }
func ExampleActivate() {
app := backpack.New(&compass.Config{})
ctx := towel.WithLocale(context.Background(), "pl")
fmt.Println(title(ctx, app)) // before boot: no translator yet
if err := phrasebook.Activate(app, []blogPlugin{{}}); err != nil {
fmt.Println(err)
return
}
fmt.Println(title(ctx, app))
// Output:
// acme.blog::posts.title
// Posty
}