Class: RailsI18nManager::TranslationsController

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

Instance Method Summary collapse

Instance Method Details

#delete_inactive_keysObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/rails_i18n_manager/translations_controller.rb', line 84

def delete_inactive_keys
  @translation_keys = TranslationKey
    .where(active: false)
    .includes(:translation_app, :translation_values)
    .references(:translation_app)

  apply_filters

  ids = translation_keys.pluck(:id)

  TranslationKey.where(id: ids).delete_all
  TranslationValue.where(translation_key_id: ids).delete_all
end

#destroyObject



75
76
77
78
79
80
81
82
# File 'app/controllers/rails_i18n_manager/translations_controller.rb', line 75

def destroy
  if @translation_key.active
    redirect_to translations_path, alert: "Cannot delete active translations"
  else
    @translation_key.destroy!
    redirect_to translations_path, notice: "Delete Successful"
  end
end

#editObject



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

def edit
end

#importObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/rails_i18n_manager/translations_controller.rb', line 98

def import
  @form = Forms::TranslationFileForm.new(params[:import_form])

  if request.get?
    render
  else
    if @form.valid?
      begin
        TranslationsImportJob.new.perform(
          translation_app_id: @form.translation_app_id,
          import_file: @form.file.path,
          overwrite_existing: @form.overwrite_existing,
          mark_inactive_translations: @form.mark_inactive_translations,
        )
      rescue TranslationsImportJob::ImportAbortedError => e
        flash.now.alert = e.message
        render
        return
      end

      redirect_to translations_path, notice: "Import Successful"
    else
      flash.now.alert = "Import not started due to form errors."
      render
    end
  end
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/rails_i18n_manager/translations_controller.rb', line 5

def index
  case params[:sort]
  when "app_name"
    sort = "#{TranslationApp.table_name}.name"
  when "updated_at"
    sort = "#{TranslationKey.table_name}.updated_at"
  else
    sort = params[:sort]
  end

  @translation_keys = TranslationKey
    .includes(:translation_app, :translation_values)
    .references(:translation_app)
    .sort_order(sort, params[:direction], base_sort_order: "#{TranslationApp.table_name}.name ASC, #{TranslationKey.table_name}.key ASC")

  apply_filters

  if request.format.to_sym != :html && TranslationApp.first.nil?
    request.format = :html
    flash[:alert] = "No Translation apps exists"
    redirect_to action: :index
    return false
  end

  respond_to do |format|
    format.html do
      @translation_keys = @translation_keys.page(params[:page])
    end

    format.any do
      @translations_keys = @translation_keys.where(active: true) ### Ensure exported keys are active for any exports

      case request.format.to_sym
      when :csv
        send_data @translation_keys.to_csv, filename: "translations.csv"
      when :zip
        file = @translation_keys.export_to(format: params[:export_format], zip: true, app_name: params[:app_name].presence)

        if file
          send_file file, filename: "translations-#{params[:export_format]}-#{params[:app_name].presence || "all-apps"}.zip"
        else
          flash[:alert] = "Sorry, Nothing to export"
          redirect_to action: :index
        end
      else
        raise ActionController::UnknownFormat
      end
    end
  end
end

#showObject



56
57
58
# File 'app/controllers/rails_i18n_manager/translations_controller.rb', line 56

def show
  render "edit"
end

#translate_missingObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/controllers/rails_i18n_manager/translations_controller.rb', line 126

def translate_missing
  @translation_keys = TranslationKey.includes(:translation_values)

  apply_filters

  translated_count = 0
  total_missing = 0

  if params[:app_name]
    app_locales = TranslationApp.find_by(name: params[:app_name]).additional_locales_array
  else
    @translation_keys = @translation_keys.includes(:translation_app)
  end

  ### Check & Translate for Every i18n key
  @translation_keys.each do |key_record|
    locales = (app_locales || key_record.translation_app.additional_locales_array)

    ### Filter to just google translate supported languages
    locales = (locales & GoogleTranslate::SUPPORTED_LOCALES) # intersection

    default_translation_text = key_record.default_translation

    next if default_translation_text.blank?

    locales.each do |locale|
      if locale == key_record.translation_app.default_locale
        next ### skip, we dont translate the default locale
      end

      val_record = key_record.translation_values.detect{|x| x.locale == locale.to_s }

      ### Translate Missing
      if val_record.nil? || val_record.translation.blank?
        total_missing += 1

        translated_text = GoogleTranslate.translate(
          default_translation_text,
          from: key_record.translation_app.default_locale,
          to: locale
        )

        if translated_text.present?
          if val_record.nil?
            val_record = key_record.translation_values.new(locale: locale)
          end

          val_record.assign_attributes(translation: translated_text)

          val_record.save!

          translated_count += 1
        end
      end
    end
  end

  if params[:translation_key_id]
    url = request.referrer || translation_path(params[:translation_key_id])
  else
    url = params.to_unsafe_h.merge(action: :index)
  end

  redirect_to url, notice: "Translated #{translated_count} of #{total_missing} total missing translations"
end

#updateObject



63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/rails_i18n_manager/translations_controller.rb', line 63

def update
  @translation_key.assign_attributes(allowed_params)

  if @translation_key.save
    flash[:notice] = "Update success."
    redirect_to edit_translation_path(@translation_key)
  else
    flash[:notice] = "Update failed."
    render "translations/edit"
  end
end