mattermost-plugin-shelfmark/server/i18n_test.go
Felipe M. e05ab1bc96
Add i18n support for user-facing messages and rewrite README
Replace hardcoded English strings throughout the plugin with localized
messages via a new i18n system supporting English and Spanish. Store the
requester's locale on the download task so background job messages are
properly localized. Replace the starter template README with
project-specific documentation covering features, usage, configuration,
and development.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 17:55:33 +01:00

98 lines
2.3 KiB
Go

package main
import (
"strings"
"testing"
)
func TestAllKeysExistInBothLocales(t *testing.T) {
enMessages := messages["en"]
esMessages := messages["es"]
for key := range enMessages {
if _, ok := esMessages[key]; !ok {
t.Errorf("key %q exists in 'en' but not in 'es'", key)
}
}
for key := range esMessages {
if _, ok := enMessages[key]; !ok {
t.Errorf("key %q exists in 'es' but not in 'en'", key)
}
}
}
func TestFormatStringParity(t *testing.T) {
enMessages := messages["en"]
esMessages := messages["es"]
for key, enFmt := range enMessages {
esFmt, ok := esMessages[key]
if !ok {
continue
}
enCount := strings.Count(enFmt, "%s") + strings.Count(enFmt, "%d")
esCount := strings.Count(esFmt, "%s") + strings.Count(esFmt, "%d")
if enCount != esCount {
t.Errorf("key %q: 'en' has %d format verbs, 'es' has %d", key, enCount, esCount)
}
}
}
func TestFallbackToEnglish(t *testing.T) {
result := T("fr", MsgCommandEmpty)
expected := messages["en"][MsgCommandEmpty]
if result != expected {
t.Errorf("expected fallback to English %q, got %q", expected, result)
}
}
func TestLocaleNormalization(t *testing.T) {
tests := []struct {
locale string
expected string
}{
{"es-ES", "es"},
{"es-MX", "es"},
{"EN-US", "en"},
{"en", "en"},
{"", "en"},
}
for _, tt := range tests {
got := normalizeLocale(tt.locale)
if got != tt.expected {
t.Errorf("normalizeLocale(%q) = %q, want %q", tt.locale, got, tt.expected)
}
}
}
func TestTWithArgs(t *testing.T) {
result := T("en", MsgNoBooksFound, "test query")
expected := `No books found for "test query".`
if result != expected {
t.Errorf("expected %q, got %q", expected, result)
}
}
func TestTWithSpanish(t *testing.T) {
result := T("es", MsgNoBooksFound, "test query")
expected := `No se encontraron libros para "test query".`
if result != expected {
t.Errorf("expected %q, got %q", expected, result)
}
}
func TestTUnknownKeyReturnsKey(t *testing.T) {
result := T("en", "nonexistent.key")
if result != "nonexistent.key" {
t.Errorf("expected key returned as-is, got %q", result)
}
}
func TestTNoArgs(t *testing.T) {
result := T("en", MsgCommandEmpty)
expected := "Empty command received."
if result != expected {
t.Errorf("expected %q, got %q", expected, result)
}
}