lively-ability-6202
08/14/2025, 1:41 PMlemon-wall-20836
08/14/2025, 3:06 PMlively-ability-6202
08/14/2025, 3:08 PMruby
module Localization
extend ActiveSupport::Concern
# Returns a lambda for field help text (evaluated in field context)
# Usage: help: field_help(:guest_email)
def field_help(field_name, **interpolations)
# Capture the translation key outside the lambda
translation_key = field_translation_key(field_name, :help)
-> { I18n.t(translation_key, **interpolations) }
end
# Returns a string for field placeholder
# Usage: placeholder: field_placeholder(:comment)
def field_placeholder(field_name, **interpolations)
I18n.t(field_translation_key(field_name, :placeholder), **interpolations)
end
private
def field_translation_key(field_name, key)
# Get the full class name and extract just the resource part
# e.g., "Avo::Resources::BlockedPeriodOwnerStay" => "blocked_period_owner_stay"
# Note: We use to_s instead of name because Avo overrides the name method
resource_name = self.class.to_s.split('::').last.underscore
"avo.resources.#{resource_name}.fields.#{field_name}.#{key}"
end
end
ruby
help: field_help(:guest_type))
But would be nicer to have something more avo-native ofc 🙂lemon-wall-20836
08/14/2025, 3:45 PMlemon-wall-20836
08/14/2025, 3:45 PMlemon-wall-20836
08/14/2025, 3:46 PMlemon-wall-20836
08/14/2025, 3:46 PMlemon-wall-20836
08/15/2025, 5:23 AMruby
module Avo
class BaseResource < Avo::Resources::Base
# Users can override this class to add custom methods for all resources.
# in Avo::BaseResource
def field(field_name, *args, help_for: nil, **kwargs, &block)
if help_for.present?
help_for_key = help_for || field_name
kwargs[:help] = -> { I18n.t("avo.resources.#{resource_name}.fields.#{field_name}.#{help_for_key}") }
end
super(field_name, *args, **kwargs, &block)
end
end
endlemon-wall-20836
08/15/2025, 5:24 AMfield :guest_type, as: :text, help_for: :guest_typelemon-wall-20836
08/15/2025, 5:25 AM