Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Gettext for Elixir - getting serious at compile time

Gettext for Elixir - getting serious at compile time

Andrea Leopardi

October 03, 2015
Tweet

More Decks by Andrea Leopardi

Other Decks in Programming

Transcript

  1. defmodule Gettext do @translations Gettext.Compiler.po_files() for {msgid, msgstr} <- @translations

    do def gettext_fn(unquote(msgid)) do unquote(msgstr) end end end
  2. defmodule Gettext do @translations Gettext.Compiler.po_files() for {msgid, msgstr} <- @translations

    do def gettext_fn(unquote(msgid)) do unquote(msgstr) end end end
  3. defmodule Gettext do @translations Gettext.Compiler.po_files() for {msgid, msgstr} <- @translations

    do def gettext_fn(unquote(msgid)) do unquote(msgstr) end end end
  4. defmodule Gettext do @translations Gettext.Compiler.po_files() for {msgid, msgstr} <- @translations

    do def gettext_fn(unquote(msgid)) do unquote(msgstr) end end end
  5. defmodule Gettext do @translations Gettext.Compiler.po_files() for {msgid, msgstr} <- @translations

    do def gettext_fn(unquote(msgid)) do unquote(msgstr) end end end
  6. defmodule Unicode do @pairs Unicode.read("data.txt") for {lower, upper} <- @pairs

    do def lower(unquote(upper)) do unquote(lower) end end end
  7. gettext "Hello %{name}!", %{name: "Frodo"} case %{name: "Frodo"} do %{name:

    name} -> {:ok, "Hello " <> name} _ -> {:error, :bad_interp} end
  8. gettext "Hello %{name}!", %{name: "Frodo"} case %{name: "Frodo"} do %{name:

    name} -> {:ok, "Hello " <> name} _ -> {:error, :bad_interp} end
  9. import MyApp.Gettext ngettext "One attendant", "%{count} attendants", 254 msgid "One

    attendant" msgid_plural "%{count} attendants" msgstr[0] "Uno spettatore" msgstr[1] "%{count} spettatori"
  10. # Arabic def plural("ar", 0), do: 0 def plural("ar", 1),

    do: 1 def plural("ar", 2), do: 2 def plural("ar", n) when rem(n, 100) >= 3 and ... def plural("ar", n) when rem(n, 100) >= 11, ... def plural("ar", _n), do: 5
  11. so.