| 1 |
= Localization Plugin for Rails |
|---|
| 2 |
|
|---|
| 3 |
This plugin provides a simple, gettext-like method to |
|---|
| 4 |
provide localizations. |
|---|
| 5 |
|
|---|
| 6 |
== Features |
|---|
| 7 |
|
|---|
| 8 |
* Any number of languages or locales |
|---|
| 9 |
* Simple method to defines singluar/plural translations |
|---|
| 10 |
* Can use lambdas to provide Ruby-code based dynamic translations |
|---|
| 11 |
* Customizable for different instances of the application |
|---|
| 12 |
|
|---|
| 13 |
== Usage |
|---|
| 14 |
|
|---|
| 15 |
If the localization plugin is installed, it is used automatically. |
|---|
| 16 |
|
|---|
| 17 |
You need to create a /lang dir in your RAILS_ROOT. |
|---|
| 18 |
|
|---|
| 19 |
The recommended way to use it is to create files that are named |
|---|
| 20 |
like the languages you define in them (but you can put everything in |
|---|
| 21 |
one big file too.) |
|---|
| 22 |
|
|---|
| 23 |
For instance-customizable strings, add overrides in files you |
|---|
| 24 |
put in /lang/custom. |
|---|
| 25 |
|
|---|
| 26 |
=== Simple example: |
|---|
| 27 |
|
|---|
| 28 |
Create a file /lang/translations.rb: |
|---|
| 29 |
|
|---|
| 30 |
Localization.define('de') do |l| |
|---|
| 31 |
l.store 'yes', 'Ja' |
|---|
| 32 |
l.store 'no', 'Nein' |
|---|
| 33 |
end |
|---|
| 34 |
|
|---|
| 35 |
Localization.define('fr') do |l| |
|---|
| 36 |
l.store 'yes', 'oui' |
|---|
| 37 |
l.store 'no', 'non' |
|---|
| 38 |
end |
|---|
| 39 |
|
|---|
| 40 |
In your controller or application.rb: |
|---|
| 41 |
|
|---|
| 42 |
Localization.lang = 'de' # or 'fr' |
|---|
| 43 |
|
|---|
| 44 |
In your view: |
|---|
| 45 |
|
|---|
| 46 |
<%=_ 'yes' %> |
|---|
| 47 |
<%=_ 'no' %> |
|---|
| 48 |
|
|---|
| 49 |
Because the _ method is simply an extension to Object, you |
|---|
| 50 |
can use it anywhere (models/controllers/views/libs). |
|---|
| 51 |
|
|---|
| 52 |
=== Extended example: |
|---|
| 53 |
|
|---|
| 54 |
Create a file /lang/default.rb with following contents: |
|---|
| 55 |
|
|---|
| 56 |
Localization.define do |l| |
|---|
| 57 |
l.store '(time)', lambda { |t| t.strftime('%I:%M%p') } |
|---|
| 58 |
end |
|---|
| 59 |
|
|---|
| 60 |
Create a file /lang/de_DE.rb with following contents: |
|---|
| 61 |
|
|---|
| 62 |
Localization.define('de_DE') do |l| |
|---|
| 63 |
l.store '%d entries', ['Ein Eintrag', '%d EintrÀge'] |
|---|
| 64 |
l.store '(time)', lambda { |t| t.strftime('%H:%M') } |
|---|
| 65 |
end |
|---|
| 66 |
|
|---|
| 67 |
In your controller or application.rb: |
|---|
| 68 |
|
|---|
| 69 |
Localization.lang = 'de_DE' |
|---|
| 70 |
|
|---|
| 71 |
In your view: |
|---|
| 72 |
|
|---|
| 73 |
<%=_ '%d entries', 1 %> # singular variant is chosen |
|---|
| 74 |
<%=_ '%d entries', 4 %> # plural variant is chosen |
|---|
| 75 |
<%=_ '(time)', Time.now %> # call the block with a parameter |
|---|
| 76 |
|
|---|
| 77 |
== Translation file guesstimation |
|---|
| 78 |
|
|---|
| 79 |
You can generate a guesstimation of all strings needed to be |
|---|
| 80 |
translated in your views by first adding the _('blah') syntax |
|---|
| 81 |
everywhere and then calling: |
|---|
| 82 |
|
|---|
| 83 |
puts Localization.generate_l10n_file |
|---|
| 84 |
|
|---|
| 85 |
in the Rails console. |
|---|