Forum: Ruby on Rails Making my site multi-lingual

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-05-11 22:08
(Received via mailing list)
I'm wondering how I should go about making my website multi-lingual.
Basically, there will be different content for the different
languages, and that's not really a problem... all I need to do is give
each content entry in the database a language_id. But the problem is
with the non-content text, eg, "In order to access this area, you must
be logged in", "Created by", "Last updated" etc. Surely rails has a
way of making this simple. I just can't find it. Anyone know how?
Davis Cabral - Listas (Guest)
on 2006-05-11 22:15
(Received via mailing list)
removed_email_address@domain.invalid wrote:
> removed_email_address@domain.invalid

= Localization Plugin for Rails

This plugin provides a simple, gettext-like method to
provide localizations.

== Features

 * Any number of languages or locales
 * Simple method to defines singluar/plural translations
 * Can use lambdas to provide Ruby-code based dynamic translations
 * Customizable for different instances of the application

== Usage

If the localization plugin is installed, it is used automatically.

You need to create a /lang dir in your RAILS_ROOT.

The recommended way to use it is to create files that are named
like the languages you define in them (but you can put everything in
one big file too.)

For instance-customizable strings, add overrides in files you
put in /lang/custom.

=== Simple example:

Create a file /lang/translations.rb:

  Localization.define('de') do |l| 'yes', 'Ja' 'no', 'Nein'

  Localization.define('fr') do |l| 'yes', 'oui' 'no', 'non'

In your controller or application.rb:

  Localization.lang = 'de' # or 'fr'

In your view:

  <%=_ 'yes' %>
  <%=_ 'no' %>

Because the _ method is simply an extension to Object, you
can use it anywhere (models/controllers/views/libs).

=== Extended example:

Create a file /lang/default.rb with following contents:

  Localization.define do |l| '(time)', lambda { |t| t.strftime('%I:%M%p') }

Create a file /lang/de_DE.rb with following contents:

  Localization.define('de_DE') do |l| '%d entries', ['Ein Eintrag', '%d Einträge'] '(time)', lambda { |t| t.strftime('%H:%M') }

In your controller or application.rb:

  Localization.lang = 'de_DE'

In your view:

  <%=_ '%d entries', 1 %>     # singular variant is chosen
  <%=_ '%d entries', 4 %>     # plural variant is chosen
  <%=_ '(time)', %>  # call the block with a parameter

== Translation file guesstimation

You can generate a guesstimation of all strings needed to be
translated in your views by first adding the _('blah') syntax
everywhere and then calling:

  puts Localization.generate_l10n_file

in the Rails console.

Davis Zanetti Cabral
Gtalk/Msn: daviscabral at gmail dot com
Impact Media Estúdio de Criações
Top x Bottom      ::
Responder e-mails ::
Masao M. (Guest)
on 2006-05-11 22:28
(Received via mailing list)

On Thu, 11 May 2006 19:07:59 +0100
removed_email_address@domain.invalid wrote:

> removed_email_address@domain.invalid

How about Ruby-GetText-Package?
This topic is locked and can not be replied to.