fix(i18n): preserve exact namespace matches
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
fd97bc7048
commit
1fe523b921
2 changed files with 31 additions and 6 deletions
18
service.go
18
service.go
|
|
@ -364,7 +364,11 @@ func (s *Service) Prompt(key string) string {
|
|||
if key == "" {
|
||||
return ""
|
||||
}
|
||||
return s.T(namespaceLookupKey("prompt", key))
|
||||
lookupKey := namespaceLookupKey("prompt", key)
|
||||
if text, ok := s.translateWithStatus(lookupKey); ok {
|
||||
return text
|
||||
}
|
||||
return lookupKey
|
||||
}
|
||||
|
||||
// CurrentPrompt is a short alias for Prompt.
|
||||
|
|
@ -378,17 +382,19 @@ func (s *Service) Lang(key string) string {
|
|||
if key == "" {
|
||||
return ""
|
||||
}
|
||||
if got := s.T(namespaceLookupKey("lang", key)); got != namespaceLookupKey("lang", key) {
|
||||
return got
|
||||
lookupKey := namespaceLookupKey("lang", key)
|
||||
if text, ok := s.translateWithStatus(lookupKey); ok {
|
||||
return text
|
||||
}
|
||||
if idx := indexAny(key, "-_"); idx > 0 {
|
||||
if base := key[:idx]; base != "" {
|
||||
if got := s.T(namespaceLookupKey("lang", base)); got != namespaceLookupKey("lang", base) {
|
||||
return got
|
||||
baseLookupKey := namespaceLookupKey("lang", base)
|
||||
if text, ok := s.translateWithStatus(baseLookupKey); ok {
|
||||
return text
|
||||
}
|
||||
}
|
||||
}
|
||||
return namespaceLookupKey("lang", key)
|
||||
return lookupKey
|
||||
}
|
||||
|
||||
func (s *Service) AvailableLanguages() []string {
|
||||
|
|
|
|||
|
|
@ -434,6 +434,25 @@ func TestServicePromptAndLang(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestServicePromptAndLangExactMatch(t *testing.T) {
|
||||
svc, err := New()
|
||||
if err != nil {
|
||||
t.Fatalf("New() failed: %v", err)
|
||||
}
|
||||
|
||||
svc.AddMessages("en", map[string]string{
|
||||
"prompt.exact": "prompt.exact",
|
||||
"lang.exact": "lang.exact",
|
||||
})
|
||||
|
||||
if got, want := svc.Prompt("exact"), "prompt.exact"; got != want {
|
||||
t.Fatalf("Prompt(exact) = %q, want %q", got, want)
|
||||
}
|
||||
if got, want := svc.Lang("exact"), "lang.exact"; got != want {
|
||||
t.Fatalf("Lang(exact) = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewWithLoaderNormalisesLanguageTags(t *testing.T) {
|
||||
svc, err := NewWithLoader(underscoreLangLoader{})
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue