Created
September 21, 2009 22:01
-
-
Save czak/190594 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Helpery pożyczone z projektu 'russian' | |
module LocalizationHelper | |
# Ułatwia wypisywanie komunikatów zależnych od wartości liczbowych | |
# | |
# Użycie: | |
# pluralize(1, "piłka", "piłki", "piłek") # => "piłka" | |
# pluralize(2, "piłka", "piłki", "piłek") # => "piłki" | |
# pluralize(21, "piłka", "piłki", "piłek") # => "piłek" | |
# pluralize(4.12, "piłka", "piłki", "piłek", "piłki") # => "piłki" | |
# pluralize(200, "# but", "# buty", "# butów") # => "200 butów" | |
def pluralize(n, *variants) | |
raise ArgumentError, "Must have a Numeric as a first parameter" unless n.is_a?(Numeric) | |
raise ArgumentError, "Must have at least 3 variants for pluralization" if variants.size < 3 | |
raise ArgumentError, "Must have at least 4 variants for pluralization" if (variants.size < 4 && n != n.round) | |
variants_hash = pluralization_variants_to_hash(n, *variants) | |
I18n.backend.send(:pluralize, I18n.locale, variants_hash, n) | |
end | |
alias :p :pluralize | |
protected | |
# Converts an array of pluralization variants to a Hash that can be used | |
# with I18n pluralization. | |
def pluralization_variants_to_hash(n, *variants) | |
{ | |
:one => variants[0].gsub(/#/, n.to_s), | |
:few => variants[1].gsub(/#/, n.to_s), | |
:many => variants[2].gsub(/#/, n.to_s), | |
:other => variants[3] && variants[3].gsub(/#/, n).to_s | |
} | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Reguły wzorowane na http://github.com/yaroslav/russian (dla polskiego są prostsze) | |
# Polski: | |
# :one = 1 | |
# :few = 2-4, 22-24, 32-34... | |
# :many = 0, 5-21, 25-31, 35-41... | |
# :other = pozostałe (np. 0.5, 3.14) | |
{ | |
:pl => { | |
:i18n => { | |
:pluralize => lambda { |n| | |
n == 1 ? :one : [2, 3, 4].include?(n % 10) && ![12, 13, 14].include?(n % 100) ? :few : n % 10 == 0 || [1, 5, 6, 7, 8, 9].include?(n % 10) || [11, 12, 13, 14].include?(n % 100) ? :many : :other | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'i18n/backend/pluralization' | |
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= pluralize(room.size, "# osoby", "# osób", "# osób") %> | |
<%= pluralize(room.windows_count, "# okno", "# okna", "# okien") %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment