File: translation_key.rb

package info (click to toggle)
ruby-fast-gettext 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 720 kB
  • ctags: 301
  • sloc: ruby: 3,092; makefile: 3
file content (38 lines) | stat: -rw-r--r-- 1,068 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class TranslationKey < ActiveRecord::Base
  has_many :translations, :class_name => 'TranslationText', :dependent => :destroy

  accepts_nested_attributes_for :translations, :allow_destroy => true

  validates_uniqueness_of :key
  validates_presence_of :key

  attr_accessible :key, :translations, :translations_attributes if ActiveRecord::VERSION::MAJOR == 3 || defined?(ProtectedAttributes)

  before_save :normalize_newlines

  def self.translation(key, locale)
    return unless translation_key = find_by_key(newline_normalize(key))
    return unless translation_text = translation_key.translations.find_by_locale(locale)
    translation_text.text
  end

  def self.available_locales
    @@available_locales ||= begin
      if ActiveRecord::VERSION::MAJOR >= 3
        TranslationText.group(:locale).count
      else
        TranslationText.count(:group=>:locale)
      end.keys.sort
    end
  end

  protected

  def self.newline_normalize(s)
    s.to_s.gsub("\r\n", "\n")
  end

  def normalize_newlines
    self.key = self.class.newline_normalize(key)
  end
end