Skip to content

Commit

Permalink
Merge pull request #144 from HParker/make-string-patch-optional
Browse files Browse the repository at this point in the history
Convert String#% to Ruby 1.9+ behavior with option use maintain current behavior
  • Loading branch information
grosser authored Jan 6, 2025
2 parents e3a7bae + 47eee63 commit c2bba50
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
15 changes: 15 additions & 0 deletions lib/fast_gettext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ def self.add_text_domain(name, options)
translation_repositories[name] = TranslationRepository.build(name, options)
end

def self.allow_invalid_keys!
eval(<<CODE)
class ::String
alias :_fast_gettext_old_format_m :%
def %(*args)
begin
_fast_gettext_old_format_m(*args)
rescue KeyError
self
end
end
end
CODE
end

# some repositories know where to store their locales
def self.locale_path
translation_repositories[text_domain].instance_variable_get(:@options)[:path]
Expand Down
16 changes: 0 additions & 16 deletions lib/fast_gettext/vendor/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,3 @@ def %(args)
end
end
end

# 1.9.1 if you misspell a %{key} your whole page would blow up, no thanks...
begin
("%{b}" % {:a=>'b'})
rescue KeyError
class String
alias :_fast_gettext_old_format_m :%
def %(*args)
begin
_fast_gettext_old_format_m(*args)
rescue KeyError
self
end
end
end
end
14 changes: 13 additions & 1 deletion spec/fast_gettext/vendor/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,19 @@ def %(*args)
end
end

it "does not raise when key was not found" do
it "raise when key was not found" do
lambda { ("%{typo} xxx" % {:something=>1}) }.should raise_error(KeyError)
end

it "does not raise when key was not found if allow_invalid_keys! is enabled" do
FastGettext.allow_invalid_keys!
("%{typo} xxx" % {:something=>1}).should == "%{typo} xxx"

# cleanup
eval(<<CODE)
class ::String
alias :% :_fast_gettext_old_format_m
end
CODE
end
end

0 comments on commit c2bba50

Please sign in to comment.