Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 70 lines (62 sloc) 2.286 kB
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, r…
josevalim authored
1 module ActiveModel
7d7d54f @rizwanreza Revised the rest of the files from ActiveModel.
rizwanreza authored
2
3 # == Active Model Translation
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
4 #
7d7d54f @rizwanreza Revised the rest of the files from ActiveModel.
rizwanreza authored
5 # Provides integration between your object and the Rails internationalization
6 # (i18n) framework.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
7 #
91c3840 @mikel Adding documentation for ActiveModel::Translation
mikel authored
8 # A minimal implementation could be:
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
9 #
91c3840 @mikel Adding documentation for ActiveModel::Translation
mikel authored
10 # class TranslatedPerson
11 # extend ActiveModel::Translation
12 # end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
13 #
ccf9577 @dolzenko Fix a bunch of minor spelling mistakes
dolzenko authored
14 # TranslatedPerson.human_attribute_name('my_attribute')
755af49 @fxn edit pass to apply API guideline wrt the use of "# =>" in example code
fxn authored
15 # # => "My attribute"
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
16 #
91c3840 @mikel Adding documentation for ActiveModel::Translation
mikel authored
17 # This also provides the required class methods for hooking into the
18 # Rails internationalization API, including being able to define a
a2bfa5f @smartinez87 Better formatting on ActiveModel::Translation
smartinez87 authored
19 # class based +i18n_scope+ and +lookup_ancestors+ to find translations in
91c3840 @mikel Adding documentation for ActiveModel::Translation
mikel authored
20 # parent classes.
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, r…
josevalim authored
21 module Translation
22 include ActiveModel::Naming
23
a2bfa5f @smartinez87 Better formatting on ActiveModel::Translation
smartinez87 authored
24 # Returns the +i18n_scope+ for the class. Overwrite if you want custom lookup.
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, r…
josevalim authored
25 def i18n_scope
26 :activemodel
27 end
28
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
29 # When localizing a string, it goes through the lookup returned by this
7d7d54f @rizwanreza Revised the rest of the files from ActiveModel.
rizwanreza authored
30 # method, which is used in ActiveModel::Name#human,
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
31 # ActiveModel::Errors#full_messages and
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, r…
josevalim authored
32 # ActiveModel::Translation#human_attribute_name.
33 def lookup_ancestors
34 self.ancestors.select { |x| x.respond_to?(:model_name) }
35 end
36
7d7d54f @rizwanreza Revised the rest of the files from ActiveModel.
rizwanreza authored
37 # Transforms attribute names into a more human format, such as "First name"
38 # instead of "first_name".
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, r…
josevalim authored
39 #
40 # Person.human_attribute_name("first_name") # => "First name"
41 #
42 # Specify +options+ with additional translating options.
43 def human_attribute_name(attribute, options = {})
cafe6a3 @carlosantoniodasilva Do not modify options hash in human_attribute_name, remove reverse_merge
carlosantoniodasilva authored
44 options = { :count => 1 }.merge!(options)
d4964b3 @josevalim Namespaced attribute lookup now works as 'model/association.attribute'.
josevalim authored
45 parts = attribute.to_s.split(".", 2)
46 attribute = parts.pop
47 namespace = parts.pop
f48d83b @carlosantoniodasilva Refactor human attribute name
carlosantoniodasilva authored
48 attributes_scope = "#{self.i18n_scope}.attributes"
d4964b3 @josevalim Namespaced attribute lookup now works as 'model/association.attribute'.
josevalim authored
49
50 if namespace
f48d83b @carlosantoniodasilva Refactor human attribute name
carlosantoniodasilva authored
51 defaults = lookup_ancestors.map do |klass|
52 :"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}"
d4964b3 @josevalim Namespaced attribute lookup now works as 'model/association.attribute'.
josevalim authored
53 end
f48d83b @carlosantoniodasilva Refactor human attribute name
carlosantoniodasilva authored
54 defaults << :"#{attributes_scope}.#{namespace}.#{attribute}"
d4964b3 @josevalim Namespaced attribute lookup now works as 'model/association.attribute'.
josevalim authored
55 else
f48d83b @carlosantoniodasilva Refactor human attribute name
carlosantoniodasilva authored
56 defaults = lookup_ancestors.map do |klass|
57 :"#{attributes_scope}.#{klass.model_name.i18n_key}.#{attribute}"
dff19f7 @kuroda Fix human_attribute_name to handle names with dots
kuroda authored
58 end
410a428 @josevalim Get rid of the alternate namespace lookup.
josevalim authored
59 end
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, r…
josevalim authored
60
a25ef06 @josevalim Allow to specify default attributes names translation in I18n yml files.
josevalim authored
61 defaults << :"attributes.#{attribute}"
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, r…
josevalim authored
62 defaults << options.delete(:default) if options[:default]
d4964b3 @josevalim Namespaced attribute lookup now works as 'model/association.attribute'.
josevalim authored
63 defaults << attribute.humanize
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, r…
josevalim authored
64
cafe6a3 @carlosantoniodasilva Do not modify options hash in human_attribute_name, remove reverse_merge
carlosantoniodasilva authored
65 options[:default] = defaults
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, r…
josevalim authored
66 I18n.translate(defaults.shift, options)
67 end
68 end
69 end
Something went wrong with that request. Please try again.