package i18n

import "testing"

func TestFallback(t *testing.T) {
	tr := New("ms")
	tr.Load("ms", map[string]string{"hello": "Hai"})
	tr.Load("en", map[string]string{"hello": "Hello"})

	if got := tr.T("en", "hello"); got != "Hello" {
		t.Fatalf("en hello = %q", got)
	}
	if got := tr.T("ms", "hello"); got != "Hai" {
		t.Fatalf("ms hello = %q", got)
	}
	if got := tr.T("en", "missing"); got != "missing" {
		t.Fatalf("missing key should echo key, got %q", got)
	}
	if got := tr.T("fr", "hello"); got != "Hai" {
		t.Fatalf("unknown lang should fall back to default, got %q", got)
	}
}

func TestLoadJSON(t *testing.T) {
	tr := New("ms")
	if err := tr.LoadJSON("en", []byte(`{"greeting":"Welcome"}`)); err != nil {
		t.Fatal(err)
	}
	if got := tr.T("en", "greeting"); got != "Welcome" {
		t.Fatalf("got %q", got)
	}
}

func TestTf(t *testing.T) {
	tr := New("ms")
	tr.Load("en", map[string]string{"books": "%d books"})
	if got := tr.Tf("en", "books", 5); got != "5 books" {
		t.Fatalf("got %q", got)
	}
}
