Class: CouchI18n::TranslationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/couch_i18n/translations_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/couch_i18n/translations_controller.rb', line 46

def create
  @translation = CouchI18n::Translation.new translation_params
  if @translation.value.present? && params[:is_json].present?
    @translation.value = JSON.parse(@translation.value)
  end
  if @translation.save
    redirect_to({:action => :index, :offset => @translation.translation_key.to_s.sub(/\.[\w\s-]+$/, '')}, :notice => I18n.t('couch_i18n.action.create.successful', :model => CouchI18n::Translation.model_name.human))
  else
    render :action => :new
  end
end

#destroyObject



78
79
80
81
82
83
84
# File 'app/controllers/couch_i18n/translations_controller.rb', line 78

def destroy
  @translation = CouchI18n::Translation.find(params[:id])
  if @translation.destroy
    flash[:notice] = I18n.t('couch_i18n.action.destroy.successful', :model => CouchI18n::Translation.model_name.human)
  end
  redirect_to({:action => :index, :offset => @translation.translation_key.to_s.sub(/\.\w+$/, '')})
end

#destroy_offsetObject

Very dangarous action, please handle this with care, large removals are supported! DELETE /couch_i18n/translations/destroy_offset?…



148
149
150
151
152
153
154
155
156
# File 'app/controllers/couch_i18n/translations_controller.rb', line 148

def destroy_offset
  if params[:offset].present?
    @translations = CouchI18n::Translation.with_offset(params[:offset])
  else
    @translations = CouchI18n::Translation.all
  end
  @translations.map(&:destroy)
  redirect_to({:action => :index}, :notice => I18n.t('couch_i18n.general.offset_deleted_notice', :count => @translations.size, :offset => params[:offset]))
end

#editObject

GET /couch_i18n/translations/:id/edit



59
60
61
# File 'app/controllers/couch_i18n/translations_controller.rb', line 59

def edit
  @translation = CouchI18n::Translation.find(params[:id])
end

#exportObject

POST /couch_i18n/translations/export Export to yml, csv or json



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/couch_i18n/translations_controller.rb', line 88

def export
  if params[:offset].present?
    if params[:untranslated].present?
      @translations = CouchI18n::Translation.unstranslated_with_offset(params[:offset])
    else
      @translations = CouchI18n::Translation.with_offset(params[:offset])
    end
  else
    if params[:untranslated].present?
      @translations = CouchI18n::Translation.untranslated
    else
      @translations = CouchI18n::Translation.all
    end
  end
  base_filename = "export#{Time.now.strftime('%Y%m%d%H%M')}"
  if params[:exportformat] == 'csv'
    response.headers['Content-Type'] = 'text/csv'
    response.headers['Content-Disposition'] = %{attachment; filename="#{base_filename}.csv"}
    render :text => @translations.map{|s| [s.translation_key, s.translation_value.to_json].join(',')}.join("\n")
  elsif params[:exportformat] == 'json'
    response.headers['Content-Type'] = 'application/json'
    response.headers['Content-Disposition'] = %{attachment; filename="#{base_filename}.json"}
    # render :text => CouchI18n.indent_keys(@translations).to_json # for indented json
    render :json => @translations.map{|s| {s.translation_key => s.translation_value}}.to_json
  else #yaml
    response.headers['Content-Type'] = 'application/x-yaml'
    response.headers['Content-Disposition'] = %{attachment; filename="#{base_filename}.yml"}
    render :text => CouchI18n.indent_keys(@translations).to_yaml
  end
end

#importObject

POST /couch_i18n/translations/import Import yml files



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/controllers/couch_i18n/translations_controller.rb', line 121

def import
  redirect_to({:action => :index, :offset => params[:offset]}, :alert => I18n.t('couch_i18n.import.no_file_given')) and return unless params[:importfile].present?
  filename = params[:importfile].original_filename
  extension = filename.sub(/.*\./, '')
  if extension == 'yml'
    hash = YAML.load_file(params[:importfile].tempfile.path) rescue nil
    redirect_to({:action => :index, :offset => params[:offset]}, :alert => I18n.t('couch_i18n.import.cannot_parse')) and return unless hash
    CouchI18n.traverse_flatten_keys(hash).each do |key, value|
      existing = CouchI18n::Translation.find_by_translation_key(key)
      if existing
        if existing.translation_value != value || !existing.translated?
          existing.translation_value = value
          existing.translated = true
          existing.save
        end
      else
        CouchI18n::Translation.create translation_key: key, translation_value: value
      end
    end 
  else
    redirect_to({:action => :index, :offset => params[:offset]}, :alert => I18n.t('couch_i18n.import.extension_not_valid', :extension => extension)) and return 
  end
  redirect_to({:action => :index, :offset => params[:offset]}, :notice => I18n.t('couch_i18n.import.notice', :filename => filename))
end

#indexObject



3
4
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
34
35
36
# File 'app/controllers/couch_i18n/translations_controller.rb', line 3

def index
  @available_higher_offsets = []
  @available_deeper_offsets = []
  per_page = params[:per_page].presence.try(:to_i) || 30
  if params[:partfinder].present?
    if untranslated?
      @translations = CouchI18n::Translation.find_all_untranslated_by_key_part(params[:offset], page: params[:page], per_page: per_page)
    else
      @translations = CouchI18n::Translation.find_all_by_key_part(params[:offset], page: params[:page], per_page: per_page)
    end
  elsif params[:valuefinder].present?
    if untranslated?
      @translations = CouchI18n::Translation.find_all_untranslated_by_value(params[:offset], page: params[:page], per_page: per_page)
    else
      @translations = CouchI18n::Translation.find_all_by_value(params[:offset], page: params[:page], per_page: per_page)
    end
  else
    if params[:offset].present?
      if untranslated?
        @translations = CouchI18n::Translation.untranslated_with_offset(params[:offset], :page => params[:page], :per_page => per_page)
      else
        @translations = CouchI18n::Translation.with_offset(params[:offset], :page => params[:page], :per_page => per_page)
      end
    else
      if untranslated?
        @translations = CouchI18n::Translation.untranslated(:page => params[:page], :per_page => per_page)
      else
        @translations = CouchI18n::Translation.all(:page => params[:page], :per_page => per_page)
      end
    end
    @available_higher_offsets = CouchI18n::Translation.higher_keys_for_offset(params[:offset])
    @available_deeper_offsets = CouchI18n::Translation.deeper_keys_for_offset(params[:offset])
  end
end

#newObject



42
43
44
# File 'app/controllers/couch_i18n/translations_controller.rb', line 42

def new
  @translation = CouchI18n::Translation.new translation_key: params[:offset]
end

#showObject



38
39
40
# File 'app/controllers/couch_i18n/translations_controller.rb', line 38

def show
  redirect_to action: :edit
end

#updateObject

PUT /couch_i18n/translations/:id



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/couch_i18n/translations_controller.rb', line 64

def update
  @translation = CouchI18n::Translation.find(params[:id])
  @translation.translated = true
  tparams = translation_params
  if tparams["value"].present? && params[:is_json].present?
    tparams["value"] = JSON.parse(tparams["value"])
  end
  if @translation.update_attributes(tparams)
    redirect_to({:action => :index, :offset => @translation.translation_key.to_s.sub(/\.[\w\s-]+$/, '')}, :notice => I18n.t('couch_i18n.action.update.successful', :model => CouchI18n::Translation.model_name.human))
  else
    render :action => :edit
  end
end