Jump to content

Extension:Google Translator

From mediawiki.org
MediaWiki extensions manual
Google Translator
Release status: stable
Implementation Skin
Description Adds Google Translator box to the sidebar
Author(s) Joachim De Schrijver (joa_dstalk)
Latest version 0.3 (2018-09-17)
MediaWiki 1.30+
License GNU Lesser General Public License 3.0 or later
Download see below
Example MountWiki.com
  • $wgGoogleTranslatorOriginal
  • $wgGoogleTranslatorLanguages

The Google Translator extension enables easy addition of a Google Translation box to a MediaWiki instance. See screenshot and Installation for details. This extension requires no Google account. This extension is based on the Google AdSense 2 extension.

Installation

[edit]
  • Download the extension (copy paste the 4 files from below)
  • Store the files in the extensions/GoogleTranslator directory
  • Add the following line to your LocalSettings.php:
require_once "$IP/extensions/GoogleTranslator/GoogleTranslator.php";
  • Default settings are to translate from English to German and French. These default values can be changed by adding the following lines in LocalSettings.php
$wgGoogleTranslatorOriginal  = Original language;          
$wgGoogleTranslatorLanguages  = Languages to be included in the translation dropdown box;
  • Leave the list of languages between the single quotes blank to make all languages available. Example settings
$wgGoogleTranslatorOriginal  = 'en';          
$wgGoogleTranslatorLanguages  = 'nl,fr,de';
  • The description (label) of the portlet can be changed in MediaWiki:Googletranslator. Defaults to "Translate".
  • When used in combination with another plugin that adds boxes to the sidebar, the order of calling the extension in the LocalSettings.php will determine the order of the extension boxes in the sidebar.
  • If the "Powered by Google Translate" spills into your page (MW 1.18.0), add the following to your MediaWiki:Common.css and force wrapping
a.goog-logo-link img {
    display: none;
}
  • If you want to hide the link that leads to the official page of google translator page, add the following to your MediaWiki:Common.css and force wrapping
a.goog-logo-link {
	display:none;
}


  • At default the extension box will show at the end of the sidebar. You can place the box at any place by edit your MediaWiki:Sidebar page, insert
* googletranslator

at the position you want.
Example (box will appear at top):

* googletranslator
* navigation
** mainpage|mainpage-description
** portal-url|portal
** currentevents-url|currentevents
** recentchanges-url|recentchanges
** randompage-url|randompage
** helppage|help
* SEARCH
* TOOLBOX
* LANGUAGES

Code

[edit]

GoogleTranslator.php

[edit]
<?php
/**
 * MediaWiki extension to add Google Translator in a portlet in the sidebar.
 * Installation instructions can be found on
 * http://www.mediawiki.org/wiki/Extension:Google_Translator
 *
 * This extension will not add the Google Translator portlet to *any* skin
 * that is used with MediaWiki. Because of inconsistencies in the skin
 * implementation, it will not be add to the following skins:
 * cologneblue, standard, nostalgia
 *
 * @addtogroup Extensions
 * @author Joachim De Schrijver
 * @license LGPL
 *
 * Loosely based on the Google AdSense extension by Siebrand Mazeland
 */

/**
 * Exit if called outside of MediaWiki
 */
if( !defined( 'MEDIAWIKI' ) ) {
	echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
	die( 1 );
}

/**
 * SETTINGS
 * --------
 * The description of the portlet can be changed in [[MediaWiki:Googletranslator]].
 *
 * The following variables may need to be reset in your LocalSettings.php.
  */
$wgGoogleTranslatorOriginal  = $wgLanguageCode; // Original languages of the page that needs translation
$wgGoogleTranslatorLanguages  = 'fr,de';        // Languages included in the translating box

$wgExtensionCredits['other'][] = array(
	'name'           => 'Google Translator',
	'version'        => '0.3',
	'author'         => 'Joachim De Schrijver',
	'description'    => 'Adds [https://translate.google.com Google Translator] to the sidebar',
	'descriptionmsg' => 'googletranslator-desc',
	'url'            => 'https://www.mediawiki.org/wiki/Extension:Google_Translator',
);

