Ruby On Rails: pluralize for other languages
Add your rules to an inflections.rb file in config/initializers. See the API documentation:
ActiveSupport::Inflector.inflections do |inflect|
inflect.plural 'boek', 'boeken'
end
i18n Pluralization
Try this:
en.yml
:
en:
misc:
kids:
zero: no kids
one: 1 kid
other: %{count} kids
In a view:You have <%= t('misc.kids', :count => 4) %>
Updated answer for languages with multiple pluralization (tested with Rails 3.0.7):
File config/initializers/pluralization.rb
:
require "i18n/backend/pluralization"
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
File config/locales/plurals.rb
:{:ru =>
{ :i18n =>
{ :plural =>
{ :keys => [:one, :few, :other],
:rule => lambda { |n|
if n == 1
:one
else
if [2, 3, 4].include?(n % 10) &&
![12, 13, 14].include?(n % 100) &&
![22, 23, 24].include?(n % 100)
:few
else
:other
end
end
}
}
}
}
}
#More rules in this file: https://github.com/svenfuchs/i18n/blob/master/test/test_data/locales/plurals.rb
#(copy the file into `config/locales`)
File config/locales/en.yml
:en:
kids:
zero: en_zero
one: en_one
other: en_other
File config/locales/ru.yml
:ru:
kids:
zero: ru_zero
one: ru_one
few: ru_few
other: ru_other
Test:$ rails c
>> I18n.translate :kids, :count => 1
=> "en_one"
>> I18n.translate :kids, :count => 3
=> "en_other"
>> I18n.locale = :ru
=> :ru
>> I18n.translate :kids, :count => 1
=> "ru_one"
>> I18n.translate :kids, :count => 3
=> "ru_few" #works! yay!
>> I18n.translate :kids, :count => 5
=> "ru_other" #works! yay!
pluralize and singularize for spanish language
I found this great way:
http://www.slideshare.net/crnixon/advanced-internationalization-with-rails
Regards.
How use rails inflections with diferent languages
Please see the documentation for String#pluralize
pluralize(count = nil, locale = :en)You have to specify your locale if you want to use non-English pluralization rules.If the optional parameter locale is specified, the word will be
pluralized as a word of that language. By default, this parameter is
set to :en.
"canal".pluralize(:"pt-BR")
Translate model's plural form
ActiveRecord first translates the model name using I18n.translate with default
:count => 1
Pluralizing this string afterwards dosn't know about model translations.But, human accepts options so
Vehicle.model_name.human(:count => 2)
does the trick together with pluralized translations:de:
activerecord:
models:
vehicle:
one: 'Fahrzeug'
other: 'Fahrzeuge'
ruby on rails pluralization help?
Put the following code in config/environment.rb:
Inflector.inflections do |inflect|
inflect.irregular 'actividad', 'actividades'
end
Test the code in the console (script/console):'actividad'.pluralize
'actividades'.singularize
More details can be found here: http://codeidol.com/other/rubyckbk/Web-Development-Ruby-on-Rails/Understanding-Pluralization-Rules/
Ruby on Rails: how to check pluralized and single form of names
You can do this in the rails console.
$ "anonymous".pluralize
=> "anonymous"
or another example where the plural word is different.$ "cookie".pluralize
=> "cookies"
Related Topics
Ruby: Class C Includes Module M; Including Module N in M Does Not Affect C. What Gives
Will_Paginate Find Out If I'M on The Last Page
Why Can't The Mail Block See My Variable
Sorting a Hash in Ruby Based on Value and Then Key
How to Restrict Markdown Syntax in Ruby
Giving an Example of a Cycle in a Directed Graph
Unit Testing Code Which Gets Current Time
Rails How to Tell If a Sidekiq Worker Is Done with Perform_Async
Cucumber Embed for Screenshots Not Linking to Screenshot
Actionmailer Pass Local Variables to The Erb Template
Ruby: Is It Acceptable to Put More Than One Class in a File
Sorting a Multidimensional Array in Ruby
Error Installing Ruby in Yosemite
Issue Installing Gems on Windows 7 with Proxy
Install Ree-1.8.7 with Rvm on Mountain Lion
Fresh Ruby Gem from Bundler - Cannot Load My Version.Rb File