Class: Tolk::Locale

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Import, Pagination::Methods, Sync
Defined in:
app/models/tolk/locale.rb

Constant Summary collapse

PLURALIZATION_KEYS =

cldr.unicode.org/index/cldr-spec/plural-rules - TODO: usage of ‘none’ isn’t standard-conform

['none', 'zero', 'one', 'two', 'few', 'many', 'other']

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Import

included, #read_locale_file

Methods included from Sync

included

Methods included from Pagination::Methods

#pagination_method, #pagination_param

Class Method Details

._dump_pathObject



10
11
12
13
# File 'app/models/tolk/locale.rb', line 10

def self._dump_path
  # Necessary to acces rails.root at runtime !
  @dump_path ||= Tolk.config.dump_path.is_a?(Proc) ? instance_eval(&Tolk.config.dump_path) : Tolk.config.dump_path
end

.dump_all(*args) ⇒ Object



56
57
58
# File 'app/models/tolk/locale.rb', line 56

def dump_all(*args)
  secondary_locales.each { |locale| locale.dump(*args) }
end

.dump_yaml(name, *args) ⇒ Object



60
61
62
# File 'app/models/tolk/locale.rb', line 60

def dump_yaml(name, *args)
  where(name: name).first.dump(*args)
end

.pluralization_data?(data) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'app/models/tolk/locale.rb', line 70

def pluralization_data?(data)
  keys = data.keys.map(&:to_s)
  keys.all? {|k| PLURALIZATION_KEYS.include?(k) }
end

.primary_language_nameObject



48
49
50
# File 'app/models/tolk/locale.rb', line 48

def primary_language_name
  primary_locale.language_name
end

.primary_locale(reload = false) ⇒ Object



40
41
42
43
44
45
46
# File 'app/models/tolk/locale.rb', line 40

def primary_locale(reload = false)
  @_primary_locale = nil if reload
  @_primary_locale ||= begin
    raise "Primary locale is not set. Please set Locale.primary_locale_name in your application's config file" unless self.primary_locale_name
    where(name: self.primary_locale_name).first_or_create
  end
end

.rename(old_name, new_name) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/models/tolk/locale.rb', line 195

def self.rename(old_name, new_name)
  if old_name.blank? || new_name.blank?
    "You need to provide both names, aborting."
  else
    if locale = where(name: old_name).first
      locale.name = new_name
      locale.save
      "Locale ' #{old_name}' was renamed '#{new_name}'"
    else
      "Locale with name '#{old_name}' not found."
    end
  end
end

.secondary_localesObject



52
53
54
# File 'app/models/tolk/locale.rb', line 52

def secondary_locales
  all - [primary_locale]
end

.special_key_or_prefix?(prefix, key) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/models/tolk/locale.rb', line 64

def special_key_or_prefix?(prefix, key)
  self.special_prefixes.include?(prefix) || self.special_keys.include?(key)
end

Instance Method Details

#count_phrases_with_updated_translation(page = nil) ⇒ Object



97
98
99
# File 'app/models/tolk/locale.rb', line 97

def count_phrases_with_updated_translation(page = nil)
  find_phrases_with_translations(page, :'tolk_translations.primary_updated' => true).count
end

#count_phrases_without_translationObject



92
93
94
95
# File 'app/models/tolk/locale.rb', line 92

def count_phrases_without_translation
  existing_ids = self.translations(:select => 'tolk_translations.phrase_id').map(&:phrase_id).uniq
  Tolk::Phrase.count - existing_ids.count
end

#dump(to = self.locales_config_path, exporter = Tolk::Export) ⇒ Object



76
77
78
# File 'app/models/tolk/locale.rb', line 76

def dump(to = self.locales_config_path, exporter = Tolk::Export)
  exporter.dump(name: name, data: to_hash, destination: to)
end

#get(key) ⇒ Object



177
178
179
180
181
182
# File 'app/models/tolk/locale.rb', line 177

def get(key)
  if phrase = Tolk::Phrase.where(key: key).first
    t = self.translations.where(:phrase_id => phrase.id).first
    t.text if t
  end
end

#has_updated_translations?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'app/models/tolk/locale.rb', line 80

def has_updated_translations?
  translations.where('tolk_translations.primary_updated' => true).count > 0
end

#language_nameObject



173
174
175
# File 'app/models/tolk/locale.rb', line 173

def language_name
  Tolk.config.mapping[self.name] || self.name
