Skip to content

Commit 561f3d4

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

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ 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_from_namespace(:labels, mandatory: true))
7575
translated_label
7676
elsif object.class.respond_to?(:human_attribute_name)
7777
object.class.human_attribute_name(reflection_or_attribute_name.to_s)

lib/simple_form/inputs/base.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def html_options_for(namespace, css_classes)
171171
# email: 'E-mail.'
172172
#
173173
# Take a look at our locale example file.
174-
def translate_from_namespace(namespace, default = '')
174+
def translate_from_namespace(namespace, default = '', mandatory: false)
175175
model_names = lookup_model_names.dup
176176
lookups = []
177177

@@ -184,9 +184,13 @@ def translate_from_namespace(namespace, default = '')
184184
end
185185
lookups << :"defaults.#{lookup_action}.#{reflection_or_attribute_name}"
186186
lookups << :"defaults.#{reflection_or_attribute_name}"
187-
lookups << default
187+
lookups << default unless SimpleForm.enforce_translations
188188

189-
I18n.t(lookups.shift, scope: :"#{i18n_scope}.#{namespace}", default: lookups).presence
189+
I18n.t(
190+
lookups.shift,
191+
scope: :"#{i18n_scope}.#{namespace}",
192+
default: lookups,
193+
raise: SimpleForm.enforce_translations && mandatory).presence
190194
end
191195

192196
def merge_wrapper_options(options, wrapper_options)

test/components/label_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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!' }

0 commit comments

Comments
 (0)