summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/resources/js/ext.translate.special.translationstash.js
blob: 8ceca653acd8fe81488d330c2c420274cacfbcd1 (plain)
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
/*!
 * TranslationStash front-end logic.
 *
 * @author Santhosh Thottingal
 * @license GPL-2.0-or-later
 * @since 2013.10
 */

( function () {
	'use strict';

	var userTranslations = {},
		translationStashStorage = new mw.translate.TranslationStashStorage();

	mw.translate.canTranslate = function () {
		// At this page, the new translator can translate
		return true;
	};

	function getMessages( messageGroup, language, offset, limit ) {
		var deferred = new mw.Api().get( {
			action: 'query',
			list: 'messagecollection',
			mcgroup: messageGroup,
			mclanguage: language,
			mcoffset: offset,
			mclimit: limit,
			mcprop: 'definition'
		} );

		return deferred.promise();
	}

	function addMessage( message ) {
		var $messageWrapper, $message,
			$messageTable = $( '.tux-messagelist' ),
			sourceLanguage = $messageTable.data( 'sourcelangcode' ),
			sourceLanguageDir = $.uls.data.getDir( sourceLanguage ),
			targetLanguage = $messageTable.data( 'targetlangcode' ),
			targetLanguageDir = $.uls.data.getDir( targetLanguage ),
			status = message.properties.status,
			statusClass = 'tux-status-' + status,
			statusMsg;

		if ( status === 'translated' ) {
			// tux-status-translated
			statusMsg = 'tux-status-' + status;
		}

		$messageWrapper = $( '<div>' )
			.addClass( 'row tux-message' );

		$message = $( '<div>' )
			.addClass( 'row message tux-message-item ' + status )
			.append(
				$( '<div>' )
					.addClass( 'eight columns tux-list-message' )
					.append(
						$( '<span>' )
							.addClass( 'tux-list-source' )
							.attr( {
								lang: sourceLanguage,
								dir: sourceLanguageDir
							} )
							.text( message.definition ),
						// Bidirectional isolation.
						// This should be removed some day when proper
						// unicode-bidi: isolate
						// is supported everywhere
						$( '<span>' )
							.html( $( 'body' ).hasClass( 'rtl' ) ? '&rlm;' : '&lrm;' ),
						$( '<span>' )
							.addClass( 'tux-list-translation' )
							.attr( {
								lang: targetLanguage,
								dir: targetLanguageDir
							} )
							.text( message.translation || '' )
					),
				$( '<div>' )
					.addClass( 'two columns tux-list-status text-center' )
					.append(
						$( '<span>' )
							.addClass( statusClass )
							.text( statusMsg ? mw.msg( statusMsg ) : '' )
					),
				$( '<div>' )
					.addClass( 'two column tux-list-edit text-right' )
					.append(
						$( '<a>' )
							.attr( {
								title: mw.msg( 'translate-edit-title', message.key )
							} )
							.text( mw.msg( 'tux-edit' ) )
					)
			);

		$messageWrapper.append( $message );
		$messageTable.append( $messageWrapper );
		// Attach translate editor to the message
		$messageWrapper.translateeditor( {
			message: message,
			storage: translationStashStorage,
			onSave: updateStats,
			onSkip: function () {
				var $next = this.$editTrigger.next( '.tux-message' );

				// If there is text in the skipped message, avoid showing the
				// regular "you have unsaved messages" when navigating away,
				// because there is no way to get back to these messages.
				this.markUnunsaved();

				// This can happen when it's
				// the last message in the translation stash
				if ( !$next.length ) {
					// Reload the page to get more messages
					// when we get to the last one
					window.location.reload();
				}
			},
			onReady: function () {
				this.$editor.find( '.tux-editor-skip-button' )
					.text( mw.msg( 'translate-translationstash-skip-button-label' ) );
			}
		} );
	}

	/**
	 * Updates the translation count at the top of the message list and
	 * displays warning when translation limit has been reached.
	 * Relies on classes stash-stats and tux-status-translated.
	 */
	function updateStats() {
		var count,
			$target = $( '.stash-stats' );

		count = $( '.tux-status-translated' ).length;
		if ( count === 0 ) {
			return;
		}

		$target.text( mw.msg(
			'translate-translationstash-translations',
			mw.language.convertNumber( count )
		) );

		if ( count >= mw.config.get( 'wgTranslateSandboxLimit' ) ) {
			// Remove the untranslated message to disallow translation beyond the limit
			$( '.tux-message' ).has( '.untranslated' ).remove();

			// Show a message telling that the limit was reached
			$( '.limit-reached' )
				.empty()
				.append( $( '<h1>' ).text( mw.msg( 'tsb-limit-reached-title' ) ) )
				.append( $( '<p>' ).text( mw.msg( 'tsb-limit-reached-body' ) ) )
				.removeClass( 'hide' );
		}
	}

	function loadMessages() {
		var $messageTable = $( '.tux-messagelist' ),
			messagegroup = '!sandbox';

		$( '<div>' )
			.addClass( 'tux-loading-indicator' )
			.appendTo( $messageTable );

		getMessages( messagegroup, $messageTable.data( 'targetlangcode' ) )
			.done( function ( result ) {
				var untranslated, messages = result.query.messagecollection;

				$messageTable.empty();
				$.each( messages, function ( index, message ) {
					message.properties = {};
					message.properties.status = 'untranslated';

					message.group = messagegroup;
					if ( userTranslations[ message.title ] ) {
						message.translation = userTranslations[ message.title ].translation;
						message.properties.status = 'translated';
					}

					addMessage( message );
				} );

				// Show the editor for the first untranslated message.
				untranslated = $( '.tux-message' )
					.has( '.tux-message-item.untranslated' )
					.first();
				if ( untranslated.length ) {
					untranslated.data( 'translateeditor' ).show();
				}

				updateStats();
			} ).fail( function ( errorCode, response ) {
				$messageTable.empty().addClass( 'error' )
					.text( 'Error: ' + errorCode + ' - ' +
						( response.error && response.error.info || 'Unknown error' )
					);
			} );
	}

	$( function () {
		var $messageTable = $( '.tux-messagelist' ),
			$ulsTrigger = $( '.ext-translate-language-selector > .uls' );

		// Some links in helpers will navigate away by default. But since the messages
		// will change on this page on every load, we want to avoid that. Force the
		// links to open on new window/tab.
		mw.hook( 'mw.translate.editor.showTranslationHelpers' ).add( function ( helpers, $editor ) {
			$editor.find( 'a' ).prop( 'target', '_blank' );
		} );

		$ulsTrigger.uls( {
			ulsPurpose: 'translate-special-translationstash',
			onSelect: function ( language ) {
				var direction = $.uls.data.getDir( language ),
					autonym = $.uls.data.getAutonym( language );

				$ulsTrigger
					.text( autonym )
					.attr( {
						lang: language,
						dir: direction
					} );

				$messageTable
					.empty()
					.data( {
						targetlangcode: language,
						targetlangdir: direction
					} );

				loadMessages();
			}
		} );
		// Get the user translations if any(possibly from an early attempt)
		// and new messages to try.
		translationStashStorage.getUserTranslations()
			.done( function ( translations ) {
				if ( translations.translationstash.translations ) {
					$.each( translations.translationstash.translations,
						function ( index, translation ) {
							userTranslations[ translation.title ] = translation;
						} );
				}
				loadMessages();
			} );
	} );
}() );