Sha256: 06798392d5b098e6c7aed048a60b1e36dfc4628ad8ed346fca974550702eeac0

Contents?: true

Size: 1.33 KB

Versions: 5

Compression:

Stored size: 1.33 KB

Contents

class Translation < ActiveRecord::Base
  
  cattr_accessor :locales_config_path
  self.locales_config_path = "#{Rails.root}/config/locales"
  
  scope :pending, where('value is null')
  scope :not_pending, where('value is not null')
  scope :search_query, lambda {|query| where("key LIKE ? OR value LIKE ?", "%#{query}%", "%#{query}%") }

  # create one yml per locale
  def self.dump_all(to = self.locales_config_path)
    I18n.available_locales.each do |locale|
      File.open("#{to}/#{locale}.yml", "w+") do |file|
        hash = to_hash(locale)
        hash = { locale.to_s => {} }if hash.size == 0
        YAML.dump(hash, file)
      end
    end
  end
  
  protected
  
  def self.to_hash(locale)
    hash = {}
    translations = self.where(:locale => locale).where("value not null")
    translations.each do |translation|
      if translation.key.include?(".")
        hash.deep_merge!(locale.to_s => unsquish(translation.key, translation.value))
      else
        hash.merge!(locale.to_s => { translation.key => translation.value }) 
      end
    end
    hash
  end
    
  def self.unsquish(string, value)
    if string.is_a?(String)
      unsquish(string.split("."), value)
    elsif string.size == 1
      { string.first => value }
    else
      key  = string[0]
      rest = string[1..-1]
      { key => unsquish(rest, value) }
    end
  end
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
merrycms-0.1.7 app/models/translation.rb
merrycms-0.1.4 app/models/translation.rb
merrycms-0.1.2 app/models/translation.rb
merrycms-0.1.1 app/models/translation.rb
merrycms-0.1.0 app/models/translation.rb