Adding translations using Javascript

Jump to navigation Jump to search

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