[Rails-core] Localization ideas/proposal

Kyle Maxwell
Mon, 07 Nov 2005 22:55:48 -0000

So far all I''ve seen from the Rails community, with respect to
internationalization or localization have been gettext ports.

This doesn''t help me much, as I have two problems:
1. All of my text is in the database, and I prefer to keep it there.
2. I need different view code for different languages.

Below is the solution I am proposing.  I am willing to do most/all of
the legwork in the near future (next few weeks).  I''m not trying to
sneak this into 1.0 or something; I''m just looking for someone in the
core team to review this and give it his/her blessing, or possibly,
say that its an overworked pile of crap.  I know localization was a
point of empahasis for 1.0, but I haven''t seen any indication that it
ever was finished.

This solution has multiple parts.
0. General
1. Gettext
2. ActionPack changes
3. ActiveRecord changes

-- General --
def ActionController.set_locale(locale_code) ..
def ActionController.get_locale ..
Also, there would be a global variable like $L to retrieve the locale
code from.  The locales would be stored in a AR model called Locale.
It would have a singleton method "current."
AC.get_locale == $L == Locale.current.code

It''s possible that Locale would use a yaml/csv fixture as the data
store, rather than a database table

-- Gettext --
Someone else''s gettext port will be adapted to fit this system, and
work where other methods are not preferred.

-- ActionPack changes --
Modify the render method so that if $L != nil, it will look for
template_name_#{$L}.rhtml, then if not found, template_name.rhtml,
then if not found, error.  It would also look for localized layouts
and partials.

-- ActiveRecord changes --
A model can have a magic "locale" field (similar to created_on), that
will (possibly with a acts_as_localized declaration) set up a Locale
has_many :models (and Model belongs_to :locale) relationship.
Model.find will use Locale.current.models.find, and Model.new will
inject the current locale automatically.

Thanks a lot,
Kyle Maxwell