-- lx-bundle:true

local loc = require("loc")

-- ============================================================
-- Example usage
-- ============================================================

-- Locale dictionaries (note: multiline uses dedent([[ ... ]]) to keep files readable)
loc.add_locale("en", {
	["welcome.user"] = "{s:gender male{Welcome, sir} female{Welcome, ma'am} other{Welcome}}",
	["inbox.summary"] = "You have {p:n =0{No messages} one{# message} other{# messages}}",
	["auto:Hello {name}!"] = "Hello {name}!",

	["email.body"] = loc.dedent([[
    Hello {name},

    You have {p:n =0{no new messages} one{# new message} other{# new messages}}.

    Regards,
    Support
  ]]),
})

loc.add_locale("de", {
	["welcome.user"] = "{s:gender male{Willkommen, mein Herr} female{Willkommen, gnädige Frau} other{Willkommen}}",
	["inbox.summary"] = "Du hast {p:n =0{keine Nachrichten} one{# Nachricht} other{# Nachrichten}}",
	["auto:Hello {name}!"] = "Hallo {name}!",

	["email.body"] = loc.dedent([[
        Hallo {name},

        Du hast {p:n =0{keine neuen Nachrichten} one{# neue Nachricht} other{# neue Nachrichten}}.

        Viele Grüße
        Support
  ]]),
})

-- Select locale
loc.set_locale("de")

loc.precompile_all()


-- Optional warmup (compile everything in German upfront)
-- loc.precompile_locale("de")

-- 1) id lookup + select
print(loc.text("welcome.user", "Welcome", { vars = { gender = "female" } }))
-- -> Willkommen, gnädige Frau

-- 2) id lookup + plural with exact match and #
print(
	loc.text("inbox.summary", "You have {p:n =0{No messages} one{# message} other{# messages}}", { vars = { n = 0 } })
)
print(loc.text("inbox.summary", "You have ...", { vars = { n = 1 } }))
print(loc.text("inbox.summary", "You have ...", { vars = { n = 5 } }))
-- -> Du hast keine Nachrichten
-- -> Du hast 1 Nachricht
-- -> Du hast 5 Nachrichten

-- 3) auto-id form + vars
print(loc.text("Hello {name}!", { vars = { name = "Tom" } }))
-- -> Hallo Tom!

-- 4) multiline string (contains real newlines, preserved)
print(loc.text("email.body", "Hello", { vars = { name = "Tom", n = 2 } }))

-- 5) direct formatting without translations (still cached compilation)
print(loc.render("Raw: {s:mode a{A} b{B} other{?}} / {p:n =0{Z} one{O} other{M}}\nNext line.", { mode = "b", n = 2 }))
