Class Misago\I18n

Handles translation of strings.

Syntax

Translating a string is just a key/value pairing system. If you want to translate 'this is my name' in french, all you need a YAML file like this:

fr:
  "this is my name": "ceci est mon nom" 

And then to translate it, call Misago\I18n::translate or it's shorcut 't():

$translated_string = t('this is my name');
# => "ceci est mon nom" 

Files

Translations are stored in a YAML file under config/locales/. For instance english translations are in config/locales/en.yml, while french ones will be in config/locales/fr.yml, etc.

You may also separate translations in multiple YAML file, by storing them under config/locales/$locale/, so you may have:

config/locales/en.yml
config/locales/en/posts/index.yml
config/locales/en/posts/show.yml
config/locales/en/posts/new.yml
config/locales/en/posts/edit.yml 

Context

You may specify a particular context to search the translation in. Either as passing the 'context' option, or by prepending the string in the following way: context.string

You may have subcontexts, like: active_record.error.messages

This is reflected in the YAML file as hashes:

en:
  active_record:
    error:
      messages:
        empty: "{{attribute} can't be empty"

t('empty', array('context' => 'active_record.error.messages',
  'attribute' => 'name')) 

Interpolation

Any other option, plus the count option, will be used for interpolating variables in the string.

Example:

t('{{user_name}} sent you a message', array('user_name' => 'James'));
# => James sent you a message 

Pluralization

Depending on your locale, a computing is done to determine which translation must be selected based on the count variable. The algorythm is the same than the one used by gettext.

There must be multiple translations in your YAML file for a single message. In fact as many translations as there are possible results to the above computing. Eventually, you need to pass a count interpolation variable, that will determine which translation to use.

Examples:

en:
  x_minutes:
    0: "a minute"
    1: "{{count}} minutes"
  there_are_x_messages:
    "There is {{count}} message"
    "There are {{count}} messages"

t('x_minutes', array('count' => 1));   # => 'a minute'
t('x_minutes', array('count' => 12));  # => '12 minutes'

t('there_are_x_messages', array('count' => 0));   # => There are 0 messages
t('there_are_x_messages', array('count' => 1));   # => There is 1 message
t('there_are_x_messages', array('count' => 29));  # => There are 29 messages 

Public static attributes

Methods

Public static methods

available_locales()

Returns the list of available locales.

do_translate($str, $options=array())

Same as translate, but returns null if no translation is found.

initialize()

Initializes the I18n translator.

locale($locale=null)

Sets or returns the current locale. On setting, loads associated translation strings.

localize($obj, $options=array())

Localizes certain objects like dates.

Options:

  • format - specify a particular format.

Standard formats:

You may have your own formats by adding translations within the following contexts:

translate($str, $options=null)

Finds the translation for a string. Returns the unstranslated string if no translation is found.