Class: Lit::CloudTranslationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/lit/cloud_translations_controller.rb

Instance Method Summary collapse

Instance Method Details

#showObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/lit/cloud_translations_controller.rb', line 5

def show
  params.delete(:from) if params[:from] == 'auto'
  @target_localization = Localization.find(params[:localization_id])
  @localization_key = @target_localization.localization_key
  if params[:from]
    @localization = @localization_key.localizations.joins(:locale)
                                     .find_by!(lit_locales: { locale: params[:from] })
  end
  opts =
    {
      # if :from was auto, translate from the target localization's
      # current text itself
      text: (@localization || @target_localization).value,
      from: params[:from],
      to: @target_localization.locale.locale
    }.compact
  @translated_text = Lit::CloudTranslation.translate(opts)
rescue Lit::CloudTranslation::TranslationError => e
  @error_message = "Translation failed. #{e.message}"
end