🏷️ Created language Direction domain

This commit is contained in:
Maxim Lebedev 2024-01-27 09:55:48 +06:00
parent 1718bf6f7e
commit 25494915ba
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
3 changed files with 33 additions and 10 deletions

View File

@ -0,0 +1,23 @@
package domain
import "source.toby3d.me/toby3d/home/internal/common"
type Direction struct{ direction string }
var (
DirectionUnd Direction = Direction{} // "und"
DirectionLeftToRight Direction = Direction{"ltr"} // "ltr"
DirectionRightToLeft Direction = Direction{"rtl"} // "rtl"
)
func (d Direction) String() string {
if d.direction == "" {
return common.Und
}
return d.direction
}
func (d Direction) GoString() string {
return "domain.Direction(" + d.String() + ")"
}

View File

@ -13,7 +13,7 @@ type Language struct {
code string
lang string
name string
dir string
dir Direction
}
var LanguageUnd Language = Language{} // "und"
@ -26,13 +26,13 @@ func NewLanguage(raw string) Language {
out := Language{
code: tag.String(),
dir: "ltr",
dir: DirectionLeftToRight,
name: strings.ToLower(display.Self.Name(tag)),
}
switch tag {
case language.Arabic, language.Persian, language.Hebrew, language.Urdu:
out.dir = "rtl"
out.dir = DirectionRightToLeft
}
base, _ := tag.Base()
@ -49,7 +49,7 @@ func (l Language) Code() string {
return l.code
}
func (l Language) Dir() string {
func (l Language) Dir() Direction {
return l.dir
}

View File

@ -60,13 +60,13 @@ func TestLanguage_Dir(t *testing.T) {
for name, tc := range map[string]struct {
input string
expect string
expect domain.Direction
}{
"2letter": {"en", "ltr"},
"rtl": {"ur", "rtl"},
"3letter": {"eng", "ltr"},
"region": {"en-US", "ltr"},
common.Und: {"", ""},
"2letter": {"en", domain.DirectionLeftToRight},
"rtl": {"ur", domain.DirectionRightToLeft},
"3letter": {"eng", domain.DirectionLeftToRight},
"region": {"en-US", domain.DirectionLeftToRight},
common.Und: {"", domain.DirectionUnd},
} {
name, tc := name, tc