Class: Tolk::Locale

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Import, 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

Class Method Details

._dump_pathObject



7
8
9
10
# File 'app/models/tolk/locale.rb', line 7

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



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

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

.dump_yaml(name, *args) ⇒ Object



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

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

.pluralization_data?(data) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'app/models/tolk/locale.rb', line 67

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

.primary_language_nameObject



45
46
47
# File 'app/models/tolk/locale.rb', line 45

def primary_language_name
  primary_locale.language_name
end

.primary_locale(reload = false) ⇒ Object



37
38
39
40
41
42
43
# File 'app/models/tolk/locale.rb', line 37

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



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/models/tolk/locale.rb', line 192

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



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

def secondary_locales
  all - [primary_locale]
end

.special_key_or_prefix?(prefix, key) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#count_phrases_without_translationObject



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

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



73
74
75
# File 'app/models/tolk/locale.rb', line 73

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

#get(key) ⇒ Object



174
175
176
177
178
179
# File 'app/models/tolk/locale.rb', line 174

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)


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

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

#language_nameObject



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

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

#phrases_with_translation(page = nil) ⇒ Object



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

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

#phrases_with_updated_translation(page = nil) ⇒ Object



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

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

#phrases_without_translation(page = nil, options = {}) ⇒ Object



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

def phrases_without_translation(page = nil, options = {})
  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.paginate({:page => page, :per_page => Phrase.per_page}.merge(options))
  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)


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

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

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



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

def search_phrases(query, scope, key_query, page = nil, options = {})
  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.paginate({:page => page}.merge(options))
end

#search_phrases_without_translation(query, page = nil, options = {}) ⇒ Object



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

def search_phrases_without_translation(query, page = nil, options = {})
  return phrases_without_translation(page, options) 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.paginate({:page => page}.merge(options))
  if Rails.version =~ /^4\.0/
    ActiveRecord::Associations::Preloader.new result, :translations
  else
    ActiveRecord::Associations::Preloader.new().preload(result, :translations)
  end

  result
end

#to_hashObject



150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/tolk/locale.rb', line 150

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



162
163
164
# File 'app/models/tolk/locale.rb', line 162

def to_param
  name.parameterize
end

#translations_with_htmlObject



181
182
183
184
185
186
187
188
189
190
# File 'app/models/tolk/locale.rb', line 181

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