i18n Internationalization without Rails
I searched the Internat in vain for examples of using the Ruby i18n library outside of Rails apps.
Below is a minimal example: the aim is (as usual) to produce a greeting - this time in English and Italian.
Installation
$ sudo gem install i18n
Translations
Create files with the translations:
en.yml
en: hello world: Hello World!
it.yml
it: hello world: Ciao Mondo!
Program
Create a file in the same directory and call it salutation.rb (or s8n.rb if you prefer):
require 'rubygems' if RUBY_VERSION < '1.9' require 'i18n' I18n.load_path = ['en.yml', 'it.yml'] puts 'In English...' I18n.locale = :en puts I18n.t('hello world') puts 'In Italian...' I18n.locale = :it puts I18n.t('hello world')
Output
$ ruby salutation.rb In English... Hello World! In Italian... Ciao Mondo!
I hope this will show up in someone else's search for the same thing.