Class: Translator::TranslationsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/translator/translations_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject


65
66
67
68
# File 'app/controllers/translator/translations_controller.rb', line 65

def create
  Translator.current_store[params[:key]] = params[:value]
  redirect_to :back unless request.xhr?
end

#destroyObject


70
71
72
73
74
75
76
# File 'app/controllers/translator/translations_controller.rb', line 70

def destroy
  key = params[:id].gsub('-','.')
  Translator.locales.each do |locale|
    Translator.current_store.destroy_entry(locale.to_s + '.' + key)
  end
  redirect_to :back unless request.xhr?
end

#exportObject


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/translator/translations_controller.rb', line 35

def export
  @keys = Translator.keys_for_strings

  headers["Content-Disposition"] = "attachment; filename=\"translations.json\""

  render json: Hash[@keys.map { |key|
    [
      key,

      Hash[Translator.locales.map do |locale|
        [
          locale,
          begin I18n.backend.translate locale, key; rescue; end
        ]
      end]
    ]
  }].to_json
end

#importObject


54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/translator/translations_controller.rb', line 54

def import
  hash = JSON.parse(params[:file].read)

  hash.each do |key_suffix, values|
    values.each do |key_prefix, value|
      Translator.current_store["#{key_prefix}.#{key_suffix}"] = value unless value.nil?
    end
  end
  redirect_to :back
end

#indexObject


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/translator/translations_controller.rb', line 5

def index
  section = params[:key].present? && params[:key] + '.'
  params[:group] = "all" unless params["group"]
  @sections = Translator.keys_for_strings(:group => params[:group]).map {|k| k = k.scan(/^[a-zA-Z0-9\-_]*\./)[0]; k ? k.gsub('.', '') : false}.select{|k| k}.uniq.sort
  @groups = ["framework", "application", "deleted"]
  @keys = Translator.keys_for_strings(:group => params[:group], :filter => section)
  if params[:search]
    @keys = @keys.select {|k|
      Translator.locales.any? {|locale| I18n.translate("#{k}", :locale => locale).to_s.downcase.include?(params[:search].downcase)}
    }
  end

  if params[:translated] == '1'
    @keys = @keys.select {|k|
      Translator.locales.all? {|locale| (begin I18n.backend.translate(locale, "#{k}") rescue nil; end).present? }
    }
  end

  if params[:translated] == '0'
    @keys = @keys.select {|k|
      Translator.locales.any? {|locale| (begin I18n.backend.translate(locale, "#{k}") rescue nil; end).blank? }
    }
  end


  @keys = paginate(@keys)

  render :layout => Translator.layout_name
end