end

#phrases_with_translation(page = nil) ⇒ Object



84
85
86
# File 'app/models/tolk/locale.rb', line 84

def phrases_with_translation(page = nil)
  find_phrases_with_translations(page, :'tolk_translations.primary_updated' => false)
end

#phrases_with_updated_translation(page = nil) ⇒ Object



88
89
90
# File 'app/models/tolk/locale.rb', line 88

def phrases_with_updated_translation(page = nil)
  find_phrases_with_translations(page, :'tolk_translations.primary_updated' => true)
end

#phrases_without_translation(page = nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/tolk/locale.rb', line 101

def phrases_without_translation(page = nil)
  phrases = Tolk::Phrase.all.order('tolk_phrases.key ASC')

  existing_ids = self.translations(:select => 'tolk_translations.phrase_id').map(&:phrase_id).uniq
  phrases = phrases.where('tolk_phrases.id NOT IN (?)', existing_ids) if existing_ids.present?

  result = phrases.public_send(pagination_method, page)
  if Rails.version =~ /^4\.0/
    ActiveRecord::Associations::Preloader.new result, :translations
  else
    ActiveRecord::Associations::Preloader.new().preload(result, :translations)
  end

  result
end

#primary?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'app/models/tolk/locale.rb', line 169

def primary?
  name == self.class.primary_locale_name
end

#search_phrases(query, scope, key_query, page = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/tolk/locale.rb', line 117

def search_phrases(query, scope, key_query, page = nil)
  return [] unless query.present? || key_query.present?

  translations = case scope
  when :origin
    Tolk::Locale.primary_locale.translations.containing_text(query)
  else # :target
    self.translations.containing_text(query)
  end

  phrases = Tolk::Phrase.all.order('tolk_phrases.key ASC')
  phrases = phrases.containing_text(key_query)

  phrases = phrases.where('tolk_phrases.id IN(?)', translations.map(&:phrase_id).uniq)
  phrases.public_send(pagination_method, page)
end

#search_phrases_without_translation(query, page = nil) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/tolk/locale.rb', line 134

def search_phrases_without_translation(query, page = nil)
  return phrases_without_translation(page) unless query.present?

  phrases = Tolk::Phrase.all.order('tolk_phrases.key ASC')

  found_translations_ids = Tolk::Locale.primary_locale.translations.where(["tolk_translations.text LIKE ?", "%#{query}%"]).to_a.map(&:phrase_id).uniq
  existing_ids = self.translations.select('tolk_translations.phrase_id').to_a.map(&:phrase_id).uniq
#      phrases = phrases.scoped(:conditions => ['tolk_phrases.id NOT IN (?) AND tolk_phrases.id IN(?)', existing_ids, found_translations_ids]) if existing_ids.present?
  phrases = phrases.where(['tolk_phrases.id NOT IN (?) AND tolk_phrases.id IN(?)', existing_ids, found_translations_ids]) if existing_ids.present?
  result = phrases.public_send(pagination_method, page)
  if Rails.version =~ /^4\.0/
    ActiveRecord::Associations::Preloader.new result, :translations
  else
    ActiveRecord::Associations::Preloader.new().preload(result, :translations)
  end

  result
end

#to_hashObject



153
154
155
156
157
158
159
160
161
162
163
# File 'app/models/tolk/locale.rb', line 153

def to_hash
  data = translations.includes(:phrase).references(:phrases).order(phrases.arel_table[:key]).
    each_with_object({}) do |translation, locale|
      if translation.phrase.key.include?(".")
        locale.deep_merge!(unsquish(translation.phrase.key, translation.value))
      else
        locale[translation.phrase.key] = translation.value
      end
    end
  { name => data }
end

#to_paramObject



165
166
167
# File 'app/models/tolk/locale.rb', line 165

def to_param
  name.parameterize
end

#translations_with_htmlObject



184
185
186
187
188
189
190
191
192
193
# File 'app/models/tolk/locale.rb', line 184

def translations_with_html
  translations = self.translations.all(:conditions => "tolk_translations.text LIKE '%>%' AND
    tolk_translations.text LIKE '%<%' AND tolk_phrases.key NOT LIKE '%_html'", :joins => :phrase)
  if Rails.version =~ /^4\.0/
    ActiveRecord::Associations::Preloader.new translations, :phrase
  else
    ActiveRecord::Associations::Preloader.new().preload(translations, :phrase)
  end
  translations
end