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
|
( function ( $, mw ) {
'use strict';
mw.translate = mw.translate || {};
mw.translate = $.extend( mw.translate, {
dirty: false,
// A cache for language stats loaded from API,
// indexed by language code
languagestats: {},
/**
* Checks if the input placeholder attribute
* is supported on this element in this browser.
*
* @param {jQuery} $element
* @return {boolean}
*/
isPlaceholderSupported: function ( $element ) {
return ( 'placeholder' in $element[ 0 ] );
},
// Storage for language stats loader functions from API,
// indexed by language code
languageStatsLoader: {},
/**
* Get language stats for a language from the API.
*
* @param {string} language Language code.
* @return {deferred}
*/
loadLanguageStats: function ( language ) {
if ( !mw.translate.languageStatsLoader[ language ] ) {
mw.translate.languageStatsLoader[ language ] = new mw.Api().get( {
action: 'query',
meta: 'languagestats',
lslanguage: language
} );
}
mw.translate.languageStatsLoader[ language ].done( function ( result ) {
mw.translate.languagestats[ language ] = result.query.languagestats;
} );
return mw.translate.languageStatsLoader[ language ];
},
/**
* Load message group information asynchronously.
*
* @param {string} id Message group id
* @param {string|Array} [props] List of properties to load
* @return {jQuery.Promise} Object containing the requested properties on success.
*/
getMessageGroup: function ( id, props ) {
var params, api;
if ( Array.isArray( props ) ) {
props = props.join( '|' );
} else if ( props === undefined ) {
props = 'id|label|description|icon|priority|prioritylangs|priorityforce|workflowstates';
}
params = {
meta: 'messagegroups',
mgformat: 'flat',
mgprop: props,
mgroot: id
};
api = new mw.Api();
return api.get( params ).then( function ( result ) {
return result.query.messagegroups[ 0 ];
} );
},
/**
* Find a group from an array of message groups as returned by web api
* and recurse it through sub groups.
*
* @param {string} id Group id to search for.
* @param {Array} groups Array of message grous
* @return {Object} Message group object
*/
findGroup: function ( id, groups ) {
var result = null;
if ( !id ) {
return groups;
}
$.each( groups, function ( index, group ) {
if ( group.id === id ) {
result = group;
return false;
}
if ( group.groups ) {
group = mw.translate.findGroup( id, group.groups );
if ( group ) {
result = group;
return false;
}
}
} );
return result;
},
/**
* Check if the current user is allowed to translate on this wiki.
*
* @return {boolean}
*/
canTranslate: function () {
return mw.config.get( 'TranslateRight' );
},
/**
* Check if the current user is allowed to proofread on this wiki.
*
* @return {boolean}
*/
canProofread: function () {
return mw.config.get( 'TranslateMessageReviewRight' );
},
/**
* Check if the current user can delete translations on this wiki.
*
* @return {boolean}
*/
canDelete: function () {
return mw.config.get( 'DeleteRight' ) && mw.config.get( 'TranslateRight' );
},
/**
* Adds missing languages to the language database so that they can be used in ULS.
*
* @param {Object} languages Language tags mapped to language names
* @param {Array} regions Which regions to add the languages.
*/
addExtraLanguagesToLanguageData: function ( languages, regions ) {
var code;
for ( code in languages ) {
if ( code in $.uls.data.languages ) {
continue;
}
$.uls.data.addLanguage( code, {
script: 'Zyyy',
regions: regions,
autonym: languages[ code ]
} );
}
},
isDirty: function () {
return $( '.mw-ajax-dialog:visible' ).length || // For old Translate
// For new Translate, something being typed in the current editor.
mw.translate.dirty ||
// For new translate, previous editors has some unsaved edits
$( '.tux-status-unsaved' ).length;
}
} );
function pageShowHandler() {
$( window ).on( 'beforeunload.translate', function () {
if ( mw.translate.isDirty() ) {
// Return our message
return mw.msg( 'translate-js-support-unsaved-warning' );
}
} );
}
/**
* A warning to be shown if a user tries to close the page or navigate away
* from it without saving the written translation.
*/
function translateOnBeforeUnloadRegister() {
pageShowHandler();
$( window ).on( 'pageshow.translate', pageShowHandler );
}
$( function () {
translateOnBeforeUnloadRegister();
} );
}( jQuery, mediaWiki ) );
|