Class: Tolk::Locale
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Tolk::Locale
show all
- Includes:
- Import, Sync
- Defined in:
- app/models/tolk/locale.rb
Constant Summary
collapse
- PLURALIZATION_KEYS =
['none', '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_path ⇒ Object
7
8
9
10
|
# File 'app/models/tolk/locale.rb', line 7
def self._dump_path
@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) ⇒ Object
54
55
56
57
58
59
60
61
|
# File 'app/models/tolk/locale.rb', line 54
def dump_all(to = self.locales_config_path)
secondary_locales.each do |locale|
File.open("#{to}/#{locale.name}.yml", "w+") do |file|
data = locale.to_hash
data.respond_to?(:ya2yaml) ? file.write(data.ya2yaml(:syck_compatible => true)) : YAML.dump(locale.to_hash, file)
end
end
end
|
.pluralization_data?(data) ⇒ Boolean
68
69
70
71
|
# File 'app/models/tolk/locale.rb', line 68
def pluralization_data?(data)
keys = data.keys.map(&:to_s)
keys.all? {|k| PLURALIZATION_KEYS.include?(k) }
end
|
.primary_language_name ⇒ Object
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
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'app/models/tolk/locale.rb', line 169
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_locales ⇒ Object
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
63
64
65
|
# File 'app/models/tolk/locale.rb', line 63
def special_key_or_prefix?(prefix, key)
self.special_prefixes.include?(prefix) || self.special_keys.include?(key)
end
|
Instance Method Details
#count_phrases_without_translation ⇒ Object
86
87
88
89
|
# File 'app/models/tolk/locale.rb', line 86
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
155
156
157
158
159
160
|
# File 'app/models/tolk/locale.rb', line 155
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
74
75
76
|
# File 'app/models/tolk/locale.rb', line 74
def has_updated_translations?
translations.count(:conditions => {:'tolk_translations.primary_updated' => true}) > 0
end
|
#language_name ⇒ Object
151
152
153
|
# File 'app/models/tolk/locale.rb', line 151
def language_name
Tolk.config.mapping[self.name] || self.name
end
|
#phrases_with_translation(page = nil) ⇒ Object
78
79
80
|
# File 'app/models/tolk/locale.rb', line 78
def phrases_with_translation(page = nil)
find_phrases_with_translations(page, :'tolk_translations.primary_updated' => false)
end
|
#phrases_with_updated_translation(page = nil) ⇒ Object
82
83
84
|
# File 'app/models/tolk/locale.rb', line 82
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
91
92
93
94
95
96
97
98
99
100
|
# File 'app/models/tolk/locale.rb', line 91
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
147
148
149
|
# File 'app/models/tolk/locale.rb', line 147
def primary?
name == self.class.primary_locale_name
end
|
#search_phrases(query, scope, key_query, page = nil, options = {}) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'app/models/tolk/locale.rb', line 102
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 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
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'app/models/tolk/locale.rb', line 119
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_hash ⇒ Object
133
134
135
136
137
138
139
140
141
|
# File 'app/models/tolk/locale.rb', line 133
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_param ⇒ Object
143
144
145
|
# File 'app/models/tolk/locale.rb', line 143
def to_param
name.parameterize
end
|
#translations_with_html ⇒ Object
162
163
164
165
166
167
|
# File 'app/models/tolk/locale.rb', line 162
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
|