Class: TranslationCenter::TranslationKey

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/translation_center/translation_key.rb

Constant Summary collapse

PER_PAGE =
7

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.keys_countObject



89
90
91
# File 'app/models/translation_center/translation_key.rb', line 89

def self.keys_count
  TranslationKey.all.count
end

.langs_statsObject

builds hash of stats about the langs supported by translation center



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/models/translation_center/translation_key.rb', line 124

def self.langs_stats
  stats = {}
  all_count = keys_count
  I18n.available_locales.each do |locale|
    stats[locale] = {}
    stats[locale]['name'] = TranslationCenter::CONFIG['lang'][locale.to_s]['name']

    translated = translated_count(locale)
    pending = pending_count(locale)
    untranslated = untranslated_count(locale)

    stats[locale]['translated_percentage'] = (100.0 * translated / all_count).round(2)
    stats[locale]['pending_percentage'] = (100.0 * pending / all_count).round(2)
    stats[locale]['untranslated_percentage'] = (100.0 * untranslated / all_count).round(2)

    stats[locale]['translated_count'] = translated
    stats[locale]['pending_count'] = pending
    stats[locale]['untranslated_count'] = untranslated
  end
  stats
end

.pending_count(lang) ⇒ Object

returns the count of pending keys in lang



99
100
101
# File 'app/models/translation_center/translation_key.rb', line 99

def self.pending_count(lang)
  TranslationKey.pending(lang).count
end

.pending_percentage(lang) ⇒ Object

returns the percentage of pending keys in lang



114
115
116
# File 'app/models/translation_center/translation_key.rb', line 114

def self.pending_percentage(lang)
  (100.0 * TranslationKey.pending(lang).count / keys_count).round(2)
end

.translated_count(lang) ⇒ Object

returns the count of translated keys in lang



94
95
96
# File 'app/models/translation_center/translation_key.rb', line 94

def self.translated_count(lang)
  TranslationKey.translated(lang).count
end

.translated_percentage(lang) ⇒ Object

returns the percentage of translated keys in lang



109
110
111
# File 'app/models/translation_center/translation_key.rb', line 109

def self.translated_percentage(lang)
  (100.0 * TranslationKey.translated(lang).count / keys_count).round(2)
end

.untranslated_count(lang) ⇒ Object

returns the count of untranslated keys in lang



104
105
106
# File 'app/models/translation_center/translation_key.rb', line 104

def self.untranslated_count(lang)
  TranslationKey.untranslated(lang).count
end

.untranslated_percentage(lang) ⇒ Object

returns the percentage of untranslated keys in lang



119
120
121
# File 'app/models/translation_center/translation_key.rb', line 119

def self.untranslated_percentage(lang)
  (100.0 * TranslationKey.untranslated(lang).count / keys_count).round(2)
end

Instance Method Details

#accepted_in?(lang) ⇒ Boolean Also known as: translated_in?

returns true if the key is translated (has accepted translation) in this lang

Returns:

  • (Boolean)


43
44
45
# File 'app/models/translation_center/translation_key.rb', line 43

def accepted_in?(lang)
  self.send("#{lang}_status") == 'translated'
end

#accepted_translation_in(lang) ⇒ Object

returns the accepted translation in certain language



49
50
51
# File 'app/models/translation_center/translation_key.rb', line 49

def accepted_translation_in(lang)
  self.translations.accepted(self.id).in(lang).first
end

#add_categoryObject

add a category of this translation key



23
24
25
26
27
28
29
# File 'app/models/translation_center/translation_key.rb', line 23

def add_category
  category_name = self.name.to_s.split('.').first
  # if one word then add to general category
  category_name = self.name.to_s.split('.').size == 1 ? 'general' : self.name.to_s.split('.').first
  self.category = TranslationCenter::Category.find_or_create_by(name: category_name)
  self.last_accessed = Time.now
end

#add_to_hash(all_translations, lang) ⇒ Object

adds a translation key with its translation to a translation yaml hash send the hash and the language as parameters



159
160
161
162
# File 'app/models/translation_center/translation_key.rb', line 159

def add_to_hash(all_translations, lang)
  levels = self.name.split('.')
  add_to_hash_rec(all_translations, levels, lang.to_s)
end

#children_translations(locale) ⇒ Object



146
147
148
149
150
151
# File 'app/models/translation_center/translation_key.rb', line 146

def children_translations(locale)
  TranslationKey.where('name LIKE ?', "#{self.name}.%").inject({}) do |translations, child|
    translations[child.name.split('.').last.to_sym] = child.accepted_translation_in(locale).try(:value)
    translations
  end
end

#create_default_translationObject

create default translation



81
82
83
84
85
86
87
# File 'app/models/translation_center/translation_key.rb', line 81

def create_default_translation
  translation = self.translations.build(value: self.name.to_s.split('.').last.titleize,
                                        lang: :en, status: 'accepted')
  translation.translator = TranslationCenter.prepare_translator

  translation.save
end

#has_children?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'app/models/translation_center/translation_key.rb', line 153

def has_children?
  TranslationKey.where('name LIKE ?', "#{self.name}.%").count >= 1
end

#has_translations_in?(lang) ⇒ Boolean

returns true if the key has translations in the language

Returns:

  • (Boolean)


60
61
62
# File 'app/models/translation_center/translation_key.rb', line 60

def has_translations_in?(lang)
  !no_translations_in?(lang)
end

#no_translations_in?(lang) ⇒ Boolean Also known as: untranslated_in?

returns true if the translation key is untranslated (has no translations) in the language

Returns:

  • (Boolean)


54
55
56
# File 'app/models/translation_center/translation_key.rb', line 54

def no_translations_in?(lang)
  self.send("#{lang}_status") == 'untranslated'
end

#pending_in?(lang) ⇒ Boolean

returns true if the key is pending (has translations but none is accepted)

Returns:

  • (Boolean)


65
66
67
# File 'app/models/translation_center/translation_key.rb', line 65

def pending_in?(lang)
  self.send("#{lang}_status") == 'pending'
end

#status(lang) ⇒ Object

returns the status of the key in a language



70
71
72
73
74
75
76
77
78
# File 'app/models/translation_center/translation_key.rb', line 70

def status(lang)
  if accepted_in?(lang)
    'translated'
  elsif pending_in?(lang)
    'pending'
  else
    'untranslated'
  end
end

#update_status(lang) ⇒ Object

updates the status of the translation key depending on the translations



32
33
34
35
36
37
38
39
40
# File 'app/models/translation_center/translation_key.rb', line 32

def update_status(lang)
  if self.translations.in(lang).blank?
    self.update_attribute("#{lang}_status", 'untranslated')
  elsif !self.translations.in(lang).accepted(self.id).blank?
    self.update_attribute("#{lang}_status", 'translated')
  else
    self.update_attribute("#{lang}_status", 'pending')
  end
end