R18n
R18n is a i18n tool to translate your Ruby application in several languages.
Use merb_r18n
to localize Merb Web application and r18n-desktop
to localize desktop application.
Features
-
It has special support for countries with two official languages. If there isn’t translation in user locale, it will be found in locales, which user may know (not only in default locale). For example, many people in Belarus can understand Russian, and locale has information about it.
-
It can format numbers and time to the rules of the user locale, translate month and week days name and give other locale information.
-
It has translation for commons words, like “OK”, “Cancel”, etc.
-
It storage translation in rich YAML format. You can put procedures and pluralization (“1 comment”, “5 comments”) in your translation.
-
It can translate Web and desktop applications.
Usage
Locale
All supported locales are storage in R18n gem at locales
dir. If you want to add your locale, please write me to [email protected].
To get information about locale create R18n::Locale instance:
locale = R18n::Locale.load('en')
You can get from locale:
-
Locale title and RFC 3066 code:
locale['title'] #=> "English" locale['code'] #=> "en"
-
Language direction (left to right, or right to left for Arabic and Hebrew):
locale['direction'] #=> "ltr"
-
Week start day (“sunday” or “monday”)
locale['week']['start'] #=> "sunday"
Translation
Translation files use YAML format and has name like en.yml (English) or en_US.yml (USA English dialect) with language/country code (RFC 3066).
In translation you can use:
-
Strings
robot: This is robot percent: "Percent sign (%)"
-
Numbers
number: 123 float: 12.45
-
Pluralizable messages
robots: !!pl 0: No robots 1: One robot n: %1 robots
-
Procedures
sum: !!proc |x, y| x + y
To get translated string use method with key name or square brackets [] for keys, which is same with Object methods (class
, inspect
, etc):
i18n.robot #=> "This is robot"
i18n["robot"] #=> "This is robot"
Translation may be hierarchical:
i18n.post.add #=> "Add post"
i18n['post']['add'] #=> "Add post"
If locale willn’t be found in user locale R18n will search it in they sublocales or in another locale, which user know:
i18n.no.in.english #=> "В английском нет"
Translated string has locale
method and you can get it locale (Locale instance or code string if locale is’t supported in R18n):
i18n.no.in.english.locale #=> Locale ru (Русский)
You can replace some parameters in translated string by put it as arguments:
name: "My name is %1"
i18n.name('John') #=> "My name is John"
Pluralizable messages get item count from first argument:
i18n.robots(0) #=> "No robots"
i18n.robots(1) #=> "One robot"
i18n.robots(50) #=> "50 robots"
If there isn’t pluralization for some number, translation will be use ‘n’. If there isn’t locale file for translation, it will be use English pluralization rule (0, 1 and ‘n’).
R18n already has translation for common words for most supported locales. See base/
in dir in gem.
i18n.yes #=> "Yes"
i18n.cancel #=> "Cancel"
i18n.delete #=> "Delete"
Localization
You can print number and float according to the rules of the user locale:
i18n.l -12000.5 #=> "−12,000.5"
Number and float formatters will also put real typographic minus and put non-break thin spaces (for locale, which use it as digit separator).
You can translate months and week days names in Time, Date and DateTime by strftime
method:
i18n.l Time.now, "%B" #=> "September"
R18n has some time formats for locales: :month
, :time
, :date
, :short_data
, :long_data
, :datetime
, :short_datetime
and :long_datetime
:
i18n.l Time.now, :date #=> "09/21/2008"
By default strftime
will be use :datetime
format for Time and DateTime and :date
for Date:
i18n.l Time.now #=> "Sun Sep 21 19:50:04 GMT 2008"
License
R18n is licensed under the GNU Lesser General Public License version 3. You can read it in LICENSE file or in www.gnu.org/licenses/lgpl.html.
Author
Andrey “A.I.” Sitnik <[email protected]>