...

/

Finalize Building Our Internationalization Library

Finalize Building Our Internationalization Library

Learn about Macro.to_string and finalize building the Internalization Library

Macro.to_string: Making sense of our generated code

Macro.to_string takes an AST and produces a string of the high-level Elixir source. It’s incredibly helpful when debugging our generated ASTs, especially for cases where many function heads are generated, such as in our Translator module.

The project

Let’s inspect the generated code that compile has produced so far by adding the following changes to our Translator module:

defmodule I18n do
  use Translator

  locale "en",
    flash: [
      hello: "Hello %{first} %{last}!",
      bye: "Bye, %{name}!"
    ],
    users: [
      title: "Users",
    ]

  locale "fr",
    flash: [
      hello: "Salut %{first} %{last}!",
      bye: "Au revoir, %{name}!"
    ],
    users: [
      title: "Utilisateurs",
    ]
end
The `Translator` module with implemented functions and macros.

On line 6, we stored the result of the generated AST in a final_ast binding. Then on line 12, we printed the entire AST expanded as Elixir source using Macro.to_string. To finish, we ...