User:Kaldari/translate.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

//<nowiki>
if ( mw.config.get( 'wgNamespaceNumber' ) === 0 && mw.config.get( 'wgAction' ) === 'view' && $( 'table.translations' ).length ) {
	// Script depends on jQuery dialog widget
	mw.loader.using( 'jquery.ui', function() {
		// Construct object (to prevent namespace conflicts)
		translations = {

			displayProgress: function( message ) {
				$('#translationForm div').hide(); // remove everything else from the dialog box
				$('#translationForm').append ( $('<div style="text-align:center;margin:3em 0;"></div>').html( message+'<br/><img src="http://upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif" />' ) );
			},
			
			displayError: function( error ) {
				$('#translationForm div').hide(); // remove everything else from the dialog box
				$('#translationForm').append ( $('<div style="color:#990000;margin-top:0.4em;"></div>').html( 'Error: '+error ) );
			},
			
			editPage: function( summary, template ) {
				var editToken = mw.user.tokens.get( 'csrfToken' );
				if ( !editToken ) {
					translations.displayError( 'Could not retrieve edit token.' );
				} else {
					summary = "t+";
					$.ajax({
						url: wgScriptPath + '/api.php?',
						data: 'action=edit&title='+encodeURIComponent(mw.config.get('wgPageName'))+'&section=new&summary='+encodeURIComponent(summary)+'&text='+encodeURIComponent(template)+'&format=json&token='+encodeURIComponent(editToken),
						dataType: 'json',
						type: 'POST',
						success: function( data ) {
							if ( data.edit.result == "Success" ) {
								window.location.reload();
							} else {
								translations.displayError( 'Unknown result from API.' );
							}
						},
						error: function( xhr ) {
							translations.displayError( 'Edit failed.' );
							//console.debug( xhr.responseText );
						}
					});
				}
			},
			
			initialize: function() {
				// Define interface
				$translationDialog = $('<div id="translationForm"></div>')
					.append(
						$('<div style="margin-top:0.4em;"></div>')
							.html( 'Language code: ' )
							.append( $('<input type="text" name="lang" size="4" title="The two or three letter ISO 639 language code"/>') )
					)
					.append(
						$('<div style="margin-top:0.4em;"></div>')
							.html( 'Translation: ' )
							.append( $('<input type="text" name="word" size="25"/>') )
					)
					.dialog({
						width: 500,
						autoOpen: false,
						title: 'Add a translation',
						modal: false,
						buttons: { "Add translation": function() {
							$(this).dialog({buttons:{}});
							// Perform edit to page
							translations.displayProgress( 'Adding translation to page...' );
							var message = $('#wordMessage').val();
							var template = '{{subst:'+$('#languageSelect').val()+'|'+ message +'}}';
							translations.editPage( message, template );
						}}
					});
		
				// Insert new button into page
				var addButton = '<a href="#" onclick="$translationDialog.dialog(\'open\');return false;">Add a translation</a>';
				$( 'table.translations:not([data-gloss="Translations to be checked"]' ).after( addButton );

			} // close initialize function

		}; // close translations object

		// Remove old translation adding interface if present
		//$( 'table.translations:not([data-gloss="Translations to be checked"]) tr:last-child' ).remove();

		$( document ).ready( translations.initialize );
	} ); // close mw.loader
} // close if
//</nowiki>