Skip to content

Commit f6f6fc8

Browse files
committed
Add option to enforce translations
1 parent 921ce9c commit f6f6fc8

File tree

6 files changed

+60
-5
lines changed

6 files changed

+60
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Remove test files from the gem package. [@orien](https://github.com/orien)
44
* Speed up input mapping lookup by avoiding rescuing exceptions. [@meanphil](https://github.com/meanphil) [@kriom](https://github.com/kriom) [@egeek](https://github.com/egeek)
5+
* Add support to enforcing translations defined in the locale files instead of humanizing attributes. [@Nerian](https://github.com/Nerian)
56

67
## 5.2.0
78

lib/simple_form.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ def self.configured? #:nodoc:
213213
mattr_accessor :input_field_valid_class
214214
@@input_field_valid_class = nil
215215

216+
# If set to true all labels, hints, and placeholders would be expected to have an entry in a locale file and raise
217+
# an exception if not.
218+
mattr_accessor :enforce_translations
219+
@@enforce_translations = false
220+
216221
# Retrieves a given wrapper
217222
def self.wrapper(name)
218223
@@wrappers[name.to_s] or raise WrapperNotFound, "Couldn't find wrapper with name #{name}"

lib/simple_form/components/labels.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,8 @@ def required_label_text #:nodoc:
7171

7272
# First check labels translation and then human attribute name.
7373
def label_translation #:nodoc:
74-
if SimpleForm.translate_labels && (translated_label = translate_from_namespace(:labels))
74+
if SimpleForm.translate_labels && (translated_label = translate_label)
7575
translated_label
76-
elsif object.class.respond_to?(:human_attribute_name)
77-
object.class.human_attribute_name(reflection_or_attribute_name.to_s)
7876
else
7977
attribute_name.to_s.humanize
8078
end

lib/simple_form/inputs/base.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

test/components/label_test.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ def with_label_for(object, attribute_name, type, options = {})
2323

2424
test 'label uses human attribute name from object when available' do
2525
with_label_for @user, :description, :text
26-
assert_select 'label[for=user_description]', /User Description!/
26+
assert_select 'label[for=user_description]', /Description/
2727
end
2828

2929
test 'label uses human attribute name based on association name' do
3030
with_label_for @user, :company_id, :string, setup_association: true
31-
assert_select 'label', /Company Human Name!/
31+
assert_select 'label', /Company/
3232
end
3333

3434
test 'label uses i18n based on model, action, and attribute to lookup translation' do
@@ -85,6 +85,16 @@ def @controller.action_name; nil; end
8585
end
8686
end
8787

88+
test 'missing translation in location file raises exception if SimpleForm.enforce_translations is true' do
89+
swap SimpleForm, enforce_translations: true do
90+
store_translations(:en, simple_form: { }) do
91+
assert_raise I18n::MissingTranslationData do
92+
with_label_for @user, :age, :integer
93+
end
94+
end
95+
end
96+
end
97+
8898
test 'label uses i18n with lookup for association name' do
8999
store_translations(:en, simple_form: { labels: {
90100
user: { company: 'My company!' }

test/support/models.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def group_method
8686

8787
class User
8888
extend ActiveModel::Naming
89+
extend ActiveModel::Translation
8990
include ActiveModel::Conversion
9091

9192
attr_accessor :id, :name, :company, :company_id, :time_zone, :active, :age,

0 commit comments

Comments
 (0)