diff options
author | Maarten Claes <maartencls@gmail.com> | 2015-01-22 22:59:42 +0100 |
---|---|---|
committer | Maarten Claes <maartencls@gmail.com> | 2015-01-22 23:02:48 +0100 |
commit | c946b2a30038701cdff3ed6e22b8fdcbe2ee3b7d (patch) | |
tree | 5a84286d94acabd898b6ebd9916424a31ce08ee5 /actionview/lib/action_view/helpers/tags/translator.rb | |
parent | ee3dd2f0ff1820804888cb3a7475dd593deb0212 (diff) | |
download | rails-c946b2a30038701cdff3ed6e22b8fdcbe2ee3b7d.tar.gz rails-c946b2a30038701cdff3ed6e22b8fdcbe2ee3b7d.tar.bz2 rails-c946b2a30038701cdff3ed6e22b8fdcbe2ee3b7d.zip |
Reduce duplication when generating translations
Diffstat (limited to 'actionview/lib/action_view/helpers/tags/translator.rb')
-rw-r--r-- | actionview/lib/action_view/helpers/tags/translator.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/actionview/lib/action_view/helpers/tags/translator.rb b/actionview/lib/action_view/helpers/tags/translator.rb new file mode 100644 index 0000000000..b626dfab3b --- /dev/null +++ b/actionview/lib/action_view/helpers/tags/translator.rb @@ -0,0 +1,40 @@ +module ActionView + module Helpers + module Tags # :nodoc: + class Translator # :nodoc: + attr_reader :object, :object_name, :method_and_value, :i18n_scope + + def initialize(object, object_name, method_and_value, i18n_scope) + @object = object + @object_name = object_name.gsub(/\[(.*)_attributes\]\[\d+\]/, '.\1') + @method_and_value = method_and_value + @i18n_scope = i18n_scope + end + + def call + placeholder ||= I18n.t("#{object_name}.#{method_and_value}", :default => i18n_default, :scope => i18n_scope).presence + placeholder || human_attribute_name + end + + def i18n_default + if model + key = model.model_name.i18n_key + ["#{key}.#{method_and_value}".to_sym, ""] + else + "" + end + end + + def human_attribute_name + if model && model.class.respond_to?(:human_attribute_name) + model.class.human_attribute_name(method_and_value) + end + end + + def model + @model ||= object.to_model if object.respond_to?(:to_model) + end + end + end + end +end |