Locale-aware formatting for the Lua (Luerl) VM, backed by Localize.
Localize.Lua installs a small, sandbox-safe localize table into a Luerl VM
so that Lua templates — such as those rendered by ash_cms — can format
numbers, currencies, dates, times, units, lists and MessageFormat 2 messages
with full CLDR locale awareness. Because Luerl runs entirely on the BEAM, each
localize.* call is an ordinary Elixir function call into Localize; nothing
crosses a native boundary and no unsafe capability is exposed.
The public surface is deliberately tiny:
Every installed function returns a string and falls back gracefully on bad input, so an untrusted template can never raise on the host render path.
Locale data
Only the en locale ships with Localize; other locales are downloaded once
with mix localize.download_locales de fr ja. A localize.number(1, {locale = "de"}) call for a locale that has not been installed formats with the root
locale rather than raising.
Examples
iex> {[money], _lua} =
...> Lua.new()
...> |> Localize.Lua.install()
...> |> Lua.eval!(~S[return localize.currency(1234.56, "USD")])
iex> money
"$1,234.56"
Summary
Functions
Installs the localize API and evaluates a Lua script.
Installs the localize API and evaluates a Lua script, returning results or raising.
Installs the localize API table into a Lua VM.
Functions
@spec eval(String.t(), Lua.t()) :: {:ok, [term()]} | {:error, Exception.t()}
Installs the localize API and evaluates a Lua script.
Arguments
scriptis the Lua source to evaluate.luais an optionalLua.t/0to install into; a fresh sandboxed VM is used when omitted.
Returns
{:ok, results}whereresultsis the list of decoded Lua return values.{:error, exception}if the script raises inside Lua.
Examples
iex> Localize.Lua.eval(~S[return localize.percent(0.56)])
{:ok, ["56%"]}
Installs the localize API and evaluates a Lua script, returning results or raising.
Arguments
scriptis the Lua source to evaluate.luais an optionalLua.t/0to install into; a fresh sandboxed VM is used when omitted.
Returns
- The list of decoded Lua return values, or raises
Lua.RuntimeExceptionon a Lua error.
Examples
iex> Localize.Lua.eval!(~S[return localize.unit(42, "kilometer", {format = "short"})])
["42 km"]
Installs the localize API table into a Lua VM.
Arguments
Returns
- The
Lua.t/0with alocalizeglobal table whose functions are listed inLocalize.Lua.API.
Examples
iex> lua = Localize.Lua.install(Lua.new())
iex> {[list], _lua} = Lua.eval!(lua, ~S[return localize.list({"a", "b", "c"})])
iex> list
"a, b, and c"