feat(i18n): add rtl aliases and definite article template func
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
e7bbae8d18
commit
e9f06342a7
8 changed files with 74 additions and 0 deletions
|
|
@ -318,11 +318,21 @@ func (s *CoreService) IsRTL() bool {
|
|||
return s.svc.IsRTL()
|
||||
}
|
||||
|
||||
// RTL reports whether the wrapped service language is right-to-left.
|
||||
func (s *CoreService) RTL() bool {
|
||||
return s.IsRTL()
|
||||
}
|
||||
|
||||
// CurrentIsRTL reports whether the wrapped service language is right-to-left.
|
||||
func (s *CoreService) CurrentIsRTL() bool {
|
||||
return s.IsRTL()
|
||||
}
|
||||
|
||||
// CurrentRTL reports whether the wrapped service language is right-to-left.
|
||||
func (s *CoreService) CurrentRTL() bool {
|
||||
return s.CurrentIsRTL()
|
||||
}
|
||||
|
||||
// PluralCategory returns the plural category for the wrapped service language.
|
||||
func (s *CoreService) PluralCategory(n int) PluralCategory {
|
||||
return s.svc.PluralCategory(n)
|
||||
|
|
|
|||
|
|
@ -950,6 +950,7 @@ func TemplateFuncs() template.FuncMap {
|
|||
"pluralForm": PluralForm,
|
||||
"article": ArticlePhrase,
|
||||
"articlePhrase": ArticlePhrase,
|
||||
"definiteArticle": DefiniteArticle,
|
||||
"definite": DefinitePhrase,
|
||||
"definitePhrase": DefinitePhrase,
|
||||
"quote": Quote,
|
||||
|
|
|
|||
|
|
@ -1122,6 +1122,7 @@ func TestTemplateFuncs(t *testing.T) {
|
|||
"pluralForm",
|
||||
"article",
|
||||
"articlePhrase",
|
||||
"definiteArticle",
|
||||
"definite",
|
||||
"definitePhrase",
|
||||
"quote",
|
||||
|
|
@ -1170,6 +1171,20 @@ func TestTemplateFuncs_Article(t *testing.T) {
|
|||
if got, want := buf.String(), "an apple|the apple"; got != want {
|
||||
t.Fatalf("template article aliases = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
tmpl, err = template.New("").Funcs(TemplateFuncs()).Parse(`{{definiteArticle "apple"}}`)
|
||||
if err != nil {
|
||||
t.Fatalf("Parse() definite article helper failed: %v", err)
|
||||
}
|
||||
|
||||
buf.Reset()
|
||||
if err := tmpl.Execute(&buf, nil); err != nil {
|
||||
t.Fatalf("Execute() definite article helper failed: %v", err)
|
||||
}
|
||||
|
||||
if got, want := buf.String(), "the"; got != want {
|
||||
t.Fatalf("template definite article = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTemplateFuncs_CompositeHelpers(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -355,6 +355,8 @@ func TestCoreService_DelegatesToWrappedService(t *testing.T) {
|
|||
assert.Equal(t, svc.Direction(), coreSvc.CurrentDirection())
|
||||
assert.Equal(t, svc.IsRTL(), coreSvc.IsRTL())
|
||||
assert.Equal(t, svc.IsRTL(), coreSvc.CurrentIsRTL())
|
||||
assert.Equal(t, svc.IsRTL(), coreSvc.RTL())
|
||||
assert.Equal(t, svc.IsRTL(), coreSvc.CurrentRTL())
|
||||
assert.Equal(t, svc.PluralCategory(2), coreSvc.PluralCategory(2))
|
||||
assert.Equal(t, svc.PluralCategory(2), coreSvc.CurrentPluralCategory(2))
|
||||
assert.Equal(t, svc.Mode(), coreSvc.CurrentMode())
|
||||
|
|
|
|||
16
localise.go
16
localise.go
|
|
@ -132,6 +132,13 @@ func CurrentDirection() TextDirection {
|
|||
// rtl := i18n.IsRTL()
|
||||
func IsRTL() bool { return Direction() == DirRTL }
|
||||
|
||||
// RTL is a short alias for IsRTL.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// rtl := i18n.RTL()
|
||||
func RTL() bool { return IsRTL() }
|
||||
|
||||
// CurrentIsRTL returns true if the current default language uses
|
||||
// right-to-left text.
|
||||
//
|
||||
|
|
@ -142,6 +149,15 @@ func CurrentIsRTL() bool {
|
|||
return defaultServiceValue(false, func(svc *Service) bool { return svc.IsRTL() })
|
||||
}
|
||||
|
||||
// CurrentRTL is a short alias for CurrentIsRTL.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// rtl := i18n.CurrentRTL()
|
||||
func CurrentRTL() bool {
|
||||
return CurrentIsRTL()
|
||||
}
|
||||
|
||||
// CurrentPluralCategory returns the plural category for the current default language.
|
||||
//
|
||||
// Example:
|
||||
|
|
|
|||
|
|
@ -232,6 +232,16 @@ func TestIsRTL_Good(t *testing.T) {
|
|||
assert.False(t, IsRTL(), "English should not be RTL")
|
||||
}
|
||||
|
||||
// --- Package-level RTL ---
|
||||
|
||||
func TestRTL_Good(t *testing.T) {
|
||||
svc, err := New()
|
||||
require.NoError(t, err)
|
||||
SetDefault(svc)
|
||||
|
||||
assert.Equal(t, IsRTL(), RTL())
|
||||
}
|
||||
|
||||
// --- Package-level CurrentIsRTL ---
|
||||
|
||||
func TestCurrentIsRTL_Good(t *testing.T) {
|
||||
|
|
@ -242,6 +252,16 @@ func TestCurrentIsRTL_Good(t *testing.T) {
|
|||
assert.False(t, CurrentIsRTL(), "English should not be RTL")
|
||||
}
|
||||
|
||||
// --- Package-level CurrentRTL ---
|
||||
|
||||
func TestCurrentRTL_Good(t *testing.T) {
|
||||
svc, err := New()
|
||||
require.NoError(t, err)
|
||||
SetDefault(svc)
|
||||
|
||||
assert.Equal(t, CurrentIsRTL(), CurrentRTL())
|
||||
}
|
||||
|
||||
// --- Package-level CurrentPluralCategory ---
|
||||
|
||||
func TestCurrentPluralCategory_Good(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -428,6 +428,13 @@ func (s *Service) IsRTL() bool { return s.Direction() == DirRTL }
|
|||
func (s *Service) CurrentIsRTL() bool {
|
||||
return s.IsRTL()
|
||||
}
|
||||
|
||||
// RTL is a short alias for IsRTL.
|
||||
func (s *Service) RTL() bool { return s.IsRTL() }
|
||||
|
||||
// CurrentRTL is a short alias for CurrentIsRTL.
|
||||
func (s *Service) CurrentRTL() bool { return s.CurrentIsRTL() }
|
||||
|
||||
func (s *Service) CurrentDebug() bool {
|
||||
return s.Debug()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -175,6 +175,9 @@ func TestServiceCurrentStateAliases(t *testing.T) {
|
|||
if got, want := svc.CurrentIsRTL(), svc.IsRTL(); got != want {
|
||||
t.Fatalf("CurrentIsRTL() = %v, want %v", got, want)
|
||||
}
|
||||
if got, want := svc.CurrentRTL(), svc.IsRTL(); got != want {
|
||||
t.Fatalf("CurrentRTL() = %v, want %v", got, want)
|
||||
}
|
||||
if got, want := svc.CurrentHandlers(), svc.Handlers(); len(got) != len(want) {
|
||||
t.Fatalf("CurrentHandlers() len = %d, want %d", len(got), len(want))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue