USO Google Translate

By Tim Smart Last update Sep 3, 2009 — Installed 125 times. Daily Installs: 0, 2, 1, 0, 1, 22, 1, 11, 0, 0, 0, 4, 0, 3, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1
Script Summary:
A @require Greasemonkey Library for Google Translate
Version: 25
Copyright: (c) 2009 Tim Smart
License: http://uso-google-translate.googlecode.com/svn/trunk/license.txt

Script homepage

This script has no discussions.

start the discussion

This script has no reviews.

This script does nothing on its own. It is a Greasemonkey Javascript library that provides easy access to the Google Translate API.

To use you can either make use of the metadata key @require in your script, like so:

// @require http://userscripts.org/scripts/source/56630.user.js

Or you can directly place the source code into your script.

Usage / Documentation

USO.googleTranslate( options );
Where the parameter options is an Object, that accepts the following properties:

input
Value: Object or Array or String
Default: No Default
Optional: No
Usage:
// Object
options.input = {
  "string":         "A string to translate",
  "another_string": "Another string"
};

// Array
options.input = [ "A string to translate", "Another string" ];

// String
options.input = "A string to translate";
  • Contains the string(s) you want to be translated.
  • Determines format of output. Output strings will return in a identical format, except strings will be replaced with their translated counterparts.
targetLanguage
Value: String
Default: Automatically determined
Optional: Yes ( recommended not to set to avoid over-riding language auto-detect )
Usage:
options.targetLanguage = 'fr';
  • USO Google Translate automatically detects the browser language if left unset
  • Use the two digit ISO 639-1 Code for alternate language support.
defaultLanguage
Value: String
Default: new String("en")
Optional: Yes
Usage:
options.defaultLanguage = 'en';
  • Determines the targetLanguage when using auto-detect, and it fails. Will very rarely get used
inputLanguage
Value: String
Default: new String("") (A blank string will turn on auto-detect)
Optional: Yes
Usage:
options.inputLanguage = 'en';
  • Should be set to the language of the input strings.
  • If you do not know the language of the input strings, then leave value unset or as a blank string (new String("")), and it will use Google Translates auto-detect feature to determine the input language for each string.
expires
Value: Number
Default: 24 * 60 (24 hours)
Optional: Yes
Usage:
options.expires = 24 * 60;
  • Determines how long output is cached for in minutes.
  • If set to 0, output will not be cached
  • If set to Infinity, output will be indefinately cached
callback
Value: Function
Default: null
Optional: Yes
Usage:
options.callback = function ( details, output ) {
  this; // Contains direct output
  output; // Same as 'this'
  details.output; // Same as 'this' and 'output'
  details.outputLanguage; // The language of the output string(s)
  details.targetLanguage; // The language of the 'desired' output string(s)
  details.input; // What user used as input
  details.inputLanguage; // The language of the input string(s). It will return in same format as output, expect values will contain language codes.
  details.expires; // When the cache of the output strings will expire as a timestamp. Infinity === will never expire
}
  • Is called when translation is complete
  • See comments in usage for returned values
  • The output is in the same format as your input