Class: Tolk::Translation

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/tolk/translation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#explicit_nilObject

Returns the value of attribute explicit_nil.



25
26
27
# File 'app/models/tolk/translation.rb', line 25

def explicit_nil
  @explicit_nil
end

#primaryObject

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

Returns:

  • (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

Returns:

  • (Boolean)


36
37
38
# File 'app/models/tolk/translation.rb', line 36

def out_of_date?
  primary_updated?
end

#primary_translationObject



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?(Fixnum)
  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)
    super unless value.to_s == text
  end
end

#up_to_date?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/models/tolk/translation.rb', line 32

def up_to_date?
  not out_of_date?
end

#valueObject



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

#variablesObject



94
95
96
# File 'app/models/tolk/translation.rb', line 94

def variables
  self.class.detect_variables(text)
end

#variables_match?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'app/models/tolk/translation.rb', line 98

def variables_match?
  self.variables == primary_translation.variables
end