Rails Localization

Rails SDK offers methods and helper functions for Date and Time localization.

Date and Time Localization Extensions


Date tr

TML library provides a translation method for Date and Time objects that can be used to localize dates using TML syntax.

date.tr(format, options)  

Example:

<%= Date.today.tr %>  
<%= Date.today.tr(:short_numeric) %>  
<%= Date.today.tr(:date_time) %>  
<%= Date.today.tr('%B %d, %Y') %>  

Parameters:

Name Type Default Description
format Symbol / String :default An optional format for the date
format Hash Additional options for the translation, like :description and :with_leading_zero.

When the tr method is called, it converts the standard Date format to a more readable TML version that translators can change. For instance:

<%= Date.today.tr('%B %d, %Y') %>

# March 17, 2016

The function will map the standard notation to the following TML string:

{month_name} {days}, {years}

A Russian translator can then provide the following translation for the format:

{days} {month_name::gen}, {years}

Where the {days} token is followed by the month name in a Genitive language case, followed by the {years}.

The translated format will then be rendered as:

17 Марта, 2016

The library comes with a few presets for formats:

{
  :default => '%m/%d/%Y', # 07/4/2008
  :short_numeric => '%m/%d', # 07/4
  :short_numeric_year => '%m/%d/%y', # 07/4/08
  :long_numeric => '%m/%d/%Y', # 07/4/2008
  :verbose => '%A, %B %d, %Y', # Friday, July  4, 2008
  :monthname => '%B %d', # July 4
  :monthname_year => '%B %d, %Y',# July 4, 2008
  :monthname_abbr => '%b %d', # Jul 4
  :monthname_abbr_year => '%b %d, %Y', # Jul 4, 2008
  :date_time => '%m/%d/%Y at %H:%M',  # 01/03/1010 at 5:30
}

And more custom presets can easily be added in the initialization:

Tml.configure do |config|  
  config.localization[:custom_date_formats][:special] = '%Y-%M-%d'
end  

Now any time you use :special symbol as the format parameter, it will use the special format:

<%= Date.today.tr(:special) %>

# 2016-03-17

The following is the standard notation to token mapping:

{
  '%a' => '{short_week_day_name}',
  '%A' => '{week_day_name}',
  '%b' => '{short_month_name}',
  '%B' => '{month_name}',
  '%p' => '{am_pm}',
  '%d' => '{days}',
  '%e' => '{day_of_month}',
  '%j' => '{year_days}',
  '%m' => '{months}',
  '%W' => '{week_num}',
  '%w' => '{week_days}',
  '%y' => '{short_years}',
  '%Y' => '{years}',
  '%l' => '{trimed_hour}',
  '%H' => '{full_hours}',
  '%I' => '{short_hours}',
  '%M' => '{minutes}',
  '%S' => '{seconds}',
  '%s' => '{since_epoch}'
}


Date trl

This method is just like the Date tr method, but it disables the inline translation mode.

<%= Date.today.trl(:default) %>

# March 17, 2016


Time tr

This method is similar to the Date tr method and provides additional elements for time mapping.

<%= Time.now.tr(:date_time) %>

# 01/03/1010 at 5:30


Time trl

This method is just like the Time tr method, but it disables the inline translation mode.

<%= Time.now.trl(:date_time) %>

# 01/03/1010 at 5:30