Class: Air18nUiController

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

Instance Method Summary collapse

Instance Method Details

#create_translationObject

TODO(JKB) Move bulk of create_translation and normalize_translation_value to air18n. xhr



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/air18n_ui_controller.rb', line 41

def create_translation
  pt = Air18n::PhraseTranslation.new
  pt.user = @current_user

  pt.phrase_id = params[:phrase_id]
  pt.key = params[:phrase_key]
  pt.locale = params[:target_locale]

  # Let parameters specify "value", "phrase[value]", or "air18n_phrase[value]"
  # We prefer to take directly from request.request_parameters or
  # request.query_parameters because values in the params hash are sanitized
  # and HTML goes missing!!
  value = request.request_parameters['value'] ||
    request.query_parameters['value'] ||
    params[:value] ||
    (request.request_parameters['phrase'] && request.request_parameters['phrase']['value']) ||
    (request.query_parameters['phrase'] && request.query_parameters['phrase']['value']) ||
    (request.request_parameters['air18n_phrase'] && request.request_parameters['air18n_phrase']['value']) ||
    (request.query_parameters['air18n_phrase'] && request.query_parameters['air18n_phrase']['value'])

  latest = pt.phrase.latest_translation(pt.locale)

  pt.value = normalize_translation_value(value)
  normalized_latest_value = latest.present? && normalize_translation_value(latest.value)

  value_same_as_latest = pt.value == normalized_latest_value

  if latest && value_same_as_latest && latest.is_stale && @current_user.id == latest.user_id
    latest.is_stale = false
    if latest.save
      response_obj = {
        :status => 'success',
        :message => 'Translation marked as up-to-date.',
        :phrase_id => latest.phrase_id,
        :key => latest.key,
        :locale => latest.locale,
        :value => latest.value,
        :is_verification => latest.is_verification
      }
    else
      response_obj = { :status => 'error', :message => latest.errors.values.join('; ') }
    end
  else
    do_save = false
    message = ""

    safeness = Air18n::XssDetector.safe?(pt.phrase.value, pt.value, I18n.default_locale, pt.locale)

    if pt.value.empty?
      message = "Translation empty; nothing saved."
    elsif !params[:disable_xss_check] && !safeness[:safe]
      message = safeness[:reason]
    elsif (latest && value_same_as_latest) && (latest.user_id != 0)
      if !TungstenSupport::International.locale_in_qa_stage?(pt.locale)
        locale_name = TungstenSupport::International.decode_locale(pt.locale)
        allowed, error = false, "Verification of non-stale phrases is currently disabled in #{locale_name}"
      else
        allowed, error = pt.verification_allowed?(latest)
      end
      if allowed
        pt.is_verification = true
        do_save = true
        message = "Translation verified."
      else
        message = error
      end
    else
      do_save = true
      message = "Translation saved."
    end

    message = ERB::Util.html_escape(message)

    if do_save
      if pt.save
        response_obj = {
          :status => 'success',
          :message => message,
          :phrase_id => pt.phrase_id,
          :key => pt.key,
          :locale => pt.locale,
          :value => pt.value,
          :is_verification => pt.is_verification
        }
      else
        response_obj = {:status => 'error', :message => pt.errors.values.join('; ')}
      end
    else
      response_obj = {:status => 'error', :message => message}
    end
  end

  respond_to do |format|
    format.html do
      render :text => response_obj[:message]
    end

    format.json do 
      render :json => response_obj 
    end
  end
end

#only_allow_translatorsObject



4
5
6
7
8
# File 'app/controllers/air18n_ui_controller.rb', line 4

def only_allow_translators
  unless @current_user && @current_user.has_any_role?([:admin, :translator])
    redirect_to '/'
  end
end

#phraseObject



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/air18n_ui_controller.rb', line 10

def phrase
  # error checking
  if params[:mappings]
    params[:id] = params[:mappings].keys
  end

  phrases = Air18n::Phrase.find_all_by_id(params[:id])
  locale = params[:locale] || 'en'
  render :json => {:status => 'error'} and return if phrases.empty?

  json = []

  phrases.each do |phrase|
    latest = phrase.latest_translation(locale)
    latest = latest.nil? ? "" : latest.value

    json << {:status => 'success', 
             :id => phrase.id, 
             :key => phrase.key,
             :locale => locale, 
             :original => phrase.value,
             :elements => params[:mappings] ? params[:mappings][phrase.id.to_s] : [], 
             :latest => latest}
  end

  render :json => (json.length == 1 ? json.first : json)
end