1
2
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
37
38
39
40
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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
( function () {
'use strict';
/**
* Proofread Plugin
*
* Prepare a proofread UI with all the required actions
* for a translation unit (message).
* This is mainly used with the messagetable plugin in proofread mode,
* but it is independent of messagetable.
* Example usage:
*
* $( 'div.proofread' ).proofread( {
* message: messageObject, // Mandatory message object
* sourcelangcode: 'en', // Mandatory source language code
* targetlangcode: 'hi' // Mandatory target language code
* } );
*
* @param {Element} element
* @param {Object} options
* @param {Object} options.message
* @param {string} options.sourcelangcode Language code.
* @param {string} options.targetlangcode Language code.
*/
function Proofread( element, options ) {
this.$message = $( element );
this.options = options;
this.message = this.options.message;
this.init();
this.listen();
}
Proofread.prototype = {
/**
* Initialize the plugin
*/
init: function () {
var that = this;
this.render();
// No review before translating.
if ( !this.message.translation ) {
this.disableProofread();
}
// No review for fuzzy messages.
if ( this.message.properties.status === 'fuzzy' ) {
this.disableProofread();
}
if ( !mw.translate.canProofread() ) {
this.disableProofread();
}
this.$message.translateeditor( {
message: this.message,
onSave: function ( translation ) {
that.$message.find( '.tux-proofread-translation' )
.text( translation );
that.message.translation = translation;
that.markSelfTranslation();
// `status` class is documented elsewhere
// eslint-disable-next-line mediawiki/class-doc
that.$message.find( '.tux-proofread-status' )
.removeClass( 'translated fuzzy proofread untranslated' )
.addClass( that.message.properties.status );
}
} );
},
render: function () {
// List of all reviewers
var reviewers = this.message.properties.reviewers || [];
// The id of the current user, converted to string as the are in reviewers
var userId = String( mw.config.get( 'wgUserId' ) );
// List of all reviewers excluding the current user.
var otherReviewers = reviewers.filter( function ( element ) {
return element !== userId;
} );
/* Whether the current user if the last translator of this message.
* Accepting own translations is prohibited. */
var translatedBySelf = ( this.message.properties[ 'last-translator-text' ] === mw.user.getName() );
var proofreadBySelf = reviewers.indexOf( userId ) > -1;
var sourceLangDir = $.uls.data.getDir( this.options.sourcelangcode );
// `status` class is documented elsewhere
// eslint-disable-next-line mediawiki/class-doc
var $proofreadAction = $( '<div>' )
.attr( 'title', mw.msg( 'tux-proofread-action-tooltip' ) )
.addClass(
'tux-proofread-action ' + this.message.properties.status + ' ' + ( proofreadBySelf ? 'accepted' : '' )
);
var $proofreadEdit = $( '<div>' )
.addClass( 'tux-proofread-edit' )
.append( $( '<span>' )
.addClass( 'tux-proofread-edit-label hide' )
.text( mw.msg( 'tux-proofread-edit-label' ) )
)
.on( 'mouseover', function () {
$( this ).find( '.tux-proofread-edit-label' ).removeClass( 'hide' );
} )
.on( 'mouseout', function () {
$( this ).find( '.tux-proofread-edit-label' ).addClass( 'hide' );
} );
var targetLangAttrib;
if ( this.options.targetlangcode === mw.config.get( 'wgTranslateDocumentationLanguageCode' ) ) {
targetLangAttrib = mw.config.get( 'wgContentLanguage' );
} else {
targetLangAttrib = this.options.targetlangcode;
}
var targetLangDir = $.uls.data.getDir( targetLangAttrib );
// `status` class is documented elsewhere
// eslint-disable-next-line mediawiki/class-doc
this.$message.append(
$( '<div>' )
.addClass( 'row tux-message-item-compact message' )
.append(
// `status` class is documented elsewhere
// eslint-disable-next-line mediawiki/class-doc
$( '<div>' )
.addClass( 'one column tux-proofread-status ' + this.message.properties.status ),
$( '<div>' )
.addClass( 'five columns tux-proofread-source' )
.attr( {
lang: this.options.sourcelangcode,
dir: sourceLangDir
} )
.text( this.message.definition ),
$( '<div>' )
.addClass( 'five columns tux-proofread-translation' )
.attr( {
lang: targetLangAttrib,
dir: targetLangDir
} )
.text( this.message.translation || '' ),
$( '<div>' )
.addClass( 'tux-proofread-action-block one column' )
.append(
$proofreadAction,
otherReviewers.length ?
$( '<div>' )
.addClass( 'tux-proofread-count' )
.data( 'reviewCount', reviewers.length ) // To update when accepting
.text( mw.language.convertNumber( reviewers.length ) ) :
$( [] ),
$proofreadEdit
)
)
).addClass( this.message.properties.status );
if ( !translatedBySelf && !proofreadBySelf ) {
// This will get removed later if any of various other reasons prevent it
this.message.proofreadable = true;
this.message.proofreadAction = this.proofread.bind( this );
}
if ( translatedBySelf ) {
this.markSelfTranslation();
}
/* Here we need to check that there are reviewers in the first place
* before adding review markers */
if ( reviewers.length && otherReviewers.length ) {
this.$message.addClass( 'proofread-by-others' );
}
},
disableProofread: function () {
this.message.proofreadable = false;
this.$message.find( '.tux-proofread-action' )
.remove();
},
/**
* Mark the message self translated.
*/
markSelfTranslation: function () {
// Own translations cannot be reviewed, so disable proofread
this.disableProofread();
if ( !this.$message.hasClass( 'own-translation' ) ) {
this.$message.addClass( 'own-translation' )
.find( '.tux-proofread-action-block' )
.append( $( '<div>' )
.addClass( 'translated-by-self' )
.attr( 'title', mw.msg( 'tux-proofread-translated-by-self' ) )
);
}
},
/**
* Mark this message as proofread.
*/
proofread: function () {
var message = this.message,
$message = this.$message,
api = new mw.Api();
var params = {
action: 'translationreview',
revision: this.message.properties.revision
};
if ( !mw.user.isAnon() ) {
params.assert = 'user';
}
api.postWithToken( 'csrf', params ).done( function () {
$message.find( '.tux-proofread-action' )
.removeClass( 'tux-notice' ) // in case, it failed previously
.addClass( 'accepted' );
var $counter = $message.find( '.tux-proofread-count' );
var reviews = $counter.data( 'reviewCount' );
$counter.text( mw.language.convertNumber( reviews + 1 ) );
// Update stats
$( '.tux-action-bar .tux-statsbar' ).trigger(
'change',
[ 'proofread', message.properties.status ]
);
message.properties.status = 'proofread';
if ( mw.track ) {
mw.track( 'ext.translate.event.proofread', message );
}
} ).fail( function ( errorCode ) {
$message.find( '.tux-proofread-action' ).addClass( 'tux-notice' );
if ( errorCode === 'assertuserfailed' ) {
// eslint-disable-next-line no-alert
alert( mw.msg( 'tux-session-expired' ) );
}
} );
},
/**
* Attach event listeners
*/
listen: function () {
var that = this;
this.$message.find( '.tux-proofread-action' ).on( 'click', function () {
that.proofread();
return false;
} );
this.$message.find( '.tux-proofread-edit' ).on( 'click', function () {
// Make sure that the tooltip is hidden when going to the editor
$( '.translate-tooltip' ).remove();
that.$message.data( 'translateeditor' ).show();
return false;
} );
}
};
/*
* proofread PLUGIN DEFINITION
*/
$.fn.proofread = function ( options ) {
return this.each( function () {
var $this = $( this ),
data = $this.data( 'proofread' );
if ( !data ) {
$this.data( 'proofread', new Proofread( this, options ) );
}
} );
};
$.fn.proofread.Constructor = Proofread;
}() );
|