Adding translations using Javascript

Jump to: navigation, search

You seem to be a wizard with Javascript in Wiktionary. Do you know whether there is a Javascript API like this: 'addTranslation(String entry, String translationGloss, String language, String translation)', which adds the translation in the language to the entry to the translation section with '{{trans-top|'+translationGloss? Conrad Irwin's editor.js is a place I was looking at, but I did not figure it out in a reasonably short time.

Dan Polansky (talk)17:47, 24 February 2012

No, I don't think there is one. If you already have the gloss, language code, and language name available, and don't have to deal with complicated situations (nesting, multiple translations for one language, etc), you're probably best off doing something like this:

JsMwApi().page(entryname).edit( function (text, save_function, r){
        var p = text.indexOf("{"+"{trans-top|"+gloss+"}}"), 
                b = text.indexOf("\n{"+"{trans-bottom}}", p), 
                added_wikitext = "\n* " + languagename + ": {" + "{t|" + languagecode + "|" + translation + "}}";
        while( p < b && p > -1 ){
                p = text.indexOf("\n* ", p+1);
                if( text.substr( p ) > added_wikitext )
                        break;
        }
        if( p > b || p == -1)
                p = b;
        save_function( text.substr( 0, p ) + added_wikitext + text.substr( p ), {summary:"+trans: [["+translation+"]]"}, function( was_successful ) {
                // ...
        } )
} )

(^ Not intended for actual use.)

Yair rand (talk)22:15, 26 February 2012

Thank you!

Dan Polansky (talk)23:18, 26 February 2012