112 lines
3.0 KiB
Go
112 lines
3.0 KiB
Go
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"
|
|
)
|
|
|
|
// langFS stands in for the plugin's embedded lang directory.
|
|
var langFS = fstest.MapFS{
|
|
"lang/en/posts.yaml": {Data: []byte(`title: Posts
|
|
greeting: "Hello, :name"
|
|
shout: "Welcome, :NAME"
|
|
count:
|
|
one: ":count post"
|
|
other: ":count posts"
|
|
drafts: "{0} No drafts|{1} One draft|[2,*] :count drafts"
|
|
`)},
|
|
"lang/pl/posts.yaml": {Data: []byte(`title: Posty
|
|
count:
|
|
one: ":count post"
|
|
few: ":count posty"
|
|
many: ":count postów"
|
|
other: ":count posta"
|
|
`)},
|
|
}
|
|
|
|
func ExampleTranslator_Get() {
|
|
cat := phrasebook.NewCatalog()
|
|
if err := cat.Load("acme.blog", langFS); err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
tr := phrasebook.NewTranslator(cat, phrasebook.Options{Locale: "en", Fallback: "en"})
|
|
|
|
// surf stores the request locale on the context; here the example does.
|
|
ctx := towel.WithLocale(context.Background(), "pl")
|
|
fmt.Println(tr.Get(ctx, "acme.blog::posts.title", nil))
|
|
// pl has no greeting, so the fallback locale answers.
|
|
fmt.Println(tr.Get(ctx, "acme.blog::posts.greeting", map[string]string{"name": "Ada"}))
|
|
fmt.Println(tr.GetIn("en", "acme.blog::posts.shout", map[string]string{"name": "Ada"}))
|
|
// A missing key comes back as the key.
|
|
fmt.Println(tr.Get(ctx, "acme.blog::posts.missing", nil))
|
|
// Output:
|
|
// Posty
|
|
// Hello, Ada
|
|
// Welcome, ADA
|
|
// acme.blog::posts.missing
|
|
}
|
|
|
|
func ExampleTranslator_Choice() {
|
|
cat := phrasebook.NewCatalog()
|
|
if err := cat.Load("acme.blog", langFS); err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
tr := phrasebook.NewTranslator(cat, phrasebook.Options{Locale: "en", Fallback: "en"})
|
|
|
|
for _, n := range []int{1, 3, 5, 22} {
|
|
fmt.Println(tr.ChoiceIn("pl", "acme.blog::posts.count", n, nil))
|
|
}
|
|
fmt.Println(tr.ChoiceIn("en", "acme.blog::posts.count", 5, nil))
|
|
for _, n := range []int{0, 1, 7} {
|
|
fmt.Println(tr.ChoiceIn("en", "acme.blog::posts.drafts", n, nil))
|
|
}
|
|
// Output:
|
|
// 1 post
|
|
// 3 posty
|
|
// 5 postów
|
|
// 22 posty
|
|
// 5 posts
|
|
// No drafts
|
|
// 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
|
|
}
|