Skip to content

Instantly share code, notes, and snippets.

@rubytastic
Created January 31, 2013 14:57
Show Gist options
  • Save rubytastic/4683394 to your computer and use it in GitHub Desktop.
Save rubytastic/4683394 to your computer and use it in GitHub Desktop.
module Tolk
module Import
def self.included(base)
base.send :extend, ClassMethods
end
module ClassMethods
def import_secondary_locales
locales = Dir.entries(self.locales_config_path)
locale_block_filter = Proc.new {
|l| ['.', '..'].include?(l) ||
!l.ends_with?('.yml') ||
l.match(/(.*\.){2,}/) # reject files of type xxx.en.yml
}
locales = locales.reject(&locale_block_filter).map { |x| x.split('.').first }
locales = locales - [Tolk::Locale.primary_locale.name]
locales.each { |l| import_locale(l) }
end
def import_locale(locale_name)
locale = Tolk::Locale.find_or_create_by_name(locale_name)
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
end
def read_locale_file
locale_file = "#{self.locales_config_path}/#{self.name}.yml"
raise "Locale file #{locale_file} does not exists" unless File.exists?(locale_file)
puts "[INFO] Reading #{locale_file} for locale #{self.name}"
self.class.flat_hash(YAML::load(IO.read(locale_file))[self.name])
rescue
puts "[ERROR] File #{locale_file} expected to declare #{self.name} locale, but it does not. Skipping this file."
nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment