Sha256: ff997b68b9bf15cdc89b24885cacaea62d10fb50add3c55c80dbc4fe1e0f2740

Contents?: true

Size: 1.07 KB

Versions: 10

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

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

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

  protected

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
fast_gettext-4.1.0 lib/fast_gettext/translation_repository/db_models/translation_key.rb
fast_gettext-4.0.0 lib/fast_gettext/translation_repository/db_models/translation_key.rb
fast_gettext-3.1.0 lib/fast_gettext/translation_repository/db_models/translation_key.rb
fast_gettext-2.3.0 lib/fast_gettext/translation_repository/db_models/translation_key.rb
fast_gettext-2.2.0 lib/fast_gettext/translation_repository/db_models/translation_key.rb
fast_gettext-2.1.0 lib/fast_gettext/translation_repository/db_models/translation_key.rb
fast_gettext-2.0.3 lib/fast_gettext/translation_repository/db_models/translation_key.rb
fast_gettext-2.0.2 lib/fast_gettext/translation_repository/db_models/translation_key.rb
fast_gettext-2.0.1 lib/fast_gettext/translation_repository/db_models/translation_key.rb
fast_gettext-2.0.0 lib/fast_gettext/translation_repository/db_models/translation_key.rb