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.



27
28
29
# File 'app/models/tolk/translation.rb', line 27

def explicit_nil
  @explicit_nil
end

#primaryObject

Returns the value of attribute primary.



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

def primary
  @primary
end

Class Method Details

.detect_variables(search_in) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/tolk/translation.rb', line 79

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)


30
31
32
# File 'app/models/tolk/translation.rb', line 30

def boolean?
  text.is_a?(TrueClass) || text.is_a?(FalseClass) || text == 't' || text == 'f'
end

#out_of_date?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/models/tolk/translation.rb', line 38

def out_of_date?
  primary_updated?
end

#primary_translationObject



42
43
44
45
46
47
48
# File 'app/models/tolk/translation.rb', line 42

def primary_translation
  @_primary_translation ||= begin
    if locale && !locale.primary?
      phrase.translations.primary
    end
  end
end

#text=(value) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/tolk/translation.rb', line 50

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)


34
35
36
# File 'app/models/tolk/translation.rb', line 34

def up_to_date?
  not out_of_date?
end

#valueObject



69
70
71
72
73
74
75
76
77
# File 'app/models/tolk/translation.rb', line 69

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



96
97
98
# File 'app/models/tolk/translation.rb', line 96

def variables
  self.class.detect_variables(text)
end

#variables_match?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'app/models/tolk/translation.rb', line 100

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