Download or view GoogleTranslate.frink in plain text format
// Test of using Google's AJAX Language translation API.
//
// NOTE: As of 2011-12-01, Google has eliminated free access to their
// translation APIs, so this will not work! You may wish to pay for access
// to their API version 2, but it's no longer supported through Frink.
//
// WARNING: Their language codes are NOT necessarily the same as ISO 639-1
// language codes for some insane reason.
// Their peculiar language codes can be found at:
// http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray
// If you pass the empty string "" to the sourceLang argument, the language
// will be auto-detected.
// If you don't specify the target language, it will attempt to translate
// to your default language. Thus, calling
// translate["Some text"]
// or
// "Some text" -> translate
// will attempt to guess the language of the text and translate it to your
// language.
translate[str, sourceLang="", targetLang=getDefaultLanguage[]] :=
{
query = URLEncode[str, "UTF8"]
URL = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&format=text&langpair=$sourceLang|$targetLang&q=$query"
result = read[URL]
println[result]
// Find translated text, skipping over any escaped quotes.
[trans] = result =~ %r/"translatedText":"(.*?[^\\])"/
if (trans != undef)
{
// println["Before: $trans"]
trans =~ %s/\\"/"/g // Fix escaped quotes.
trans =~ %s/\\n/\n/g // Fix newlines.
} else
return str // Could not translate
return trans
}
// Shortcut to translate the text from one language to the specified language,
// attempting to guess the original language.
translate[str, targetLang] := translate[str, "", targetLang]
// Guesses the language of a certain piece of text.
// It returns:
// [languageCode, confidence, isReliable]
// where
// languageCode: a string indicating Google's 2-letter language code
// languageName: a string indicating a human-readable language name.
// confidence: a number between 0 and 1 indicating the confidence in
// the guess
// isReliable: a boolean value indicating if the guess should be considered
// reliable.
guessLanguage[str] :=
{
query = URLEncode[str, "UTF8"]
URL = "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=$query"
result = read[URL]
// println[result]
[language] = result =~ %r/"language":"(.*?)"/
[isReliable] = result =~ %r/"isReliable":(\w+)/
[confidence] = result =~ %r/"confidence":([0-9\.]+)/
return [language, GoogleLanguageMapper.getLanguage[language], eval[confidence], eval[isReliable]]
}
// Function to get the user's default language defined by the operating system.
getDefaultLanguage[] := callJava["java.lang.System", "getProperty", ["user.language"]]
// The following are shortcuts that will auto-translate
// to the specified language, guessing the input language.
// Due to the special function of the -> operator,
// these can be called like:
//
// "I am translating this into another language." -> Hebrew
//
Afrikaans[str] := translate[str, getDefaultLanguage[], "af"]
Albanian[str] := translate[str, getDefaultLanguage[], "sq"]
Amharic[str] := translate[str, getDefaultLanguage[], "am"]
//Arabic[str] := translate[str, getDefaultLanguage[], "ar"]
Armenian[str] := translate[str, getDefaultLanguage[], "hy"]
Azerbaijani[str] := translate[str, getDefaultLanguage[], "az"]
Basque[str] := translate[str, getDefaultLanguage[], "eu"]
Belarusian[str] := translate[str, getDefaultLanguage[], "be"]
Bengali[str] := translate[str, getDefaultLanguage[], "bn"]
Bihari[str] := translate[str, getDefaultLanguage[], "bh"]
Breton[str] := translate[str, getDefaultLanguage[], "br"]
Bulgarian[str] := translate[str, getDefaultLanguage[], "bg"]
Burmese[str] := translate[str, getDefaultLanguage[], "my"]
Catalan[str] := translate[str, getDefaultLanguage[], "ca"]
Cherokee[str] := translate[str, getDefaultLanguage[], "chr"]
Chinese[str] := translate[str, getDefaultLanguage[], "zh"]
//Chinese_Simplified[str] := translate[str, getDefaultLanguage[], "zh-CN"]
//Chinese_Traditional[str] := translate[str, getDefaultLanguage[], "zh-TW"]
Corsican[str] := translate[str, getDefaultLanguage[], "co"]
Croatian[str] := translate[str, getDefaultLanguage[], "hr"]
Czech[str] := translate[str, getDefaultLanguage[], "cs"]
Danish[str] := translate[str, getDefaultLanguage[], "da"]
Dhivehi[str] := translate[str, getDefaultLanguage[], "dv"]
//Dutch[str] := translate[str, getDefaultLanguage[], "nl"]
//English[str] := translate[str, getDefaultLanguage[], "en"]
Esperanto[str] := translate[str, getDefaultLanguage[], "eo"]
Estonian[str] := translate[str, getDefaultLanguage[], "et"]
Faroese[str] := translate[str, getDefaultLanguage[], "fo"]
Filipino[str] := translate[str, getDefaultLanguage[], "tl"]
Finnish[str] := translate[str, getDefaultLanguage[], "fi"]
French[str] := translate[str, getDefaultLanguage[], "fr"]
Frisian[str] := translate[str, getDefaultLanguage[], "fy"]
Galician[str] := translate[str, getDefaultLanguage[], "gl"]
Georgian[str] := translate[str, getDefaultLanguage[], "ka"]
//German[str] := translate[str, getDefaultLanguage[], "de"]
Greek[str] := translate[str, getDefaultLanguage[], "el"]
Guarani[str] := translate[str, getDefaultLanguage[], "gn"]
Gujarati[str] := translate[str, getDefaultLanguage[], "gu"]
HaitianCreole[str] := translate[str, getDefaultLanguage[], "ht"]
Hebrew[str] := translate[str, getDefaultLanguage[], "iw"]
Hindi[str] := translate[str, getDefaultLanguage[], "hi"]
Hungarian[str] := translate[str, getDefaultLanguage[], "hu"]
Icelandic[str] := translate[str, getDefaultLanguage[], "is"]
Indonesian[str] := translate[str, getDefaultLanguage[], "id"]
Inuktitut[str] := translate[str, getDefaultLanguage[], "iu"]
Irish[str] := translate[str, getDefaultLanguage[], "ga"]
//Italian[str] := translate[str, getDefaultLanguage[], "it"]
//Japanese[str] := translate[str, getDefaultLanguage[], "ja"]
Javanese[str] := translate[str, getDefaultLanguage[], "jw"]
Kannada[str] := translate[str, getDefaultLanguage[], "kn"]
Kazakh[str] := translate[str, getDefaultLanguage[], "kk"]
Khmer[str] := translate[str, getDefaultLanguage[], "km"]
//Korean[str] := translate[str, getDefaultLanguage[], "ko"]
Kurdish[str] := translate[str, getDefaultLanguage[], "ku"]
Kyrgyz[str] := translate[str, getDefaultLanguage[], "ky"]
Lao[str] := translate[str, getDefaultLanguage[], "lo"]
Laothian[str] := translate[str, getDefaultLanguage[], "lo"]
Latin[str] := translate[str, getDefaultLanguage[], "la"]
Latvian[str] := translate[str, getDefaultLanguage[], "lv"]
Lithuanian[str] := translate[str, getDefaultLanguage[], "lt"]
Luxembourgish[str] := translate[str, getDefaultLanguage[], "lb"]
Macedonian[str] := translate[str, getDefaultLanguage[], "mk"]
Malay[str] := translate[str, getDefaultLanguage[], "ms"]
Malayalam[str] := translate[str, getDefaultLanguage[], "ml"]
Maltese[str] := translate[str, getDefaultLanguage[], "mt"]
Maori[str] := translate[str, getDefaultLanguage[], "mi"]
Marathi[str] := translate[str, getDefaultLanguage[], "mr"]
Mongolian[str] := translate[str, getDefaultLanguage[], "mn"]
Nepali[str] := translate[str, getDefaultLanguage[], "ne"]
Norwegian[str] := translate[str, getDefaultLanguage[], "no"]
Occitan[str] := translate[str, getDefaultLanguage[], "oc"]
Oriya[str] := translate[str, getDefaultLanguage[], "or"]
Pashto[str] := translate[str, getDefaultLanguage[], "ps"]
Persian[str] := translate[str, getDefaultLanguage[], "fa"]
Polish[str] := translate[str, getDefaultLanguage[], "pl"]
//Portuguese[str] := translate[str, getDefaultLanguage[], "pt-PT"]
Punjabi[str] := translate[str, getDefaultLanguage[], "pa"]
Quechua[str] := translate[str, getDefaultLanguage[], "qu"]
Romanian[str] := translate[str, getDefaultLanguage[], "ro"]
//Russian[str] := translate[str, getDefaultLanguage[], "ru"]
Sanskrit[str] := translate[str, getDefaultLanguage[], "sa"]
ScotsGaelic[str] := translate[str, getDefaultLanguage[], "gd"]
Serbian[str] := translate[str, getDefaultLanguage[], "sr"]
Sindhi[str] := translate[str, getDefaultLanguage[], "sd"]
Sinhalese[str] := translate[str, getDefaultLanguage[], "si"]
Slovak[str] := translate[str, getDefaultLanguage[], "sk"]
Slovenian[str] := translate[str, getDefaultLanguage[], "sl"]
//Spanish[str] := translate[str, getDefaultLanguage[], "es"]
Sundanese[str] := translate[str, getDefaultLanguage[], "su"]
Swahili[str] := translate[str, getDefaultLanguage[], "sw"]
//Swedish[str] := translate[str, getDefaultLanguage[], "sv"]
Syriac[str] := translate[str, getDefaultLanguage[], "syr"]
Tajik[str] := translate[str, getDefaultLanguage[], "tg"]
Tamil[str] := translate[str, getDefaultLanguage[], "ta"]
Tatar[str] := translate[str, getDefaultLanguage[], "tt"]
Tagalog[str] := translate[str, getDefaultLanguage[], "tl"]
Telugu[str] := translate[str, getDefaultLanguage[], "te"]
Thai[str] := translate[str, getDefaultLanguage[], "th"]
Tibetan[str] := translate[str, getDefaultLanguage[], "bo"]
Tonga[str] := translate[str, getDefaultLanguage[], "to"]
Turkish[str] := translate[str, getDefaultLanguage[], "tr"]
Ukrainian[str] := translate[str, getDefaultLanguage[], "uk"]
Urdu[str] := translate[str, getDefaultLanguage[], "ur"]
Uzbek[str] := translate[str, getDefaultLanguage[], "uz"]
Uighur[str] := translate[str, getDefaultLanguage[], "ug"]
Vietnamese[str] := translate[str, getDefaultLanguage[], "vi"]
Welsh[str] := translate[str, getDefaultLanguage[], "cy"]
Yiddish[str] := translate[str, getDefaultLanguage[], "yi"]
Yoruba[str] := translate[str, getDefaultLanguage[], "yo"]
FromAfrikaans[str] := translate[str, "af", getDefaultLanguage[]]
FromAlbanian[str] := translate[str, "sq", getDefaultLanguage[]]
FromAmharic[str] := translate[str, "am", getDefaultLanguage[]]
//FromArabic[str] := translate[str, "ar", getDefaultLanguage[]]
FromArmenian[str] := translate[str, "hy", getDefaultLanguage[]]
FromAzerbaijani[str] := translate[str, "az", getDefaultLanguage[]]
FromBasque[str] := translate[str, "eu", getDefaultLanguage[]]
FromBelarusian[str] := translate[str, "be", getDefaultLanguage[]]
FromBengali[str] := translate[str, "bn", getDefaultLanguage[]]
FromBihari[str] := translate[str, "bh", getDefaultLanguage[]]
FromBreton[str] := translate[str, "br", getDefaultLanguage[]]
FromBulgarian[str] := translate[str, "bg", getDefaultLanguage[]]
FromBurmese[str] := translate[str, "my", getDefaultLanguage[]]
FromCatalan[str] := translate[str, "ca", getDefaultLanguage[]]
FromCherokee[str] := translate[str, "chr", getDefaultLanguage[]]
//FromChinese[str] := translate[str, "zh", getDefaultLanguage[]]
//FromChinese_Simplified[str] := translate[str, "zh-CN", getDefaultLanguage[]]
//FromChinese_Traditional[str] := translate[str, "zh-TW", getDefaultLanguage[]]
FromCorsican[str] := translate[str, "co", getDefaultLanguage[]]
FromCroatian[str] := translate[str, "hr", getDefaultLanguage[]]
FromCzech[str] := translate[str, "cs", getDefaultLanguage[]]
FromDanish[str] := translate[str, "da", getDefaultLanguage[]]
FromDhivehi[str] := translate[str, "dv", getDefaultLanguage[]]
//FromDutch[str] := translate[str, "nl", getDefaultLanguage[]]
//FromEnglish[str] := translate[str, "en", getDefaultLanguage[]]
FromEsperanto[str] := translate[str, "eo", getDefaultLanguage[]]
FromEstonian[str] := translate[str, "et", getDefaultLanguage[]]
FromFaroese[str] := translate[str, "fo", getDefaultLanguage[]]
FromFilipino[str] := translate[str, "tl", getDefaultLanguage[]]
FromFinnish[str] := translate[str, "fi", getDefaultLanguage[]]
FromFrench[str] := translate[str, "fr", getDefaultLanguage[]]
FromFrisian[str] := translate[str, "fy", getDefaultLanguage[]]
FromGalician[str] := translate[str, "gl", getDefaultLanguage[]]
FromGeorgian[str] := translate[str, "ka", getDefaultLanguage[]]
//FromGerman[str] := translate[str, "de", getDefaultLanguage[]]
FromGreek[str] := translate[str, "el", getDefaultLanguage[]]
FromGuarani[str] := translate[str, "gn", getDefaultLanguage[]]
FromGujarati[str] := translate[str, "gu", getDefaultLanguage[]]
FromHaitianCreole[str] := translate[str, "ht", getDefaultLanguage[]]
FromHebrew[str] := translate[str, "iw", getDefaultLanguage[]]
FromHindi[str] := translate[str, "hi", getDefaultLanguage[]]
FromHungarian[str] := translate[str, "hu", getDefaultLanguage[]]
FromIcelandic[str] := translate[str, "is", getDefaultLanguage[]]
FromIndonesian[str] := translate[str, "id", getDefaultLanguage[]]
FromInuktitut[str] := translate[str, "iu", getDefaultLanguage[]]
FromIrish[str] := translate[str, "ga", getDefaultLanguage[]]
//FromItalian[str] := translate[str, "it", getDefaultLanguage[]]
//FromJapanese[str] := translate[str, "ja", getDefaultLanguage[]]
FromJavanese[str] := translate[str, "jw", getDefaultLanguage[]]
FromKannada[str] := translate[str, "kn", getDefaultLanguage[]]
FromKazakh[str] := translate[str, "kk", getDefaultLanguage[]]
FromKhmer[str] := translate[str, "km", getDefaultLanguage[]]
//FromKorean[str] := translate[str, "ko", getDefaultLanguage[]]
FromKurdish[str] := translate[str, "ku", getDefaultLanguage[]]
FromKyrgyz[str] := translate[str, "ky", getDefaultLanguage[]]
FromLao[str] := translate[str, "lo", getDefaultLanguage[]]
FromLaothian[str] := translate[str, "lo", getDefaultLanguage[]]
FromLatin[str] := translate[str, "la", getDefaultLanguage[]]
FromLatvian[str] := translate[str, "lv", getDefaultLanguage[]]
FromLithuanian[str] := translate[str, "lt", getDefaultLanguage[]]
FromLuxembourgish[str] := translate[str, "lb", getDefaultLanguage[]]
FromMacedonian[str] := translate[str, "mk", getDefaultLanguage[]]
FromMalay[str] := translate[str, "ms", getDefaultLanguage[]]
FromMalayalam[str] := translate[str, "ml", getDefaultLanguage[]]
FromMaltese[str] := translate[str, "mt", getDefaultLanguage[]]
FromMaori[str] := translate[str, "mi", getDefaultLanguage[]]
FromMarathi[str] := translate[str, "mr", getDefaultLanguage[]]
FromMongolian[str] := translate[str, "mn", getDefaultLanguage[]]
FromNepali[str] := translate[str, "ne", getDefaultLanguage[]]
FromNorwegian[str] := translate[str, "no", getDefaultLanguage[]]
FromOccitan[str] := translate[str, "oc", getDefaultLanguage[]]
FromOriya[str] := translate[str, "or", getDefaultLanguage[]]
FromPashto[str] := translate[str, "ps", getDefaultLanguage[]]
FromPersian[str] := translate[str, "fa", getDefaultLanguage[]]
FromPolish[str] := translate[str, "pl", getDefaultLanguage[]]
//FromPortuguese[str] := translate[str, "pt-PT", getDefaultLanguage[]]
FromPunjabi[str] := translate[str, "pa", getDefaultLanguage[]]
FromQuechua[str] := translate[str, "qu", getDefaultLanguage[]]
FromRomanian[str] := translate[str, "ro", getDefaultLanguage[]]
//FromRussian[str] := translate[str, "ru", getDefaultLanguage[]]
FromSanskrit[str] := translate[str, "sa", getDefaultLanguage[]]
FromScotsGaelic[str] := translate[str, "gd", getDefaultLanguage[]]
FromSerbian[str] := translate[str, "sr", getDefaultLanguage[]]
FromSindhi[str] := translate[str, "sd", getDefaultLanguage[]]
FromSinhalese[str] := translate[str, "si", getDefaultLanguage[]]
FromSlovak[str] := translate[str, "sk", getDefaultLanguage[]]
FromSlovenian[str] := translate[str, "sl", getDefaultLanguage[]]
//FromSpanish[str] := translate[str, "es", getDefaultLanguage[]]
FromSundanese[str] := translate[str, "su", getDefaultLanguage[]]
FromSwahili[str] := translate[str, "sw", getDefaultLanguage[]]
//FromSwedish[str] := translate[str, "sv", getDefaultLanguage[]]
FromSyriac[str] := translate[str, "syr", getDefaultLanguage[]]
FromTajik[str] := translate[str, "tg", getDefaultLanguage[]]
FromTamil[str] := translate[str, "ta", getDefaultLanguage[]]
FromTagalog[str] := translate[str, "tl", getDefaultLanguage[]]
FromTatar[str] := translate[str, "tt", getDefaultLanguage[]]
FromTelugu[str] := translate[str, "te", getDefaultLanguage[]]
FromThai[str] := translate[str, "th", getDefaultLanguage[]]
FromTibetan[str] := translate[str, "bo", getDefaultLanguage[]]
FromTonga[str] := translate[str, "to", getDefaultLanguage[]]
FromTurkish[str] := translate[str, "tr", getDefaultLanguage[]]
FromUkrainian[str] := translate[str, "uk", getDefaultLanguage[]]
FromUrdu[str] := translate[str, "ur", getDefaultLanguage[]]
FromUzbek[str] := translate[str, "uz", getDefaultLanguage[]]
FromUighur[str] := translate[str, "ug", getDefaultLanguage[]]
FromVietnamese[str] := translate[str, "vi", getDefaultLanguage[]]
FromWelsh[str] := translate[str, "cy", getDefaultLanguage[]]
FromYiddish[str] := translate[str, "yi", getDefaultLanguage[]]
FromYoruba[str] := translate[str, "yo", getDefaultLanguage[]]
class GoogleLanguageMapper
{
class var map = undef
// Returns the human-readable name of a language given its code.
class getLanguage[code] :=
{
if map == undef
initializeMap[]
return map@code
}
class initializeMap[] :=
{
map = new dict
map@"af" = "Afrikaans"
map@"sq" = "Albanian"
map@"am" = "Amharic"
map@"ar" = "Arabic"
map@"hy" = "Armenian"
map@"az" = "Azerbaijani"
map@"eu" = "Basque"
map@"be" = "Belarusian"
map@"bn" = "Bengali"
map@"bh" = "Bihari"
map@"br" = "Breton"
map@"bg" = "Bulgarian"
map@"my" = "Burmese"
map@"ca" = "Catalan"
map@"chr" = "Cherokee"
map@"zh" = "Chinese"
map@"zh-cn" = "Chinese_Simplified"
map@"zh-tw" = "Chinese_Traditional"
map@"co" = "Corsican"
map@"hr" = "Croatian"
map@"cs" = "Czech"
map@"da" = "Danish"
map@"dv" = "Dhivehi"
map@"nl" = "Dutch"
map@"en" = "English"
map@"eo" = "Esperanto"
map@"et" = "Estonian"
map@"fo" = "Faroese"
map@"tl" = "Filipino"
map@"fi" = "Finnish"
map@"fr" = "French"
map@"fy" = "Frisian"
map@"gl" = "Galician"
map@"ka" = "Georgian"
map@"de" = "German"
map@"el" = "Greek"
map@"gu" = "Gujarati"
map@"ht" = "Haitian_Creole"
map@"iw" = "Hebrew"
map@"hi" = "Hindi"
map@"hu" = "Hungarian"
map@"is" = "Icelandic"
map@"id" = "Indonesian"
map@"iu" = "Inuktitut"
map@"ga" = "Irish"
map@"it" = "Italian"
map@"ja" = "Japanese"
map@"jw" = "Javanese"
map@"kn" = "Kannada"
map@"kk" = "Kazakh"
map@"km" = "Khmer"
map@"ko" = "Korean"
map@"ku" = "Kurdish"
map@"ky" = "Kyrgyz"
map@"lo" = "Lao"
map@"la" = "Latin"
map@"lv" = "Latvian"
map@"lt" = "Lithuanian"
map@"lb" = "Luxembourgish"
map@"mk" = "Macedonian"
map@"ms" = "Malay"
map@"ml" = "Malayalam"
map@"mt" = "Maltese"
map@"mi" = "Maori"
map@"mr" = "Marathi"
map@"mn" = "Mongolian"
map@"ne" = "Nepali"
map@"no" = "Norwegian"
map@"oc" = "Occitan"
map@"or" = "Oriya"
map@"ps" = "Pashto"
map@"fa" = "Persian"
map@"pl" = "Polish"
map@"pt" = "Portuguese"
map@"pt-pt" = "Portuguese_Portugal"
map@"pa" = "Punjabi"
map@"qu" = "Quechua"
map@"ro" = "Romanian"
map@"ru" = "Russian"
map@"sa" = "Sanskrit"
map@"gd" = "Scots_Gaelic"
map@"sr" = "Serbian"
map@"sd" = "Sindhi"
map@"si" = "Sinhalese"
map@"sk" = "Slovak"
map@"sl" = "Slovenian"
map@"es" = "Spanish"
map@"su" = "Sundanese"
map@"sw" = "Swahili"
map@"sv" = "Swedish"
map@"syr" = "Syriac"
map@"tg" = "Tajik"
map@"ta" = "Tamil"
map@"tt" = "Tatar"
map@"te" = "Telugu"
map@"th" = "Thai"
map@"bo" = "Tibetan"
map@"to" = "Tonga"
map@"tr" = "Turkish"
map@"uk" = "Ukrainian"
map@"ur" = "Urdu"
map@"uz" = "Uzbek"
map@"ug" = "Uighur"
map@"vi" = "Vietnamese"
map@"cy" = "Welsh"
map@"yi" = "Yiddish"
map@"yo" = "Yoruba"
map@"" = "Unknown"
}
}
"GoogleTranslate.frink included OK"
Download or view GoogleTranslate.frink in plain text format
This is a program written in the programming language Frink.
For more information, view the Frink
Documentation or see More Sample Frink Programs.
Alan Eliasen was born 20217 days, 15 hours, 24 minutes ago.