[Rails-core] Localization ideas/proposal

jmharvey.19309139@bloglines.com
Tue, 08 Nov 2005 16:29:52 -0000

I''m about to release a plugin with Josh Sierles that addresses a lot of
these
issues. Here''s what we did.

To translate certain fields of a model (the
''model level'' ActiveRecord translations):

1. Don''t include those fields
in the model table.
2. Use the "translates" class method:

class Product
< ActiveRecord::Base
  translates :name, :description, :specs
end

3.
Do Local.set_locale("en_US").

4. Now you can do product.name, and it will
return the name in the language currently set. Almost all AR functionality
works as expected: assignment, new, create, associations.

Behind the scenes,
the plugin uses a table called "translations", and does outer joins to
pull
the
translated fields into the model.

The rest of the plugin is based
on the Multilingual Rails package from Per Wigren. Here is a rundown of its
features:

- String translation using _("str"), str.t, :str.t, :str %
[variable_for_interpolation]


- Translated templates and partials: app/views/shop/ice_creams.sv.rhtml
loads before ice_creams.rhtml

- Overrides for common local-related methods:
distance_of_time_in_words, ActiveRecord errors, Date constants

- Logging
of untranslated strings in logs/translation-misses

- Pluralization in various
languages: :some_books % [book_count]

- Charset conversion with String
extensions:  "This string is now ISO
8859-1".iconv_to(''iso-8859-1'')

- Pluggable
translators, default being the ruby file translations

- ISO-based lists
of locale codes, country names and languages, used by a new country_select
helper

- Optional unicode string normalization for upcase, downcase, etc.


Example default translation:

  s "I want ice cream!" do
    t :sv,
"Jag vill ha glass!"
    t :no, "Jeg vil ha is!"
  end

Since Per seems
to be out for the count, we thought it would be nice to extend his package.
MLR offers a Ruby way of doing things, and opts for clean code. It stores
all of its translations in Ruby files.  Details about this package here
http://www.tuxsoft.se/sv/oss/rails/multilingual.
It no longer requires the unicode gem, but will use unicode normalization
routines should the gem be installed.

Lingering doubts:

1. Should the
plugin ennforce utf-8 encoding for storage as MLR does currently?

2. Should
controllers and views be offered either in another package or as an
''engine''
for editing translations?