@@ -189,6 +189,46 @@ def translate_from_namespace(namespace, default = '')
189189 I18n . t ( lookups . shift , scope : :"#{ i18n_scope } .#{ namespace } " , default : lookups ) . presence
190190 end
191191
192+ def translate_label ( namespace = :labels , default = '' )
193+ model_names = lookup_model_names . dup
194+ lookups = [ ]
195+
196+ while !model_names . empty?
197+ joined_model_names = model_names . join ( "." )
198+ model_names . shift
199+
200+ lookups << :"#{ joined_model_names } .#{ lookup_action } .#{ reflection_or_attribute_name } "
201+ lookups << :"#{ joined_model_names } .#{ reflection_or_attribute_name } "
202+ end
203+ lookups << :"defaults.#{ lookup_action } .#{ reflection_or_attribute_name } "
204+ lookups << :"defaults.#{ reflection_or_attribute_name } "
205+
206+ lookups = lookups . map { |l | :"#{ i18n_scope } .#{ namespace } .#{ l } " }
207+
208+ if object . class . respond_to? ( :human_attribute_name )
209+ attribute = reflection_or_attribute_name . to_s
210+ klass_scope = object . class . i18n_scope
211+ if attribute . include? ( "." )
212+ namespace , _ , attribute = attribute . rpartition ( "." )
213+ namespace . tr! ( "." , "/" )
214+
215+ object . class . lookup_ancestors . map do |klass |
216+ lookups << :"#{ klass_scope } .attributes.#{ klass . model_name . i18n_key } /#{ namespace } .#{ attribute } "
217+ end
218+ lookups << :"#{ klass_scope } .attributes.#{ namespace } .#{ attribute } "
219+ else
220+ object . class . lookup_ancestors . map do |klass |
221+ lookups << :"#{ klass_scope } .attributes.#{ klass . model_name . i18n_key } .#{ attribute } "
222+ end
223+ end
224+ lookups << :"#{ klass_scope } .attributes.#{ attribute } "
225+ end
226+
227+
228+ lookups << default unless SimpleForm . enforce_translations
229+ I18n . t ( lookups . shift , default : lookups , raise : SimpleForm . enforce_translations ) . presence
230+ end
231+
192232 def merge_wrapper_options ( options , wrapper_options )
193233 if wrapper_options
194234 wrapper_options = set_input_classes ( wrapper_options )
0 commit comments