1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00

Change the translation mechanism to use

`ActionView::Helpers::TranslationHelper`.

It has some advantages over only using `I18n.t`, such as the safe
translation if the key has the suffix `_html`.
This commit is contained in:
Lauro Caetano 2014-04-04 14:43:22 -03:00
parent 92a6a9c278
commit 63fb9a2773
5 changed files with 10 additions and 7 deletions

View file

@ -9,7 +9,7 @@ module SimpleForm
if hint.is_a?(String)
html_escape(hint)
else
content = translate(:hints)
content = translate_from_namespace(:hints)
content.html_safe if content
end
end

View file

@ -66,7 +66,7 @@ module SimpleForm
# First check labels translation and then human attribute name.
def label_translation #:nodoc:
if SimpleForm.translate_labels && (translated_label = translate(:labels))
if SimpleForm.translate_labels && (translated_label = translate_from_namespace(:labels))
translated_label
elsif object.class.respond_to?(:human_attribute_name)
object.class.human_attribute_name(reflection_or_attribute_name.to_s)

View file

@ -9,7 +9,7 @@ module SimpleForm
def placeholder_text
placeholder = options[:placeholder]
placeholder.is_a?(String) ? placeholder : translate(:placeholders)
placeholder.is_a?(String) ? placeholder : translate_from_namespace(:placeholders)
end
end
end

View file

@ -1,10 +1,13 @@
require 'simple_form/i18n_cache'
require 'active_support/core_ext/string/output_safety'
require 'action_view'
require 'action_view/helpers'
module SimpleForm
module Inputs
class Base
include ERB::Util
include ActionView::Helpers::TranslationHelper
extend I18nCache
@ -167,7 +170,7 @@ module SimpleForm
# email: 'E-mail.'
#
# Take a look at our locale example file.
def translate(namespace, default = '')
def translate_from_namespace(namespace, default = '')
model_names = lookup_model_names.dup
lookups = []
@ -182,7 +185,7 @@ module SimpleForm
lookups << :"defaults.#{reflection_or_attribute_name}"
lookups << default
I18n.t(lookups.shift, scope: :"#{i18n_scope}.#{namespace}", default: lookups).presence
t(lookups.shift, scope: :"#{i18n_scope}.#{namespace}", default: lookups).presence
end
def merge_wrapper_options(options, wrapper_options)

View file

@ -98,7 +98,7 @@ module SimpleForm
end
def translate_collection
if translated_collection = translate(:options)
if translated_collection = translate_from_namespace(:options)
@collection = collection.map do |key|
[translated_collection[key] || key, key.to_s]
end
@ -110,7 +110,7 @@ module SimpleForm
if options[key] == :translate
namespace = key.to_s.pluralize
options[key] = translate(namespace, true)
options[key] = translate_from_namespace(namespace, true)
end
end
end