Created
January 9, 2009 20:28
-
-
Save Baael/45266 to your computer and use it in GitHub Desktop.
Formula pluralization for I18n
This file contains 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
# Formula pluralizations for I18n | |
# by Wojciech Zieliński ([email protected], http://github.com/baael) | |
module I18n | |
module Backend | |
class FormulaPluralization < Simple | |
def pluralize(locale, entry, count) | |
return entry unless entry.is_a?(Hash) and count | |
case translations[locale][:formula].nil? | |
when false | |
formulas=translations[locale][:formula].dup | |
key=formulas.delete(:default).to_sym | |
formulas.each { |formula| | |
key = formula[0].to_sym if eval(formula[1].gsub(/(\\\\)?\{\{([^\}]+)\}\}/,count.to_s)) | |
} | |
else | |
key = :zero if count == 0 && entry.has_key?(:zero) | |
key ||= count == 1 ? :one : :other | |
end | |
raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key) | |
entry[key] | |
end | |
end | |
end | |
end | |
I18n.backend = I18n::Backend::FormulaPluralization .new |
This file contains 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
# Formula pluralizations for I18n | |
# by Wojciech Zieliński ([email protected], http://github.com/baael) | |
pl: | |
formula: | |
one: "{{count}}==1 && {{count}}%100!=11" | |
few: "(2..4).include?({{count}}%10) && !(12..14).include?({{count}}%100)" | |
default: "other" | |
test: | |
gry: | |
one: "gra" | |
few: "gry" | |
other: "gier" |
This file contains 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
I18n.t("test.gry", :count => 0 ).should == "gier" | |
I18n.t("test.gry", :count => 1 ).should == "gra" | |
I18n.t("test.gry", :count => 2 ).should == "gry" | |
I18n.t("test.gry", :count => 3 ).should == "gry" | |
I18n.t("test.gry", :count => 4 ).should == "gry" | |
I18n.t("test.gry", :count => 5 ).should == "gier" | |
I18n.t("test.gry", :count => 6 ).should == "gier" | |
I18n.t("test.gry", :count => 7 ).should == "gier" | |
I18n.t("test.gry", :count => 21).should == "gier" | |
I18n.t("test.gry", :count => 22).should == "gry" | |
I18n.t("test.gry", :count => 25).should == "gier" | |
I18n.t("test.gry", :count => 26).should == "gier" | |
I18n.t("test.gry", :count => 101).should == "gier" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment