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 params[:translation]
  if @translation.value.present? && params[:is_json].present?
    @translation.value = JSON.parse(@translation.value)
  end
  if @translation.save
    redirect_to({:action => :index, :offset => @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



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

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.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?…



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

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.translation.offset deleted', :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



87
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
# File 'app/controllers/couch_i18n/translations_controller.rb', line 87

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.key, s.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.key => s.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



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

def import
  redirect_to({:action => :index, :offset => params[:offset]}, :alert => I18n.t('couch_i18n.translation.no import 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.translation.cannot parse yaml')) and return unless hash
    CouchI18n.traverse_flatten_keys(hash).each do |key, value|
      existing = CouchI18n::Translation.find_by_key(key)
      if existing
        if existing.value != value
          existing.value = value
          existing.translated = true
          existing.save
        end
      else
        CouchI18n::Translation.create :key => key, :value => value
      end
    end 
  else
    redirect_to({:action => :index, :offset => params[:offset]}, :alert => I18n.t('couch_i18n.translation.no proper import extension', :extension => extension)) and return 
  end
  redirect_to({:action => :index, :offset => params[:offset]}, :notice => I18n.t('couch_i18n.translation.file imported', :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 :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
# File 'app/controllers/couch_i18n/translations_controller.rb', line 64

def update
  @translation = CouchI18n::Translation.find(params[:id])
  @translation.translated = true
  if params[:translation]["value"].present? && params[:is_json].present?
    params[:translation]["value"] = JSON.parse(params[:translation]["value"])
  end
  if @translation.update_attributes(params[:translation])
    redirect_to({:action => :index, :offset => @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