diff options
-rw-r--r-- | lib/i18n_fixes.rb | 11 | ||||
-rw-r--r-- | spec/lib/i18n_interpolation.rb | 28 |
2 files changed, 39 insertions, 0 deletions
diff --git a/lib/i18n_fixes.rb b/lib/i18n_fixes.rb index a85faddcb..82d1b2c3a 100644 --- a/lib/i18n_fixes.rb +++ b/lib/i18n_fixes.rb @@ -14,6 +14,17 @@ def _(key, options = {}) gettext_interpolate(translation, options) end +def n_(*keys) + # The last parameter should be the values to do the interpolation with + if keys.count > 3 + options = keys.pop + else + options = {} + end + translation = FastGettext.n_(*keys).html_safe + gettext_interpolate(translation, options) +end + MATCH = /\{\{([^\}]+)\}\}/ def gettext_interpolate(string, values) diff --git a/spec/lib/i18n_interpolation.rb b/spec/lib/i18n_interpolation.rb index e8d046757..b07cf1e9a 100644 --- a/spec/lib/i18n_interpolation.rb +++ b/spec/lib/i18n_interpolation.rb @@ -12,7 +12,35 @@ describe "when using i18n" do it "should assume that simple translations are always html safe" do _("Hello").should be_html_safe end +end + +describe "n_" do + it "should return the translated singular" do + FastGettext.should_receive(:n_).with("Apple", "Apples", 1).and_return("Apfel") + n_("Apple", "Apples", 1).should == "Apfel" + end + + it "should return the translated plural" do + FastGettext.should_receive(:n_).with("Apple", "Apples", 3).and_return("Äpfel") + n_("Apple", "Apples", 3).should == "Äpfel" + end + it "should return the translated singular interpolated" do + FastGettext.should_receive(:n_).with("I eat {{count}} apple", "I eat {{count}} apples", 1). + and_return("Ich esse {{count}} Apfel") + n_("I eat {{count}} apple", "I eat {{count}} apples", 1, :count => 1).should == "Ich esse 1 Apfel" + end + + it "should return the translated plural interpolated" do + FastGettext.should_receive(:n_).with("I eat {{count}} apple", "I eat {{count}} apples", 3). + and_return("Ich esse {{count}} Äpfel") + n_("I eat {{count}} apple", "I eat {{count}} apples", 3, :count => 3).should == "Ich esse 3 Äpfel" + end + + it "should always be html safe when there is no interpolation" do + FastGettext.should_receive(:n_).with("Apple", "Apples", 1).and_return("Apfel") + n_("Apple", "Apples", 1).should be_html_safe + end end describe "gettext_interpolate" do |