From 47eee634b653f012e54d0f179cf16bb3763862cc Mon Sep 17 00:00:00 2001 From: Adam Hess Date: Mon, 6 Jan 2025 11:10:25 -0800 Subject: [PATCH] Convert String#% to Ruby 1.9+ behavior with option use maintain current behavior Previously String#% was overridden on versions after Ruby 1.9 to match the 1.8 behavior where missing keys in the expanded format are ignored and not replaced. This is likely suprising behavior in modern Ruby. This removes the previous patch, but still allows overriding this behavior optionally by calling `FastGettext.allow_invalid_keys!`. --- lib/fast_gettext.rb | 15 +++++++++++++++ lib/fast_gettext/vendor/string.rb | 16 ---------------- spec/fast_gettext/vendor/string_spec.rb | 14 +++++++++++++- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/lib/fast_gettext.rb b/lib/fast_gettext.rb index 86de8a89..d794ec04 100644 --- a/lib/fast_gettext.rb +++ b/lib/fast_gettext.rb @@ -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(<'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 diff --git a/spec/fast_gettext/vendor/string_spec.rb b/spec/fast_gettext/vendor/string_spec.rb index 6355e4d4..887ec3d5 100644 --- a/spec/fast_gettext/vendor/string_spec.rb +++ b/spec/fast_gettext/vendor/string_spec.rb @@ -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(<