Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert String#% to Ruby 1.9+ behavior with option use maintain current behavior #144

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading