Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Brought back alternative convention for namespaced models in i18n #1556

Merged
merged 1 commit into from

5 participants

Thomas Höfer Prem Sichanugrist José Valim Robert Pankowecki thoefer2
Thomas Höfer

See issue #1402

I did NOT implement the usage for "Namespaced::Model.new.errors.add(...)" as I am a bit short in time currently. I basically brought back the changes from "jfirebaugh" in commit "5b8dbb0".

This means you can write your yaml file like this:

...
activerecord:
models:
invoice:
item: "Invoice::Item dot notation"
invoice/item: "Invoice::Item slash notation"
attributes:
invoice/item:
name: "Invoice::Item.name slash notation"
invoice:
item:
name: "Invoice::Item.name dot notation"

José Valim josevalim merged commit b64524d into from
Prem Sichanugrist
Collaborator

I believe this one should be backported to 3-1-stable as well, because it's a bug fix.

José Valim
Owner

3-1-stable pull request please? :)

Prem Sichanugrist
Collaborator

This has been backported in #1562

Robert Pankowecki

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
7 activemodel/lib/active_model/naming.rb
View
@@ -21,7 +21,7 @@ def initialize(klass, namespace = nil, name = nil)
@partial_path = "#{@collection}/#{@element}".freeze
@param_key = (namespace ? _singularize(@unnamespaced) : @singular).freeze
@route_key = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) : @plural).freeze
- @i18n_key = self.underscore.to_sym
+ @i18n_key = self.underscore.tr('/', '.').to_sym
end
# Transform the model name into a more humane format, using I18n. By default,
@@ -35,8 +35,9 @@ def human(options={})
@klass.respond_to?(:i18n_scope)
defaults = @klass.lookup_ancestors.map do |klass|
- klass.model_name.i18n_key
- end
+ [klass.model_name.i18n_key,
+ klass.model_name.i18n_key.to_s.tr('.', '/').to_sym]
+ end.flatten
defaults << options[:default] if options[:default]
defaults << @human
5 activemodel/lib/active_model/translation.rb
View
@@ -44,8 +44,9 @@ def lookup_ancestors
# Specify +options+ with additional translating options.
def human_attribute_name(attribute, options = {})
defaults = lookup_ancestors.map do |klass|
- :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}"
- end
+ [:"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}",
+ :"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key.to_s.tr('.', '/')}.#{attribute}"]
+ end.flatten
defaults << :"attributes.#{attribute}"
defaults << options.delete(:default) if options[:default]
10 activemodel/test/cases/translation_test.rb
View
@@ -76,5 +76,15 @@ def test_human_does_not_modify_options
Person.model_name.human(options)
assert_equal({:default => 'person model'}, options)
end
+
+ def test_alternate_namespaced_model_attribute_translation
+ I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:gender => {:attribute => 'person gender attribute'}}}}
+ assert_equal 'person gender attribute', Person::Gender.human_attribute_name('attribute')
+ end
+
+ def test_alternate_namespaced_model_translation
+ I18n.backend.store_translations 'en', :activemodel => {:models => {:person => {:gender => 'person gender model'}}}
+ assert_equal 'person gender model', Person::Gender.model_name.human
+ end
end
Something went wrong with that request. Please try again.