app/models/interpret/translation.rb in interpret-0.2.0 vs app/models/interpret/translation.rb in interpret-0.2.1
- old
+ new
@@ -2,11 +2,41 @@
class Translation < I18n::Backend::ActiveRecord::Translation
default_scope order('locale ASC')
validates_uniqueness_of :key, :scope => :locale
+ after_update :set_stale
+
+ scope :stale, where(:stale => true)
+
+ private
+
+ # If this translations is in the main language, mark this translation in
+ # other languages as stale, so the translators know that they must change
+ # it.
+ def set_stale
+ return unless locale == I18n.default_locale.to_s
+
+ Translation.where(:key => key).where(Translation.arel_table[:locale].not_eq(locale)).update_all({:stale => true})
+ end
+
class << self
+
+ def allowed
+ s = order("")
+ if Interpret.wild_blacklist.any?
+ black_keys = Interpret.wild_blacklist.map{|x| "#{CGI.escape(x)}%"}
+ s = s.where(arel_table[:key].does_not_match_all(black_keys))
+ end
+ if Interpret.fixed_blacklist.any?
+ black_keys = Interpret.fixed_blacklist.map{|x| "#{CGI.escape(x)}"}
+ s = s.where(arel_table[:key].does_not_match_all(black_keys))
+ end
+ s
+ end
+
+
# Generates a hash representing the tree structure of the translations
# for the given locale. It includes only "folders" in the sense of
# locale keys that includes some real translations, or other keys.
def get_tree(lang = I18n.default_locale)
t = arel_table
@@ -38,28 +68,33 @@
res
end
# Import the contents of the given .yml locale file into the database
# backend. If a given key already exists in database, it will be
- # overwritten, otherwise it won't be touched. This means that it won't
+ # overwritten, otherwise it won't be touched. This means that it won't
# delete any existing translation, it only overwrites the ones you give
# in the file.
- # If the given file has new translations, these will be ignored.
#
# The language will be obtained from the first unique key of the yml
# file.
def import(file)
hash = YAML.load file
raise ArgumentError, "the YAML file must contain an unique first key representing the locale" unless hash.keys.count == 1
lang = hash.keys.first
+ unless lang.to_s == I18n.locale.to_s
+ raise ArgumentError, "the language doesn't match"
+ end
+
records = parse_hash(hash.first[1], lang)
transaction do
records.each do |x|
if tr = locale(lang).find_by_key(x.key)
tr.value = x.value
- tr.save(:validate => false)
+ tr.save!
+ else
+ x.save!
end
end
end
end