Class: Translation

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

Constant Summary collapse

@@locales =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_read_timeObject

Clear value of @read_time. It is used durnig backend reload!



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

def self.clear_read_time
  @read_time = nil
end

.find_by_string_normalized_key(key) ⇒ Object

auto-magic finder using string normalized key to find translation. The first value in scope is the locale, the last is the key and all between are scopes



67
68
69
70
# File 'app/models/translation.rb', line 67

def self.find_by_string_normalized_key(key)
  scope = I18n.send(:normalize_translation_keys, I18n.locale, key, nil).map(&:to_s)
  self.first(:conditions => {:key => scope.last, :locale => scope.first, :scope => scope[1..-2].empty? ? nil : scope[1..-2].join(".") }) || raise(ActiveRecord::RecordNotFound, "Could not find translation with key #{key}")
end

.get_localesObject

return locales which translations are stored in db



21
22
23
# File 'app/models/translation.rb', line 21

def self.get_locales
  @@locales
end

.locales_up_to_date?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/models/translation.rb', line 62

def self.locales_up_to_date?
  @read_time.nil? ? true : self.update_locales_time.to_i < @read_time.to_i
end

.lookup(locale, key, default = nil, scope = nil) ⇒ Object

look up for translation and return it, if exists, or create if not



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/translation.rb', line 119

def self.lookup(locale, key, default = nil, scope = nil)
  scope = I18n.send(:normalize_translation_keys, locale, key, scope)
  locale = scope.delete_at(0).to_s
  key = scope.delete_at(scope.size - 1).to_s
  scope = scope.empty? ? nil : scope.join(".")
  if self.exists?(:locale => locale, :scope => scope, :key => key)
    translation = self.first(:conditions => {:locale => locale, :scope => scope, :key => key})
  else
    translation = self.new(:key => key, :default => default.nil? ? key : default, :locale => locale, :scope => scope)
    translation.save
  end
  translation
end

.scope?(name, locale = I18n.locale.to_s) ⇒ Boolean

return true if there is no translation with key == name, and there is some translations with scope == name

Returns:

  • (Boolean)


16
17
18
# File 'app/models/translation.rb', line 16

def self.scope?(name, locale = I18n.locale.to_s)
  !self.exists?(:key => name, :locale => locale, :scope => nil) && self.exists?(:scope => name, :locale => locale)
end

.scope_to_translation_hash(scope, locale = I18n.locale.to_s) ⇒ Object

return ready to by stored as translations hash of translations in scope



142
143
144
145
146
147
148
149
# File 'app/models/translation.rb', line 142

def self.scope_to_translation_hash(scope, locale = I18n.locale.to_s)
  children = self.find_by_sql ["SELECT * FROM translations WHERE scope LIKE ? and locale=? ORDER BY id", scope + "%", locale]
  hash = {}
  for child in children
    hash.deep_merge!  child.to_translation_hash()
  end
  Trendi18n::ScopeTranslations.new(locale, hash)
end

.set_localesObject

read locales which translation are stored in db



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

def self.set_locales
  @@locales = self.all(:select => "DISTINCT(locale)", :order => "locale ASC").map { |obj| obj.locale.to_sym  }
end

.set_read_timeObject

Set @read_time, date and time of first database read on current update.



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

def self.set_read_time
  @read_time = Time.zone.now if @read_time.nil?
end

.up_to_date?Boolean

Checking translations cache up-to-date status. We need to reload backend when cache is not up-to-date. It’s going on when so existing translation was updated. We can’t reload backend using after_update callback becouse there can be many proccesses running our apps and all of them must be reloaded, not only the updater.

Returns:

  • (Boolean)


58
59
60
# File 'app/models/translation.rb', line 58

def self.up_to_date?
  @read_time.nil? ? true : self.update_time.to_i < @read_time.to_i
end

.update_locales_timeObject



50
51
52
# File 'app/models/translation.rb', line 50

def self.update_locales_time
  self.exists? ? self.first(:order => "updated_at DESC").updated_at : Time.zone.at(0)
end

.update_timeObject

Return date and time of last translations update in database. It is used to get translations cache up-to-date status and we need to reload backend only after update existing translations (new translations will be cached when they will be used first time), so we are looking for the biggest update time in translations that updated_at is greater then created_at



46
47
48
# File 'app/models/translation.rb', line 46

def self.update_time
  self.exists?(["updated_at > created_at"]) ? self.first(:conditions => "updated_at > created_at", :order => "updated_at DESC").updated_at : Time.zone.at(0)
end

Instance Method Details

#has_all_plural_forms?Boolean

true if translation has all plural forms

Returns:

  • (Boolean)


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

def has_all_plural_forms?
  !self.zero.blank? && !self.one.blank? && !self.many.blank?
end

#has_some_plural_forms?Boolean

true if translation has some plural forms

Returns:

  • (Boolean)


114
115
116
# File 'app/models/translation.rb', line 114

def has_some_plural_forms?
  !self.zero.blank? || !self.one.blank? || !self.many.blank?
end

#pluralize(count) ⇒ Object

get correct pluralization form (using count)



101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/translation.rb', line 101

def pluralize(count)
  if count
    case count
    when 0 then self.zero
    when 1 then self.one
    else self.many
      end || self.default
  else
    self.translation || self.default
  end
end

#set_defaultsObject

assign default values to translation



83
84
85
86
87
88
# File 'app/models/translation.rb', line 83

def set_defaults
  self.locale = I18n.locale.to_s if self.locale.blank? # if translation does not have locale, set I18n default
  self.default = key unless self.default # set key to default if it is not exists
  self.scope = nil if self.scope.blank? #set nil to scope if it is blank (empty string)
  self.translation = nil if self.translation.blank? # set nil to translation if it is blank (empty string)
end

#set_stateObject

  • if there is no count in the key, set new if there is not translation and set finished when translation exists

  • if there is count in the key, then:

    • if translation has all plural forms, assign finished

    • if translation has some plural forms, then assign unfinished

    • if translation does not have plural forms, then assign new



78
79
80
# File 'app/models/translation.rb', line 78

def set_state
  self.state = self.with_count? ? self.has_all_plural_forms? ? "finished" : self.has_some_plural_forms? ? "unfinished" : "new" : self.translation.blank? ? "new" : "finished"
end

#to_translation_hashObject

return hash ready to by stored as translation



134
135
136
137
138
139
# File 'app/models/translation.rb', line 134

def to_translation_hash()
 path = []
 path += self.scope.split(".") unless self.scope.nil?
 path << self.key
 return path.reverse.inject(self) {|before, step| { step => before}}
end

#with_count?Boolean

don’t be nil if there is “{count}” in key

Returns:

  • (Boolean)


91
92
93
# File 'app/models/translation.rb', line 91

def with_count?
  /\{\{count\}\}/.match(self.key)
end