22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/tolk/import.rb', line 22
def import_locale(locale_name)
locale = Tolk::Locale.where(name: locale_name).first_or_create
data = locale.read_locale_file
return unless data
phrases = Tolk::Phrase.all
count = 0
data.each do |key, value|
phrase = phrases.detect {|p| p.key == key}
if phrase
translation = locale.translations.new(:text => value, :phrase => phrase)
if translation.save
count = count + 1
elsif translation.errors[:variables].present?
puts "[WARN] Key '#{key}' from '#{locale_name}.yml' could not be saved: #{translation.errors[:variables].first}"
end
else
puts "[ERROR] Key '#{key}' was found in '#{locale_name}.yml' but #{Tolk::Locale.primary_language_name} translation is missing"
end
end
puts "[INFO] Imported #{count} keys from #{locale_name}.yml"
end
|