View Source Strings File and I18n

It's a good idea to extract any human-readable strings in your application out into a configuration file. The reason is two-fold:

  1. It makes it easier for developers to update "copy" in the application and even allows non-developers on a team to make copy changes.
  2. When your application supports multiple languages, it is easy for translators to provide translations for all of your copy at once.

In Legendary, we provide a set of tools for doing this via linguist.

  • (English) strings are stored in config/i18n/en.yml.
  • You can call Legendary.I18n.t!/2 to get a string by its key. For example: Legendary.I18n.t! "en", "site.title" retrieves the english version of the string labeled "title" under the section "site" on en.yml.

Tip: if you use t! a lot (good job!), you can import it in your view module to save some typing like import Legendary.I18n, only: [t!: 2] and then use it like <%= t! "en", "site.title" %> in your templates.

Note that the first argument is a two-letter language code. In order to support other languages, you can provide more yml files in config/i18n (example, config/i18n/fr.yml for French) and call t!/2 with that language code instead.

Linguist also supports templated translations. If you have a section in en.yml like this:

  hello_message: Hello, %{name}!

then you could call t! substitutions like this:

t! "en", "app.hello_message", name: "Legend"

to get the string "Hello, legend!"

On the roadmap: in the future, we intend to provide a mechanism for detecting and managing each visitor's language and providing those strings if available.