<commit_before>module RailsI18nterface
  class TranslateController < ApplicationController

    before_filter :init_translations
    before_filter :set_locale
    
    def index
      load_db_translations
      initialize_keys
      filter_by_key_pattern
      filter_by_text_pattern
      filter_by_translated_or_changed
      sort_keys
      paginate_keys
      @total_entries = @keys.size
    end

    def load_db_translations
      @versions = {}
      @dbvalues = {@to_locale => {}}
      (Translation.where(:locale => @to_locale) || []).each { |translation|
        @versions[translation.key] = translation.updated_at.to_i
        @dbvalues[@to_locale][translation.key] = translation.value
      }
    end

    def export
      locale = params[:locale].to_sym
      keys = {locale => I18n.backend.send(:translations)[locale] || {}}
      Translation.where(:locale => @to_locale).each { |translation|
        next if translation.value == ''
        next if !translation.value
        set_nested(keys[locale], translation.key.split("."), translation.value)
      }
      remove_blanks keys
      yaml = keys_to_yaml(deep_stringify_keys(keys))
      response.headers['Content-Disposition'] = "attachment; filename=#{locale}.yml"
      render :text => yaml
    end

    def remove_blanks hash
      hash.each { |k, v|
        if !v || v == ''
          hash.delete k
        end
        if v.is_a? Hash
          remove_blanks v
          if v == {}
            hash.delete k
          end
        end
      }<commit_msg>now you can create translations through models and they will show up in admin<commit_after>module RailsI18nterface
  class TranslateController < ApplicationController

    before_filter :init_translations
    before_filter :set_locale
    
    def index
      @dbvalues = {}
      initialize_keys
      load_db_translations
      filter_by_key_pattern
      filter_by_text_pattern
      filter_by_translated_or_changed
      sort_keys
      paginate_keys
      @total_entries = @keys.size
    end

    def load_db_translations
      @versions = {}
      @dbvalues = {@to_locale => {}}
      (Translation.where(:locale => @to_locale) || []).each { |translation|
        @versions[translation.key] = translation.updated_at.to_i
        @dbvalues[@to_locale][translation.key] = translation.value
        @keys << translation.key
      }
      @keys.uniq!
    end

    def export
      locale = params[:locale].to_sym
      keys = {locale => I18n.backend.send(:translations)[locale] || {}}
      Translation.where(:locale => @to_locale).each { |translation|
        next if translation.value == ''
        next if !translation.value
        set_nested(keys[locale], translation.key.split("."), translation.value)
      }
      remove_blanks keys
      yaml = keys_to_yaml(deep_stringify_keys(keys))
      response.headers['Content-Disposition'] = "attachment; filename=#{locale}.yml"
      render :text => yaml
    end

    def remove_blanks hash
      hash.each { |k, v|
        if !v || v == ''
          hash.delete k
        end
        if v.is_a? Hash
          remove_blanks v
          if v == {}
            hash.delete k
          end
        end
      }
{'fraction_non_alphanumeric': 0.10087994971715902, 'fraction_numerical': 0.00251414204902577, 'mean_word_length': 2.3825717321997875, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4820669', 'n_tokens_mistral': 940, 'n_tokens_neox': 913, 'n_words': 193}