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(to = self.locales_config_path, exporter = Tolk::Export) ⇒ Object



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

def dump_all(to = self.locales_config_path, exporter = Tolk::Export)
  secondary_locales.each do |locale|
    exporter.dump(name: locale.name, data: locale.to_hash, destination: to)
  end
end

.pluralization_data?(data) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.primary_language_nameObject



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

def primary_language_name
  primary_locale.language_name
end

.primary_locale(reload = false) ⇒ Object



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

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
    find_or_create_by_name(self.primary_locale_name)
  end
end

.rename(old_name, new_name) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/models/tolk/locale.rb', line 171

def self.rename(old_name, new_name)
  if old_name.blank? || new_name.blank?
    "You need to provide both names, aborting."
  else
    if locale = find_by_name(old_name)
      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



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

def secondary_locales
  all - [primary_locale]
end

.special_key_or_prefix?(prefix, key) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#count_phrases_without_translationObject



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

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

#get(key) ⇒ Object



157
158
159
160
161
162
# File 'app/models/tolk/locale.rb', line 157

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

#has_updated_translations?Boolean

Returns:

  • (Boolean)


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

def has_updated_translations?
  translations.count(:conditions => {:'tolk_translations.primary_updated' => true}) > 0
end

#language_nameObject



153
154
155
# File 'app/models/tolk/locale.rb', line 153

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

#phrases_with_translation(page = nil) ⇒ Object



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

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

#phrases_with_updated_translation(page = nil) ⇒ Object



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

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



93
94
95
96
97
98
99
100
101
102
# File 'app/models/tolk/locale.rb', line 93

def phrases_without_translation(page = nil, options = {})
  phrases = Tolk::Phrase.scoped(:order => 'tolk_phrases.key ASC')

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

  result = phrases.paginate({:page => page, :per_page => Phrase.per_page}.merge(options))
  ActiveRecord::Associations::Preloader.new result, :translations
  result
end

#primary?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'app/models/tolk/locale.rb', line 149

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

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/tolk/locale.rb', line 104

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.scoped(:order => 'tolk_phrases.key ASC')
  phrases = phrases.containing_text(key_query)

  phrases = phrases.scoped(:conditions => ['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



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

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

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

  found_translations_ids = Tolk::Locale.primary_locale.translations.all(:conditions => ["tolk_translations.text LIKE ?", "%#{query}%"], :select => 'tolk_translations.phrase_id').map(&:phrase_id).uniq
  existing_ids = self.translations.all(:select => 'tolk_translations.phrase_id').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?

  result = phrases.paginate({:page => page}.merge(options))
  ActiveRecord::Associations::Preloader.new result, :translations
  result
end

#to_hashObject



135
136
137
138
139
140
141
142
143
# File 'app/models/tolk/locale.rb', line 135

def to_hash
  { name => translations.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 }
end

#to_paramObject



145
146
147
# File 'app/models/tolk/locale.rb', line 145

def to_param
  name.parameterize
end

#translations_with_htmlObject



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

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)
  ActiveRecord::Associations::Preloader.new translations, :phrase
  translations
end