app/models/translation.rb in trendi18n-0.9.2 vs app/models/translation.rb in trendi18n-0.9.3

- old
+ new

@@ -10,18 +10,23 @@ validates_length_of :locale, :within => 2..6 validates_uniqueness_of :key, :scope => %w(locale scope) @@locales = [] + # return true if there is no translation with key == name, and there is some translations with scope == name + def self.scope?(name, locale = I18n.locale.to_s) + !self.exists?(:key => name, :locale => locale, :scope => nil) && self.exists?(:scope => name, :locale => locale) + end + # return locales which translations are stored in db def self.get_locales @@locales end # read locales which translation are stored in db def self.set_locales - @@locales = self.all(:select => "DISTINCT(locale)", :order => "locale ASC").map { |obj| obj.locale } + @@locales = self.all(:select => "DISTINCT(locale)", :order => "locale ASC").map { |obj| obj.locale.to_sym } end # Set @read_time, date and time of first database read on current update. # @read_time is used to get translations cache up-to-date status def self.set_read_time @@ -124,11 +129,23 @@ end translation end # return hash ready to by stored as translation - def to_translation_hash - path = self.scope.nil? ? [self.key] : self.scope.split(".") << self.key + 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 + + # return ready to by stored as translations hash of translations in scope + 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 end