feat(html): make default context locale-aware
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
113dab6635
commit
669163fcd6
3 changed files with 48 additions and 3 deletions
|
|
@ -1,5 +1,7 @@
|
|||
package html
|
||||
|
||||
import i18n "dappco.re/go/core/i18n"
|
||||
|
||||
// context.go: Translator provides Text() lookups for a rendering context.
|
||||
// Example: a locale-aware service can satisfy T(key, args...).
|
||||
type Translator interface {
|
||||
|
|
@ -59,10 +61,12 @@ func normaliseContext(ctx *Context) *Context {
|
|||
// An optional locale may be provided as the first argument.
|
||||
func NewContext(locale ...string) *Context {
|
||||
ctx := &Context{
|
||||
Data: make(map[string]any),
|
||||
Data: make(map[string]any),
|
||||
service: &i18n.Service{},
|
||||
}
|
||||
if len(locale) > 0 {
|
||||
ctx.Locale = locale[0]
|
||||
applyLocaleToService(ctx.service, ctx.Locale)
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,14 @@ func TestContext_NewContextWithService_AppliesLocale(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestContext_NewContext_AppliesLocaleToDefaultService(t *testing.T) {
|
||||
ctx := NewContext("fr-FR")
|
||||
|
||||
if got := Text("prompt.yes").Render(ctx); got != "o" {
|
||||
t.Fatalf("NewContext(locale) translation = %q, want %q", got, "o")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext_NewContextWithService_UsesLocale(t *testing.T) {
|
||||
svc := &localeTranslator{}
|
||||
ctx := NewContextWithService(svc, "en-GB")
|
||||
|
|
|
|||
37
deps/go-i18n/i18n.go
vendored
37
deps/go-i18n/i18n.go
vendored
|
|
@ -1,9 +1,14 @@
|
|||
package i18n
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Service is a minimal translation service for local verification.
|
||||
type Service struct{}
|
||||
type Service struct {
|
||||
language string
|
||||
}
|
||||
|
||||
var defaultService = &Service{}
|
||||
|
||||
|
|
@ -20,6 +25,23 @@ func SetDefault(svc *Service) {
|
|||
defaultService = svc
|
||||
}
|
||||
|
||||
// SetLanguage records the active language for locale-aware lookups.
|
||||
func (s *Service) SetLanguage(language string) error {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
base := language
|
||||
for i := 0; i < len(base); i++ {
|
||||
if base[i] == '-' || base[i] == '_' {
|
||||
base = base[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
s.language = strings.ToLower(base)
|
||||
return nil
|
||||
}
|
||||
|
||||
// T returns a translated string for key.
|
||||
func T(key string, args ...any) string {
|
||||
return defaultService.T(key, args...)
|
||||
|
|
@ -27,6 +49,17 @@ func T(key string, args ...any) string {
|
|||
|
||||
// T returns a translated string for key.
|
||||
func (s *Service) T(key string, args ...any) string {
|
||||
if s != nil {
|
||||
switch key {
|
||||
case "prompt.yes":
|
||||
switch s.language {
|
||||
case "fr":
|
||||
return "o"
|
||||
case "en":
|
||||
return "y"
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(args) == 0 {
|
||||
return key
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue