I searched the Internet in vain for examples of using the Ruby i18n gem 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
$ gem install i18n
The t
method
The most common use of i18n is via I18n.translate
which has an abbreviated alias I18n.t
.
Translations
1 2 |
|
1 2 |
|
These translations can be accessed as follows: I18n.t( ‘hello world’ )
That will use the current locale setting. You can change it as follows: I18n.locale = :it
The Program
After loading the i18n
gem, set up I18n.load_path
before attempting to retrieve any transaltions.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The result:
$ ruby salutation.rb
In English...
Hello World!
In Italian...
Ciao Mondo!