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]
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
|