// Register class and localisations
$dir = dirname(__FILE__) . '/';
$wgAutoloadClasses['GoogleTranslator'] = $dir . 'GoogleTranslator.class.php';
$wgExtensionMessagesFiles['GoogleTranslator'] = $dir . 'GoogleTranslator.i18n.php';

// Hook to modify the sidebar
$wgHooks['SkinBuildSidebar'][] = 'GoogleTranslator::GoogleTranslatorInSidebar';

//js footer
$wgHooks['SkinAfterContent'][] = function(&$data, $skin) {
	global $wgGoogleTranslatorOriginal,$wgGoogleTranslatorLanguages;
	$data .= 
			"
	<script>
			function googleTranslateElementInit() {
				new google.translate.TranslateElement({
					pageLanguage: '".$wgGoogleTranslatorOriginal."',
					includedLanguages: '".$wgGoogleTranslatorLanguages."'
				}, 'google_translate_element');
			}
	</script>
	<script src=\"//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit\"></script>".
			'';
};

GoogleTranslator.class.php

[edit]
<?php
if (!defined('MEDIAWIKI')) die();
/**
 * Class file for the GoogleTranslator extension
 *
 * @addtogroup Extensions
 * @author Joachim De Schrijver
 * @license LGPL
 */
class GoogleTranslator {
	static function GoogleTranslatorInSidebar( $skin, &$bar ) {
		$bar[ 'googletranslator' ] = array(
			array(
				'html' => '<div id="google_translate_element"></div>',
			),
		);
		return true;
	}
}

GoogleTranslator.css

[edit]
/**
* Stylesheet for GoogleTranslator extension.
*
* This should be added to [[MediaWiki:Common.css]] page when installing this
* extension and should not be modified. You may modify the CSS code on the
* MediaWiki:Common.css page to adjust padding, alignment, etc.
*
* @addtogroup Extensions
*/

/* Pad Google Translator box in portlet in sidebar */
#p-googleatranslator .pBody {
    padding-top: 5px;
    text-align:  center;
}

GoogleTranslator.i18n.php

[edit]
<?php
/**
 * Internationalisation file for extension GoogleTranslator
 *
 * @addtogroup Extensions
 * @license LGPL
 */

$messages = array();

/** English
 * @author Joachim De Schrijver
 */
$messages['en'] = array(
	'googletranslator'      => 'Translate', # do not translate or duplicate this message to other languages
	'googletranslator-desc' => 'Adds [https://www.google.com/translate Google Translator] to the sidebar',
);

/** Message documentation (Message documentation)
 * @author Joachim De Schrijver
 */
$messages['qqq'] = array(
	'googletranslator-desc' => 'Short description of this extension, shown on [[Special:Version]]. Do not translate or change links.',
);

/** German
 * @author Karsten Hoffmeyer
 */
$messages['de'] = array(
	'googletranslator-desc' => 'Ermöglicht das Nutzen von [https://www.google.com/translate Google Translator] in der Seitenleiste',
);

/** French
 * @author Pierre Mavro
 */
$messages['fr'] = array(
	'googletranslator-desc' => 'Ajoute [https://www.google.com/translate Google Traduction] dans la bare latérale',
);

/** Russian
 * @author Michael P Dubner
 */
$messages['ru'] = array(
	'googletranslator-desc' => 'Добавляет [https://www.google.com/translate Google Переводчик] в блок навигации.',
);

Version history

[edit]
0.1 (2010-06-17)

Initial version

0.2 (2014-12-15)
  • Removed call to wfLoadExtensionMessages; this adds support for MediaWiki versions >= 1.21.1, but removes support for MediaWiki 1.15.
  • Retrieves the Google Translate script using a protocol-relative URL for better compatibility with wikis using HTTPS.
0.3 (2018-09-17)
  • Adpated to last version 1.30

See also

[edit]