lib/ab_admin/models/locator.rb in ab_admin-0.8.3 vs lib/ab_admin/models/locator.rb in ab_admin-0.9.0

- old
+ new

@@ -56,16 +56,42 @@ raise "Missing file #{path}" unless File.file?(path) data = YAML.load_file(path) csv_data.each do |d| key_parts = [l.to_s] + d[0].split('.') raise "Invalid key #{d[0]}" unless data.dig(*key_parts) - data.store_multi(d[i], *key_parts) + data.dig_store(d[i], *key_parts) end save path, data end end + INTERPOLATION_REGEXP = /%{[^}]+}/ + def csv_errors(csv) + return ['CSV blank'] if csv.blank? + csv_data = CSV.parse(csv) + errors = [] + csv_data.shift.each_with_index do |l, i| + next if i.zero? + unless I18n.available_locales.include?(l.to_sym) + errors << "Unknown locale #{l}" + next + end + csv_data.each do |d| + key = d[0] + next if d[i].blank? + if translations.dig(I18n.default_locale, key) + unless translations.dig(I18n.default_locale, key).scan(INTERPOLATION_REGEXP).sort == d[i].scan(INTERPOLATION_REGEXP).sort + errors << "Wrong interpolations #{I18n.default_locale}:'#{translations.dig(I18n.default_locale, key)}' #{l}:#{d[i]}" + end + else + errors << "Extra interpolations #{l}:#{d[i]}" if d[i].scan(INTERPOLATION_REGEXP).present? + end + end + end + errors + end + def translations @translations ||= I18n.backend.send(:translations).slice(*I18n.available_locales).transform_values{|v| v.flatten_hash.transform_keys{|k| k.join('.') } } end def translations_for_files(files) @@ -91,10 +117,10 @@ def prepare_files message = nil locale_replace_regexp = Regexp.new("(^#{I18n.default_locale}|(?<=\.)#{I18n.default_locale}(?=\.yml))") - locale_files = @files.map { |path| self.class.prepare_data(path) } + locale_files = @files.find_all{|f| f =~ /\/\w+(\.readonly)?\.yml/ }.map { |path| self.class.prepare_data(path) } main_locale_files = locale_files.find_all { |file| file.locale == I18n.default_locale } main_locale_files.each do |main_file| I18n.available_locales.each do |locale| next if locale == I18n.default_locale \ No newline at end of file