megas
megas

Reputation: 21791

Rails localization syntax

I've installed Twitter Bootstrap into my project and found unknown syntax for me:

<%=t '.title', :default => model_class.model_name.human.pluralize %>
<%= link_to t('.new', :default => t("helpers.links.new")),
            new_article_path,
            :class => 'btn btn-primary' %>

I can't understand the meaning of '.title', '.new' and 'helpers.links.new'. How do these constructions interact with the locale dictionary?

Also I've never met the construction :default => in t method, where I can read about it?

Upvotes: 1

Views: 784

Answers (1)

Erez Rabih
Erez Rabih

Reputation: 15788

t is a helper method supplied by I18n internationalization mechanism of rails, and is a shortcut for I18n.translate method.

The locale file which I18n reads from is set by default to Rails.root/config/locales/en.yml assuming en is your default locale.

The first argument is the key which I18n will look for in your locale file. The statement t('.new', :default => t("helpers.links.new")) means that I18n will look for the construct

en:
  new: "new string"

in your locale file.

:default is the string which will be returned in case the first key was not found.

:default => t("helpers.links.new") just means that I18n will look for the following construct in en.yml:

en:
  helpers:
    links:
      new: "new string"

and return it in case the first one was absent.

You can find here the full documentation of I18n translate method.

Upvotes: 5

Related Questions