Class: Air18n::Phrase

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/air18n/phrase.rb

Constant Summary collapse

RICH_TEXT_PATTERN =

Janky! TODO(jason) don’t be so simple and silly :(

'<p'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.by_key(lookup) ⇒ Object

TODO(jkb) retire this method, it is very old and ugly. It is used by the contextual translations code.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/air18n/phrase.rb', line 85

def self.by_key lookup
  if !@by_key then
    @by_key = {}
    Phrase.all.group_by{|e| e.key}.each_pair do |k,v|
      if v.is_a? Array
        @by_key[k] = v.first
      else
      end
    end
  end

  # Check if maybe this key has been created since @by_key was initialized.
  @by_key[lookup] ||= Phrase.find_by_key(lookup)

  @by_key[lookup]
end

.is_rich?(text) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/air18n/phrase.rb', line 157

def self.is_rich?(text)
  text.include?(RICH_TEXT_PATTERN)
end

Instance Method Details

#compute_value_hashObject



79
80
81
# File 'lib/air18n/phrase.rb', line 79

def compute_value_hash
  value.present? ? Digest::MD5.new.update(value).to_s : ""
end

#copy_existing_translationsObject

When we create a phrase with the same default text as an existing phrase, we copy the older phrase’s existing translations over.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/air18n/phrase.rb', line 38

def copy_existing_translations
  existing_phrase = Phrase.
      where(:value => self.value).
      where('`phrases`.id < ?', self.id).
      order('id DESC').
      all.
      find { |p| p.value == self.value }  # Make sure we don't copy from a phrase with different
                                          # capitalization (that matched query because of DB
                                          # collation)

  if existing_phrase.present?
    translations = PhraseTranslation.
        where(:phrase_id => existing_phrase.id).
        latest.
        order('`phrase_translations`.id DESC')
    translations_by_language = translations.group_by do |pt|
      I18n.language_from_locale(pt.locale)
    end

    # We do some gymnastics here to create the translations so that we
    # create the ones for e.g. "zh" after the ones for "zh-TW" .
    #
    # This is because translations for specific locales are sometimes
    # generated from the more general locale.
    translations_by_language.each do |language, translations|
      translations_with_language_last = translations.sort_by do |pt|
        -pt.locale.to_s.size
      end
      translations_with_language_last.each do |pt|
        copy = PhraseTranslation.new
        copy.user_id = 0  # 0 means Automated
        copy.phrase = self
        copy.key = self.key
        copy.locale = pt.locale
        copy.value = pt.value
        copy.save
      end
    end
  end
end

#is_rich_text?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/air18n/phrase.rb', line 161

def is_rich_text?
  !!(self.value && Phrase.is_rich?(self.value))
end

#latest_translation(locale) ⇒ Object

Fetches text of the most recent translation, if available.



149
150
151
# File 'lib/air18n/phrase.rb', line 149

def latest_translation locale
  PhraseTranslation.where(:phrase_id => id, :locale => locale, :is_latest => true).first
end

#mark_translations_staleObject

When we change a phrase’s text, we mark its existing translations as stale.



16
17
18
19
20
21
22
23
24
25
# File 'lib/air18n/phrase.rb', line 16

def mark_translations_stale
  translations = PhraseTranslation.find_all_by_phrase_id(id)
  translations.each do |translation|
    translation.is_stale = true

    # Skip validations. This is because for historical reasons some
    # translations are invalid. We want to mark these stale regardless.
    translation.save!(:validate => false)
  end
end

#record_phrase_revisionObject

When we change a phrase’s text, we record the text. This is so we have a record of all default texts that a phrase key has taken on in the past.



29
30
31
32
33
34
# File 'lib/air18n/phrase.rb', line 29

def record_phrase_revision
  value_hash = compute_value_hash
  if value_hash.present?
    PhraseRevision.find_or_create_by_key_and_value_hash_and_value(key, value_hash, value)
  end
end

#revisions_in_orderObject



165
166
167
# File 'lib/air18n/phrase.rb', line 165

def revisions_in_order
  phrase_revisions.order('phrase_revisions.created_at ASC')
end

#variablesObject



153
154
155
# File 'lib/air18n/phrase.rb', line 153

def variables
  PhraseTranslation.detect_variables(value)
end