On 2013-05-21 21:56, Nick Sabalausky wrote:

Speaking of, what is the current state-of-the-art for i18n? Basically
just tables of format strings looked up by language and a phrase
identifier?

Ruby on Rails uses the I18n library and works like this:

In a view you can call the "translate" method:

translate("nested.key")

This will look in the YAML file <locale>.yml:

nested:
  key: bar

So the above will print "bar". The locale is set for each request in the controller based on top domain, path or whatever you want:

I18n.locale = "en"

It also supports interpolation:

translate("nested.key", bar: 3) # the last argument is a hash map

en:
  nested:
    key: foo=%{bar}

Will print: "foo=3". On top of this Rails builds some convenience:

translate(".price", price: product.price)

When the key starts with a dot it will use the path of the view file, including partials. You many have a YAML file looking like this:

en:
  products:
    show:
      details:
        price: "Price: %{price}"

"products" would be a directory, "show" the view file, "details" a partial included in "show" and "price" would be the key.

There's also the "localize" method:

localize(product.updated_at, format: "date")

Will match the following YAML file:

en:
  time:
    formats:
      date: "%Y-%m-%d"

That will print just the date from the given time stamp.

I18n supports any kind of backend that has the following interface:

backend["nested"]["key"]

A standard translation file for English:

https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml

Documentation:

http://guides.rubyonrails.org/i18n.html

--
/Jacob Carlborg

Reply via email to