spedas / bleeding_edge

IDL-based Space Physics Environment Data Analysis Software (bleeding edge)
http://www.spedas.org
Other
7 stars 0 forks source link

Add Google Translate capability to SPEDAS wiki #137

Closed jameswilburlewis closed 6 months ago

jameswilburlewis commented 7 months ago

Users visiting with the Chrome browser have a browser tool that will automatically run any web page through Google Translate. But for other browsers, we should have a link or a button on each wiki page to "see this page in Google Translate".

It's fairly straightforward to take a URL for any wiki page and rearrange it to a Google Translate URL. This can be done by modifying a couple of PHP files to add javascript snippets to generate the Google Translate link and embed it in the page.

Another approach is to add tools to the wiki to facilitate content translation -- that is, the translated pages become a permanent part of the wiki, rather than being machine translated on the fly. This would let us recruit users fluent in other languages, and give them edit access to the wiki so they can use the translation extensions to create new pages translated from the original wiki content.

jameswilburlewis commented 7 months ago

NOTE: This didn't work, it was targeted to WikiMedia version 1.35, and the skinning interface has changed since then.

Google's Bard AI tool had some suggestions about adding google translate links to the wiki:

You can achieve this functionality without writing a full extension by using a combination of MediaWiki's built-in features and a custom JavaScript snippet:

  1. Create a custom sidebar link:

Go to your MediaWiki installation's LocalSettings.php file. Add the following PHP code to create a new sidebar link:

wfLoadExtension( 'Skin' );
$wgSkinExtensionFunctions[] = 'efTranslateSidebarLink';

function efTranslateSidebarLink() {
    global $wgOut;
    $wgOut->addHTML( '<li id="t-link"><a href="#">Translate this page</a></li>' );
}
  1. Add JavaScript code:

Edit your wiki's MediaWiki:Common.js file (or create it if it doesn't exist). Paste the following JavaScript code into the file:

// Select the sidebar link
var translateLink = document.getElementById( "t-link" );

// Function to generate the Google Translate link
function createTranslateLink() {
    var url = window.location.href;
    var translateUrl = "https://translate.google.com/translate?u=" + encodeURIComponent(url);
    translateLink.href = translateUrl;
}

// Call the function when the page loads
createTranslateLink();
  1. Save and refresh:

Save the changes to both LocalSettings.php and MediaWiki:Common.js. Clear your browser cache and refresh your wiki pages. Now, you should see a "Translate this page" link in the sidebar of each page. When clicked, it will open the current page in Google Translate.

Note from jwl: The translate.google.com link may or may not be correct. I couldn't get it working except for the top level wiki URL. (Maybe I need to URL-encode the page title string or something?). However, if you go to https://translate.google.com/translate?u=https://spedas.org/wiki , it does appear that you can navigate through the site, although the language keeps getting reset to English. When you go to translate.google.com, you get redirected to a link like this:

https://spedas-org.translate.goog/wiki/index.php?title=Main_Page"&_x_tr_sch=http&_x_tr_sl=auto&_x_tr_tl=en&_x_tr_hl=en-US

So spedas.org transforms to "spedas-org.translate.goog", then the rest of the wiki URL, then "&_x_tr_sch=http&_x_tr_sl=auto&_x_tr_tl=en&_x_tr_hl=en-US" gets tacked on at the end (and specifies the source and target languages)

jameswilburlewis commented 7 months ago

Mediawiki content translation tool: https://www.mediawiki.org/wiki/Content_translation

But this only seems to be on wikipedia, not for general use in other wikis.

jameswilburlewis commented 6 months ago

Here's ChatGPT's suggested addition to LocalSettings.php. It seems to work! If you navigate to a different page, the target language gets reset to English. ChatGPT had some ideas about adding a persistent language selection, but it was quite a bit more complicated....it might be better to try one of the suggested extensions if we want this functionality.

Conversation is here: https://chat.openai.com/share/a9c98f7d-02d3-4bca-b890-7cf50eb11622

This change is live on the development version of the wiki: https://spedas.org/wiki_dev

$wgHooks['SkinBuildSidebar'][] = function($skin, &$bar) {
    $title = $skin->getTitle();
    $url = $title->getFullURL();
    $translateUrl = "https://translate.google.com/translate?hl=&sl=auto&tl=en&u=" . urlencode($url);
    $bar['navigation'][] = array(
        'text'   => 'Translate this page',
        'href'   => $translateUrl,
        'id'     => 'n-translate',
        'active' => false
    );
    return true;
};
jameswilburlewis commented 6 months ago

I've added the MediaWiki Language Extension Bundle (https://www.mediawiki.org/wiki/MediaWiki_Language_Extension_Bundle) to spedas.org/wiki_dev . This looks like a very nice solution to creating translated versions of our Wiki pages (that is, human-assisted translations which become a permanent part of the site, as opposed to running pages through Google Translate on the fly). When you mark a page for translation, it adds some markup to assist in translating those elements to the target language. Wiki users can declare their level of fluency in various languages, set up watch lists for pages that need translation, etc.

This feature might be a bit memory-hungry. I had also enabled the VisualEditor extension, and with that plus the language extensions enabled, I started seeing some problems (e.g. Special:SpecialPages showing a blank page) that turned out to be due to PHP memory limits....I had to change the max_memory setting from 32M to unlimited to get everything working together.

jameswilburlewis commented 6 months ago

Translation tools are now live on the production wiki! In the sidebar, there is now an "Open this page in Google Translate". This takes you to a Google Translate page where you can select a target language, view the wiki content in that language, and follow the navigation links. (Clicking a link will take you to the English version of the new page, so you'll have to select the target language again).

The tools for translators are also installed. I created a "Translation Sandbox" page, which is disconnected from the rest of the wiki, to let us play with translations without messing up any important content. I picked a few target languages for the sandbox page, and went through the process of creating a Brazilian Portuguese translation (with an assist from Google Translate!). The sandbox page can be found at: https://spedas.org/wiki/index.php?title=Translation_Sandbox , and you will need to be logged in to make any edits, including translations.