diff --git a/actionpack/lib/action_view/helpers/translation_helper.rb b/actionpack/lib/action_view/helpers/translation_helper.rb index 2772aa90861fb..945c56fa95de0 100644 --- a/actionpack/lib/action_view/helpers/translation_helper.rb +++ b/actionpack/lib/action_view/helpers/translation_helper.rb @@ -40,7 +40,11 @@ def translate(keys, options = {}) html_safe_options[:default] = MISSING_TRANSLATION unless html_safe_options[:default].blank? - translation = I18n.translate(qualified_key, html_safe_options) + translation = if RUBY_VERSION >= "2.7" + I18n.translate(qualified_key, **html_safe_options) + else + I18n.translate(qualified_key, html_safe_options) + end if translation.equal?(MISSING_TRANSLATION) options[:default].first @@ -48,7 +52,11 @@ def translate(keys, options = {}) translation.respond_to?(:html_safe) ? translation.html_safe : translation end else - I18n.translate(qualified_key, options) + if RUBY_VERSION >= "2.7" + I18n.translate(qualified_key, **options) + else + I18n.translate(qualified_key, options) + end end end @@ -71,8 +79,14 @@ def translate(keys, options = {}) alias :t :translate # Delegates to I18n.localize with no additional functionality. - def localize(*args) - I18n.localize(*args) + if RUBY_VERSION >= "2.7" + def localize(...) + I18n.localize(...) + end + else + def localize(*args) + I18n.localize(*args) + end end alias :l :localize diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 5e176c1fe96e9..93e242a8b7bee 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1435,7 +1435,17 @@ def human_attribute_name(attribute_key_name, options = {}) defaults.flatten! defaults << attribute_key_name.to_s.humanize options[:count] ||= 1 - I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => [:activerecord, :attributes])) + if RUBY_VERSION >= '2.7' + I18n.translate( + defaults.shift, + **options.merge(:default => defaults, :scope => [:activerecord, :attributes]) + ) + else + I18n.translate( + defaults.shift, + options.merge(:default => defaults, :scope => [:activerecord, :attributes]) + ) + end end # Transform the modelname into a more humane format, using I18n. @@ -1447,7 +1457,17 @@ def human_name(options = {}) :"#{klass.name.underscore}" if klass.name.present? end.compact defaults << self.name.humanize if self.name.present? - I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options)) + if RUBY_VERSION >= '2.7' + I18n.translate( + defaults.shift, + **{:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options) + ) + else + I18n.translate( + defaults.shift, + {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options) + ) + end end # True if this isn't a concrete subclass needing a STI type condition. diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 26d82d4ea153d..e3d2150ba0bcd 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -79,7 +79,11 @@ def generate_message(options = {}) keys.compact! options.merge!(:default => keys) - I18n.translate(keys.shift, options) + if RUBY_VERSION >= '2.7' + I18n.translate(keys.shift, **options) + else + I18n.translate(keys.shift, options) + end end # Wraps an error message into a full_message format. @@ -117,7 +121,11 @@ def generate_full_message(options = {}) ] options.merge!(:default => keys, :message => self.message) - I18n.translate(keys.shift, options) + if RUBY_VERSION >= '2.7' + I18n.translate(keys.shift, **options) + else + I18n.translate(keys.shift, options) + end end # Return user options with default options. diff --git a/activesupport/lib/active_support/option_merger.rb b/activesupport/lib/active_support/option_merger.rb index 63662b75c7496..cefb003c502d6 100644 --- a/activesupport/lib/active_support/option_merger.rb +++ b/activesupport/lib/active_support/option_merger.rb @@ -10,14 +10,32 @@ def initialize(context, options) private def method_missing(method, *arguments, &block) + options = nil if arguments.last.is_a?(Proc) proc = arguments.pop arguments << lambda { |*args| @options.deep_merge(proc.call(*args)) } + elsif arguments.last.respond_to?(:to_hash) + options = @options.deep_merge(arguments.pop) else - arguments << (arguments.last.respond_to?(:to_hash) ? @options.deep_merge(arguments.pop) : @options.dup) + options = @options end - @context.__send__(method, *arguments, &block) + invoke_method(method, arguments, options, &block) + end + + if RUBY_VERSION >= "2.7" + def invoke_method(method, arguments, options, &block) + if options + @context.__send__(method, *arguments, **options, &block) + else + @context.__send__(method, *arguments, &block) + end + end + else + def invoke_method(method, arguments, options, &block) + arguments << options.dup if options + @context.__send__(method, *arguments, &block) + end end end end