Files
Media-download-telegram-bot/internal/i18n/i18n_test.go
T
MichilisandClaude Opus 5.5 f1f9c6156c Phase 7: languages
- Complete Spanish (neutral Latin American, Paraguay friendly, no
  inverted marks) and Dutch catalogs, including the Mini App and owner texts
- Command menus per language in three scopes (private, group members,
  group admins); per-chat menus follow a user's /language and a group's
  /grouplang, from chat and from the Mini App
- Bot name, description and short description per language, sent only
  when the catalog text changes (TELEGRAM_SET_PROFILE)
- Dates from the catalog, numbers, sizes and durations per locale
- Pseudo-locale en-XA generated from English (I18N_PSEUDO), owners only
- Catalog checks: completeness, placeholders, tags, commands, plurals,
  Telegram length limits, untranslated text, unused and missing IDs
- CI workflow; make i18n-check; docs/TRANSLATING.md
- Test fix: the queued-download helper could match another chat's status

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 17:42:31 +00:00

296 lines
8.9 KiB
Go

package i18n_test
import (
"strings"
"testing"
"testing/fstest"
"time"
"github.com/noderunners/dlbot/internal/i18n"
"github.com/noderunners/dlbot/internal/ytdlp"
"github.com/noderunners/dlbot/locales"
)
func load(t *testing.T) *i18n.Bundle {
t.Helper()
b, err := i18n.Load(locales.FS, nil)
if err != nil {
t.Fatal(err)
}
return b
}
func TestEveryErrorCodeHasAMessage(t *testing.T) {
b := load(t)
for _, code := range ytdlp.KnownCodes {
if !b.Has("en", "error."+string(code)) {
t.Errorf("the en catalog has no message for error.%s", code)
}
}
}
func TestPluralsAndArgs(t *testing.T) {
b := load(t)
if got := b.T("en", "unit.minutes", i18n.Args{"Count": 1}); got != "1 minute" {
t.Errorf("one = %q", got)
}
if got := b.T("en", "unit.minutes", i18n.Args{"Count": 5}); got != "5 minutes" {
t.Errorf("other = %q", got)
}
if got := b.T("en", "btn.res", i18n.Args{"Height": 720}); got != "720p" {
t.Errorf("btn.res = %q", got)
}
}
func TestHTMLEscapesArguments(t *testing.T) {
b := load(t)
got := b.HTML("en", "start.private", i18n.Args{"Name": "<script>alert(1)</script>"})
if strings.Contains(got, "<script>") {
t.Fatalf("user text was not escaped: %q", got)
}
if !strings.Contains(got, "<b>") {
t.Fatalf("catalog markup must survive: %q", got)
}
raw := b.HTML("en", "info.line", i18n.Args{"Label": i18n.Raw("<i>x</i>"), "Size": "1 & 2"})
if raw != "<i>x</i>: 1 &amp; 2" {
t.Fatalf("Raw handling = %q", raw)
}
}
func TestMissingKeyReturnsID(t *testing.T) {
b := load(t)
if got := b.T("en", "no.such.key", nil); got != "no.such.key" {
t.Fatalf("missing key = %q", got)
}
}
func TestResolveAndFallbackChain(t *testing.T) {
fsys := fstest.MapFS{
"en.toml": {Data: []byte("hello = \"Hello\"\nbye = \"Bye\"\nonly_en = \"Only English\"\n")},
"es.toml": {Data: []byte("hello = \"Hola\"\nbye = \"Chau\"\n")},
"nl.toml": {Data: []byte("hello = \"Hallo\"\n")},
}
b, err := i18n.Load(fsys, nil)
if err != nil {
t.Fatal(err)
}
resolve := map[string]string{
"": "en", "en": "en", "en-GB": "en", "es": "es", "es-PY": "es", "es-419": "es",
"nl": "nl", "nl-BE": "nl", "de": "en", "zz": "en", "not a tag!": "en",
}
for in, want := range resolve {
if got := b.Resolve(in); got != want {
t.Errorf("Resolve(%q) = %q, want %q", in, got, want)
}
}
// es-PY falls back to es, then to en for keys es does not have.
if got := b.T("es-PY", "bye", nil); got != "Chau" {
t.Errorf("es-PY bye = %q, want the es text", got)
}
if got := b.T("es-PY", "only_en", nil); got != "Only English" {
t.Errorf("es-PY only_en = %q, want the en text", got)
}
if got := b.T("nl", "bye", nil); got != "Bye" {
t.Errorf("nl bye = %q, want the en fallback", got)
}
if b.Has("nl", "bye") || !b.Has("nl", "hello") {
t.Error("Has must not count fallbacks")
}
if langs := b.Langs(); len(langs) != 3 || langs[0] != "en" {
t.Errorf("Langs = %v", langs)
}
}
func TestLoadNeedsEnglish(t *testing.T) {
if _, err := i18n.Load(fstest.MapFS{"es.toml": {Data: []byte("a = \"b\"\n")}}, nil); err == nil {
t.Fatal("loading without en.toml must fail")
}
}
func TestFormatting(t *testing.T) {
b := load(t)
sizes := map[int64]string{
512: "512 B",
2048: "2 KB",
5 << 20: "5.0 MB",
64_900_000: "61.9 MB",
150 << 20: "150 MB",
3 << 29: "1.50 GB",
}
for in, want := range sizes {
if got := b.Size("en", in); got != want {
t.Errorf("Size(%d) = %q, want %q", in, got, want)
}
}
if got := b.Size("es", 64_900_000); got != "61,9 MB" {
t.Errorf("Spanish decimal separator: %q", got)
}
clocks := map[int64]string{0: "0:00", 59: "0:59", 61: "1:01", 3600: "1:00:00", 3723: "1:02:03"}
for in, want := range clocks {
if got := i18n.Clock(in); got != want {
t.Errorf("Clock(%d) = %q, want %q", in, got, want)
}
}
spans := map[time.Duration]string{
0: "1 second", 30 * time.Second: "30 seconds", 60 * time.Second: "1 minute",
10 * time.Minute: "10 minutes", 3 * time.Hour: "3 hours",
}
for in, want := range spans {
if got := b.Span("en", in); got != want {
t.Errorf("Span(%s) = %q, want %q", in, got, want)
}
}
if got := b.Percent("en", 0.426); got != "43%" {
t.Errorf("Percent = %q", got)
}
}
func TestSectionExportsForTheMiniApp(t *testing.T) {
fsys := fstest.MapFS{
"en.toml": {Data: []byte(`
[app]
hello = "Hi {{.Name}}"
only_english = "English only"
[app.tab]
home = "Home"
[app.files]
one = "{{.Count}} file"
other = "{{.Count}} files"
[other]
secret = "not exported"
`)},
"es.toml": {Data: []byte(`
[app]
hello = "Hola {{.Name}}"
[app.tab]
home = "Inicio"
[app.files]
one = "{{.Count}} archivo"
other = "{{.Count}} archivos"
`)},
}
b, err := i18n.Load(fsys, nil)
if err != nil {
t.Fatal(err)
}
got := b.Section("es-PY", "app")
if got["hello"] != "Hola {Name}" || got["tab.home"] != "Inicio" {
t.Fatalf("section = %v", got)
}
// A message missing in the language comes from English, like in chat.
if got["only_english"] != "English only" {
t.Fatalf("no fallback to English: %v", got["only_english"])
}
forms, ok := got["files"].(map[string]string)
if !ok || forms["one"] != "{Count} archivo" || forms["other"] != "{Count} archivos" {
t.Fatalf("plural forms = %v", got["files"])
}
if _, leaked := got["secret"]; leaked || len(b.Section("es", "missing")) != 0 {
t.Fatal("a section must hold its own table only")
}
}
func TestDir(t *testing.T) {
for lang, want := range map[string]string{
"en": "ltr", "es-PY": "ltr", "nl": "ltr", "en-XA": "ltr",
"ar": "rtl", "ar-EG": "rtl", "he": "rtl", "fa-IR": "rtl", "ur": "rtl", "": "ltr",
} {
if got := i18n.Dir(lang); got != want {
t.Errorf("Dir(%q) = %q, want %q", lang, got, want)
}
}
}
func TestPseudoLocale(t *testing.T) {
b, err := i18n.Load(locales.FS, nil, i18n.WithPseudo())
if err != nil {
t.Fatal(err)
}
if !b.Hidden(i18n.Pseudo) {
t.Fatal("the pseudo-locale must be hidden from users")
}
found := false
for _, l := range b.Langs() {
found = found || l == i18n.Pseudo
}
if !found {
t.Fatalf("languages = %v, want %s among them", b.Langs(), i18n.Pseudo)
}
got := b.HTML(i18n.Pseudo, "start.private", i18n.Args{"Name": "Ana"})
if !strings.Contains(got, "<b>") || !strings.Contains(got, "Ana") || !strings.Contains(got, "/help") ||
strings.Contains(got, "welcome") || !strings.Contains(got, "[") {
t.Fatalf("pseudo start = %q", got)
}
// Plurals keep working.
if one, many := b.T(i18n.Pseudo, "unit.sats", i18n.Args{"Count": 1, "N": "1"}), b.T(i18n.Pseudo, "unit.sats", i18n.Args{"Count": 5, "N": "5"}); one == many || !strings.Contains(many, "5") {
t.Fatalf("pseudo plurals = %q, %q", one, many)
}
if d := b.DateTime(i18n.Pseudo, time.Date(2026, 3, 4, 5, 6, 0, 0, time.UTC)); !strings.Contains(d, "Ṁáŕ") || !strings.Contains(d, "05:06") {
t.Fatalf("pseudo date = %q", d)
}
// The Mini App gets it too.
if s, _ := b.Section(i18n.Pseudo, "app")["tab.home"].(string); !strings.HasPrefix(s, "[Ĥöḿé") {
t.Fatalf("pseudo app text = %q", s)
}
// Without the option it does not exist.
plain := load(t)
for _, l := range plain.Langs() {
if l == i18n.Pseudo {
t.Fatal("the pseudo-locale is loaded without being asked for")
}
}
}
func TestPseudolocalizeKeepsMarkup(t *testing.T) {
for in, want := range map[string]string{
"Hi {{.Name}}": "[Ĥî {{.Name}} ~]",
"<b>Paid</b> &amp; done": "[<b>Þáîð</b> &amp; ðöñé ~~~]",
"Try /help or https://x.y/a?b=c": "[Ţŕý /help öŕ https://x.y/a?b=c ~~]",
"\nTwo\nlines\n": "\n[Ţŵö\nļîñéš ~~~]\n",
} {
if got := i18n.Pseudolocalize(in); got != want {
t.Errorf("Pseudolocalize(%q) = %q, want %q", in, got, want)
}
}
}
func TestDatesFollowTheCatalog(t *testing.T) {
b := load(t)
at := time.Date(2026, 11, 18, 9, 5, 0, 0, time.FixedZone("PYT", -3*3600))
if got := b.DateTime("en", at); got != "18 Nov 2026, 12:05 UTC" {
t.Errorf("en = %q", got)
}
if got := b.Date("en", at); got != "18 Nov 2026" {
t.Errorf("en date = %q", got)
}
for lang, want := range map[string]string{"es": "18 nov 2026, 12:05 UTC", "es-PY": "18 nov 2026, 12:05 UTC", "nl": "18 nov 2026, 12:05 UTC"} {
if got := b.DateTime(lang, at); got != want {
t.Errorf("%s = %q, want %q", lang, got, want)
}
}
if got := b.DateTime("nl", time.Date(2026, 3, 1, 0, 0, 0, 0, time.UTC)); got != "1 mrt 2026, 00:00 UTC" {
t.Errorf("nl March = %q", got)
}
}
func TestNumbersAndUnitsPerLanguage(t *testing.T) {
b := load(t)
cases := []struct{ lang, got, want string }{
{"en", b.Sats("en", 10000), "10,000 sats"},
{"es", b.Sats("es", 10000), "10.000 sats"},
{"nl", b.Sats("nl", 10000), "10.000 sats"},
{"es", b.Sats("es", 1), "1 sat"},
{"es", b.Size("es", 5<<20), "5,0 MB"},
{"nl", b.Size("nl", 3<<29), "1,50 GB"},
{"es", b.Span("es", 5*time.Minute), "5 minutos"},
{"nl", b.Span("nl", time.Minute), "1 minuut"},
{"nl", b.Span("nl", 3*time.Hour), "3 uur"},
{"es-PY", b.T("es-PY", "status.done", nil), "Listo. Que lo disfrutes."},
}
for _, c := range cases {
if c.got != c.want {
t.Errorf("%s: %q, want %q", c.lang, c.got, c.want)
}
}
}