summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/resources/js/ext.translate.storage.js
blob: d2fd5fe9ca78dd09e5aae71eb47e13ac9650d315 (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
( function ( mw ) {
	'use strict';

	/**
	 * This class can save a translation into MediaWiki pages using the
	 * MediaWiki edit WebApi.
	 *
	 * @since 2013.10
	 */
	var TranslationApiStorage = function () {
		// No-op for now. Could take api module as param for example.
	};

	TranslationApiStorage.prototype = {
		/**
		 * Save the translation.
		 *
		 * @param {string} title The title of the page including language code
		 *   to store the translation.
		 * @param {string} translation The translation of the message
		 * @return {jQuery.Promise}
		 */
		save: function ( title, translation ) {
			var api = new mw.Api();

			// Change to csrf when support for MW 1.25 is dropped
			return api.postWithToken( 'edit', {
				action: 'edit',
				title: title,
				text: translation,
				// If the session expires, fail the saving instead of saving it
				// as an anonymous user (if anonymous can save).
				// When undefined, the parameter is not included in the request
				assert: mw.user.isAnon() ? undefined : 'user'
			} );
		}
	};

	mw.translate = mw.translate || {};
	mw.translate.TranslationApiStorage = TranslationApiStorage;
}( mediaWiki ) );