Localize
Localize is a small library for internationalization. Example:
puts t.hello # => Hola
puts t.hello.world # => Hola, mundo
puts t.hello('world', 'space') # => Hello world and space
puts f 380441234567 # => +380 (44) 123-45-67
puts l Time.utc(2000, "jan"), :short # => Sat 01-Jan-00
puts l 1200.05 # => 1.200,05
Install
gem install localize
require 'localize'
Setup
Set the store, yaml or plain for now, xml and others planned (default yaml):
Localize.store = :plain
Specify location of the translation files:
Localize.location = 'lib/translations'
if store set to plain, location must be a ruby hash:
Localize.location = {'text' => { 'hello' => {'world' => 'mundo' } }
Set locale:
Localize.locale = :esp
and default locale (optional, en by default):
Localize.default_locale = :ru
Now, load the translation:
t = Localize.translate
f = Localize.f
l = Localize.l
Sinatra setup
sinatra.rb contains predefined helpers t
, l
and f
and set locale from session['locale']
require 'localize/sinatra'
Localize.default_locale = :ru
Localize.location = 'locales' # Change to your path
Translation files
Translation file must contain two sections: ‘text’ and ‘formats’.
Text section contains translations and can have an unlimited nesting:
text:
hello: 'hola' # t.hello
foo:
bar: 'baz' # t.foo.bar
You may interpolate you translations:
text:
hello: 'Hello ${1} and ${2}!'
foo: 'Foo ${2}, ${1} or ${1}, ${3}'
…in translation, and…
t.hello('world', 'space')
t.foo('bar', 'baz', 'fee')
…in view produce ‘Hello world and space!’ and ‘Foo baz, bar or bar, fee’
Formats section contains different localization rules and formats:
formats:
phone:
:full: '+### (##) ###-##-##'
:short: '###-##-##'
number:
separator: ','
dec_point: '.'
Phone subsection must have format named ‘full’, and may have arbitrary names which can called by second parameter in f method:
f(78977654) # Full format
f(12345678, :short)
f(32145687, :other)
Date subsection must contains translations for months and days of the week. Also set formats in strftime format:
formats:
date:
format:
:full: '%a %b %d %H:%M:%S %Z %Y'
:short: '%a %d-%B-%y'
day_names:
short:
- Sun
- Mon
- Tue
- Wed
- Thu
- Fri
- Sat
full:
- Sunday
- Monday
- Tuesday
- Wednesday
- Thursday
- Friday
- Saturday
mon_names:
short:
- Jan
- Feb
- Mar
- Apr
- May
- Jun
- Jul
- Aug
- Sep
- Oct
- Nov
- Dec
full:
- January
- February
- March
- April
- May
- June
- July
- August
- September
- October
- November
- December