Work on a more sophisticated help file is still in progress. Any feedback that helps to improve this is appreciated (skmail@gmx.net).
Content:
1. General guidelines
2. The objects and their meaning
3. URL syntax
4. Language
codes / All languages list
5. Processing
translation results
(Regex)
6. Example
7. Advanced (own functions, POST, mimetypes, charset...)
A definition file, as shown in the editor,
always defines all 3 services of
BabelFish!
If you want to replace only one service then just leave the other
service definitions as they are (do not remove them).
The 3 BabelFish services are addressed by numbers, they are named
service1-3 (the default settings: service1=Google, service2=Yahoo,
service3=Dictionary).
Important: The dictionary service (service3) accepts
single words only!
You will find those names in almost every identifier on the left side of
the colon, here the most important for service 2:
service2Name
service2URL
service2LanguageCodes
service2ProcessResponse
Everything on the right side of a colon can be modified.
The object names on the left side are reserved and must
not be modified or removed.
The same applies for left-sided language names within a
language code structure (see here).
Do not rename/modify the following identifiers (? means any valid service
number):
var bfplugin,
minBFVersion,
pluginVersion,
pluginAuthor,
service?Name,
service?URL,
service?SiteURL,
service?MimeType,
service?MaxLength,
service?ProcessResponse,
service?LanguageCodes,
Make sure you also looked at this example
minBFVersion = For BabelFish compatibility
pluginVersion = Your plugin edit version
pluginAuthor= Your name
service?Name = The translation service name shown in BabelFish's tooltip or in the options (try to keep this short)
service?URL = Where to request a text translation (usually contains %s=Text to translate, %from=Source language code %to=Target language code, see here)
service?SiteURL = Where to request a whole page translation (usually contains %url=Page to translate, %from=Source language code %to=Target language code, %nl=Browser language (autodetected), see here)
service?MimeType = MimeType and charset to be used
service?MaxLength = Maximum text length, otherwise text is sent splitted
service?ProcessResponse = Post-processing of service answer, put your regular expression rules in here (e.g. for removing HTML code, see here)
service?LanguageCodes = Supported service languages, on the right side the code which is sent with %from and %to placeholders (do not modify the long language names on the left side, see here).
Important: URL's are enclosed in a pair outer single quotes as well as in a pair double inner quotes!
a) service?URL: Used for text translations:
Supported are GET and POST requests.
Placeholders for the requested
text (%s) the language code of the source language (%from) and
the language code for the target language (%to).
At runtime BabelFish will replace those placeholders with their
current values.
That's how a typical translation request URL looks like:
service2URL:
'"http://babelfish.yahoo.com/translate_txt?ei=UTF-8&lp=%from_%to&text=%s"',
.
More advanced stuff:
Details for POST requests
Usage of functions in URL's
b) service?SiteURL: Used for whole page translations:
Placeholders for the page to be translated (%url) the language code of the source language (%from) and
the language code for the target language (%to), the browser language (%nl).
Within a language code structure, the left-sided full-length language names are reserved and must not be modified. The short language codes on the right side are allowed to be adjusted (that is also needed in most cases). The short codes are sent with the %from and %to placeholders of the URL.
Languages can be removed if not supported by a service, but you cannot add more as defined in the following 'all languages list'.
Bold printed short codes on the right are allowed to
be modified:
service?LanguageCodes : {
"auto" : "xx",
"Albanian" : "sq",
"Arabic" : "ar",
"Bulgarian" : "bg",
"Catalan" : "ca",
"Chinese" : "zh",
"Chinese[Trad]" : "zh-TW",
"Croatian" : "hr",
"Czech" : "cs",
"Danish" : "da",
"Dutch" : "nl",
"English" : "en",
"Estonian" : "et",
"Filipino" : "tl",
"Finnish" : "fi",
"French" : "fr",
"Galician" : "gl",
"German" : "de",
"Greek" : "el",
"Hebrew" : "iw",
"Hindi" : "hi",
"Hungarian" : "hu",
"Indonesian" : "id",
"Italian" : "it",
"Japanese" : "ja",
"Korean" : "ko",
"Latvian" : "lv",
"Lithuanian" : "lt",
"Maltese" : "mt",
"Norwegian" : "no",
"Polish" : "pl",
"Portuguese" : "pt",
"Romanian" : "ro",
"Russian" : "ru",
"Serbian" : "sr",
"Slovak" : "sk",
"Slovenian" : "sl",
"Swedish" : "sv",
"Spanish" : "es",
"Thai" : "th",
"Turkish" : "tr",
"Ukrainian" : "uk",
"Vietnamese" : "vi"
As answer to a request a translation service usually sends a
complete HTML page, not only the translated words.
In that case it is needed to strip the 'surrounding' HTML code
from the complete translation result.
Usually this is done by using RegEx filters (regular
expressions). There are lots of examples to find in the
web.
The service?ProcessResponse function contains the RegEx
filter rules to postprocess the answer.
service2ProcessResponse: function(response) //do not edit this line{
//your regex rules go here...
//match the last HTML element before the the translated word and any non-< char following:
response = response.match(/v style="padding:0.6em;">[^<]+/mi);
//if the did not match, e.g. invalid service answer, the return nothing so BabelFish might try another request:
if(!response)
return '';
//remove the HTML that matched before the translated word:
response = response[0].replace(/.*>/m, ""); //remove remaining HTML
//...your regex rules end
return response; },
var bfplugin = {//never change this line
//BabelFish compatibility:
minBFVersion: '1.77',
//Your plugin edit version:
pluginVersion: '1.0',
//Author:
pluginAuthor: 'NettiCat',
//Display name of your service:
service1Name: 'Google',
service2Name: 'Yahoo',
service3Name: 'GoogleDic',
//URL to get the text translation (enclose in outer single '' AND inner double ""):
//placeholder for the text to translate is %s
//placeholder for source language code is %from
//placeholder for target language code is %to
service1URL: '"http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&langpair=%from%7C%to"',
service2URL: '"http://babelfish.yahoo.com/translate_txt?ei=UTF-8&lp=%from_%to&text=%s"',
service3URL: '"http://www.google.com/dictionary?aq=f&hl=en&q=%s&langpair=%from%7C%to"',
//URL to translate a whole page (enclose in outer single '' AND inner double ""):
//placeholder for the site to translate is %url
//placeholder for source language code is %from
//placeholder for target language code is %to
//placeholder for browser language code is %nl
service1SiteURL: '"http://translate.google.com/translate?hl=%nl&langpair=%from|%to&u=%url"',
service2SiteURL: '"http://babelfish.yahoo.com/translate_url?doit=done&tt=url&intl=1&fr=bf-home&btnTrUrl=Translate&lp=%from_%to&trurl=%url"',
//Maximum text-length accepted by service, otherwise text is splitted:
service1MaxLength: 300,
service2MaxLength: 1000,
//Language codes used in the service URL (with %from or %to):
//Remove languages not supported by your service
//Change the short language codes on the right to those used by your service
service1LanguageCodes : {
'auto' : 'auto',
'Albanian' : 'sq',
'Arabic' : 'ar',
'Bulgarian' : 'bg',
'Catalan' : 'ca',
'Chinese' : 'zh',
'Croatian' : 'hr',
'Czech' : 'cs',
'Danish' : 'da',
'Dutch' : 'nl',
'English' : 'en',
'Estonian' : 'et',
'Filipino' : 'tl',
'Finnish' : 'fi',
'French' : 'fr',
'Galician' : 'gl',
'German' : 'de',
'Greek' : 'el',
'Hebrew' : 'iw',
'Hindi' : 'hi',
'Hungarian' : 'hu',
'Indonesian' : 'id',
'Italian' : 'it',
'Japanese' : 'ja',
'Korean' : 'ko',
'Latvian' : 'lv',
'Lithuanian' : 'lt',
'Maltese' : 'mt',
'Norwegian' : 'no',
'Polish' : 'pl',
'Portuguese' : 'pt',
'Romanian' : 'ro',
'Russian' : 'ru',
'Serbian' : 'sr',
'Slovak' : 'sk',
'Slovenian' : 'sl',
'Swedish' : 'sv',
'Spanish' : 'es',
'Thai' : 'th',
'Turkish' : 'tr',
'Ukrainian' : 'uk',
'Vietnamese' : 'vi'
},
service2LanguageCodes : {
'Chinese' : 'zh',
'Dutch' : 'nl',
'English' : 'en',
'French' : 'fr',
'German' : 'de',
'Greek' : 'el',
'Italian' : 'it',
'Japanese' : 'ja',
'Korean' : 'ko',
'Portuguese' : 'pt',
'Russian' : 'ru',
'Spanish' : 'es'
},
service3LanguageCodes : {
'Albanian' : 'sq',
'Arabic' : 'ar',
'Bulgarian' : 'bg',
'Catalan' : 'ca',
'Chinese' : 'zh',
'Croatian' : 'hr',
'Czech' : 'cs',
'Danish' : 'da',
'Dutch' : 'nl',
'English' : 'en',
'Estonian' : 'et',
'Filipino' : 'tl',
'Finnish' : 'fi',
'French' : 'fr',
'Galician' : 'gl',
'German' : 'de',
'Greek' : 'el',
'Hebrew' : 'iw',
'Hindi' : 'hi',
'Hungarian' : 'hu',
'Indonesian' : 'id',
'Italian' : 'it',
'Japanese' : 'ja',
'Korean' : 'ko',
'Latvian' : 'lv',
'Lithuanian' : 'lt',
'Maltese' : 'mt',
'Norwegian' : 'no',
'Polish' : 'pl',
'Portuguese' : 'pt',
'Romanian' : 'ro',
'Russian' : 'ru',
'Serbian' : 'sr',
'Slovak' : 'sk',
'Slovenian' : 'sl',
'Swedish' : 'sv',
'Spanish' : 'es',
'Thai' : 'th',
'Turkish' : 'tr',
'Ukrainian' : 'uk',
'Vietnamese' : 'vi'
},
service1ProcessResponse: function(response){
//your regex rules go here...
response = eval( "(" + String(response) + ")" );
if(!response.responseData || !response.responseData.translatedText)
return '';
response = response.responseData.translatedText;
//...your regex rules end
return response;
},
service2ProcessResponse: function(response){
//your regex rules go here...
response = response.match(/v style="padding:0.6em;">[^<]+/mi);
if(!response)
return '';
response = response[0].replace(/.*>/m, ""); //remove remaining HTML
//...your regex rules end
return response;
},
service3ProcessResponse: function(response){
//your regex rules go here...
response = response.match(/<span class="mn">([^<]+)/gmi);
if(!response)
return '';
response = response.join(",");
response = response.replace(/<[^>]+>/gm, " "); //remove remaining HTML, insert spaces
//...your regex rules end
return response;
},
//User defined function example
//To use this function anywhere above you would address it with bfplugin.extraFunction(...)
extraFunction: function(input){
var output = input; //
return output;
},
};//never change this line
Usually not necessary but it is also possible to define own
javascript functions, e.g. if the URL needs a computed
value:
You may define something like the example:
myFunction: function (invalue){
outvalue = invalue +1;
return outvalue;
},
Functions should be defined within the bfplugin object; remember the heading bfplugin. if you call the function.
How to use a own function in a URL:
service3URL: '"http://www.xyz.com/translate?string=%s&lang=%from|%to"+
bfplugin.myFunction(%s)',
Important: Note that the function part of the URL is enclosed in single quotes only
Defining POST requests is easy. With those requests there is always a list of parameters posted to the server.
If BabelFish finds a parameter object in the service definitions it will automatically switch to POST mode for that service.
Thus, to enable POST mode, it is just needed to define a parameter object as shown below:
service1PostParams: '"from=%from&to=%to&txt=%s"',
service2PostParams: '"lorem=ipsum&name=binny"',
This example shows how POST mode is activated for service1 and service2.
Service1 posts the following data:
from=%from
to=%to
txt=%s
Important: PostParams are enclosed in a pair outer single quotes as well as in a pair double inner quotes!
Sometimes it is necessary to define a mimetype or a charset that should be used to retrieve the results from a server.
If a
service?MimeType is specified it will be used on every request of that service.
Usually you would use text/html as mimetype with a special charset as shown below:
service3MimeType: "text/html; charset=windows-1251",