Class: Tolk::Translation
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Tolk::Translation
- Defined in:
- app/models/tolk/translation.rb
Instance Attribute Summary collapse
-
#explicit_nil ⇒ Object
Returns the value of attribute explicit_nil.
-
#primary ⇒ Object
Returns the value of attribute primary.
Class Method Summary collapse
Instance Method Summary collapse
- #boolean? ⇒ Boolean
- #out_of_date? ⇒ Boolean
- #primary_translation ⇒ Object
- #text=(value) ⇒ Object
- #up_to_date? ⇒ Boolean
- #value ⇒ Object
- #variables ⇒ Object
- #variables_match? ⇒ Boolean
Instance Attribute Details
#explicit_nil ⇒ Object
Returns the value of attribute explicit_nil.
25 26 27 |
# File 'app/models/tolk/translation.rb', line 25 def explicit_nil @explicit_nil end |
#primary ⇒ Object
Returns the value of attribute primary.
22 23 24 |
# File 'app/models/tolk/translation.rb', line 22 def primary @primary end |
Class Method Details
.detect_variables(search_in) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/models/tolk/translation.rb', line 77 def self.detect_variables(search_in) variables = case search_in when String then Set.new(search_in.scan(/\{\{(\w+)\}\}/).flatten + search_in.scan(/\%\{(\w+)\}/).flatten) when Array then search_in.inject(Set[]) { |carry, item| carry + detect_variables(item) } when Hash then search_in.values.inject(Set[]) { |carry, item| carry + detect_variables(item) } else Set[] end # delete special i18n variable used for pluralization itself (might not be used in all values of # the pluralization keys, but is essential to use pluralization at all) if search_in.is_a?(Hash) && Tolk::Locale.pluralization_data?(search_in) variables.delete_if {|v| v == 'count' } else variables end end |
Instance Method Details
#boolean? ⇒ Boolean
28 29 30 |
# File 'app/models/tolk/translation.rb', line 28 def boolean? text.is_a?(TrueClass) || text.is_a?(FalseClass) || text == 't' || text == 'f' end |
#out_of_date? ⇒ Boolean
36 37 38 |
# File 'app/models/tolk/translation.rb', line 36 def out_of_date? primary_updated? end |
#primary_translation ⇒ Object
40 41 42 43 44 45 46 |
# File 'app/models/tolk/translation.rb', line 40 def primary_translation @_primary_translation ||= begin if locale && !locale.primary? phrase.translations.primary end end end |
#text=(value) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/models/tolk/translation.rb', line 48 def text=(value) value = value.to_s if value.kind_of?(Integer) if primary_translation && primary_translation.boolean? value = case value.to_s.downcase.strip when 'true', 't' true when 'false', 'f' false else self.explicit_nil = true nil end super unless value == text else value = value.strip if value.is_a?(String) && Tolk.config.strip_texts super unless value.to_s == text end end |
#up_to_date? ⇒ Boolean
32 33 34 |
# File 'app/models/tolk/translation.rb', line 32 def up_to_date? not out_of_date? end |
#value ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'app/models/tolk/translation.rb', line 67 def value if text.is_a?(String) && /^\d+$/.match(text) text.to_i elsif (primary_translation || self).boolean? %w[true t].member?(text.to_s.downcase.strip) else text end end |
#variables ⇒ Object
94 95 96 |
# File 'app/models/tolk/translation.rb', line 94 def variables self.class.detect_variables(text) end |
#variables_match? ⇒ Boolean
98 99 100 |
# File 'app/models/tolk/translation.rb', line 98 def variables_match? self.variables == primary_translation.variables end |