[agent/codex:gpt-5.4-mini] Read ~/spec/code/core/go/i18n/RFC.md fully. Find ONE feature... #130
4 changed files with 42 additions and 2 deletions
2
i18n.go
2
i18n.go
|
|
@ -21,7 +21,7 @@ func Translate(messageID string, args ...any) core.Result {
|
|||
if svc := Default(); svc != nil {
|
||||
return svc.Translate(messageID, args...)
|
||||
}
|
||||
return core.Result{Value: messageID, OK: true}
|
||||
return core.Result{Value: messageID, OK: false}
|
||||
}
|
||||
|
||||
// Raw translates without i18n.* namespace magic.
|
||||
|
|
|
|||
11
i18n_test.go
11
i18n_test.go
|
|
@ -52,6 +52,17 @@ func TestTranslate_Good(t *testing.T) {
|
|||
assert.Equal(t, "y", result.Value)
|
||||
}
|
||||
|
||||
func TestTranslate_Good_MissingKey(t *testing.T) {
|
||||
svc, err := New()
|
||||
require.NoError(t, err)
|
||||
_ = Init()
|
||||
SetDefault(svc)
|
||||
|
||||
result := Translate("nonexistent.translation.key")
|
||||
require.False(t, result.OK)
|
||||
assert.Equal(t, "nonexistent.translation.key", result.Value)
|
||||
}
|
||||
|
||||
// --- Package-level Raw() ---
|
||||
|
||||
func TestRaw_Good(t *testing.T) {
|
||||
|
|
|
|||
16
service.go
16
service.go
|
|
@ -378,7 +378,8 @@ func (s *Service) T(messageID string, args ...any) string {
|
|||
|
||||
// Translate translates a message by its ID and returns a Core result.
|
||||
func (s *Service) Translate(messageID string, args ...any) core.Result {
|
||||
return core.Result{Value: s.T(messageID, args...), OK: true}
|
||||
value := s.T(messageID, args...)
|
||||
return core.Result{Value: value, OK: translateOK(messageID, value)}
|
||||
}
|
||||
|
||||
// resolveDirect performs exact-key lookup in the current language, its base
|
||||
|
|
@ -829,6 +830,19 @@ func (s *Service) AddLoader(loader Loader) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func translateOK(messageID, value string) bool {
|
||||
if value == "" {
|
||||
return false
|
||||
}
|
||||
if value == messageID {
|
||||
return false
|
||||
}
|
||||
if value == "["+messageID+"]" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// LoadFS loads additional locale files from a filesystem.
|
||||
// Deprecated: Use AddLoader(NewFSLoader(fsys, dir)) instead for proper grammar handling.
|
||||
func (s *Service) LoadFS(fsys fs.FS, dir string) error {
|
||||
|
|
|
|||
|
|
@ -94,6 +94,21 @@ func TestServiceTranslate(t *testing.T) {
|
|||
var _ core.Translator = (*Service)(nil)
|
||||
}
|
||||
|
||||
func TestServiceTranslateMissingKey(t *testing.T) {
|
||||
svc, err := New()
|
||||
if err != nil {
|
||||
t.Fatalf("New() failed: %v", err)
|
||||
}
|
||||
|
||||
result := svc.Translate("missing.translation.key")
|
||||
if result.OK {
|
||||
t.Fatalf("Translate(missing.translation.key) returned OK, want false: %#v", result)
|
||||
}
|
||||
if got, want := result.Value, "missing.translation.key"; got != want {
|
||||
t.Fatalf("Translate(missing.translation.key) = %#v, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceTDirectKeys(t *testing.T) {
|
||||
svc, err := New()
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue