Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GoogleTranslate issue
#1
Hello. Big Grin

Error Message:
"{"responseData": null, "responseDetails": "Please use Translate v2. See http://code.google.com/apis/language/tr ... rview.html", "responseStatus": 403}"

Teach to me how Can I use it?
Thanks Always.
Function Function4
Code:
Copy      Help
str t r
t=GoogleTranslate("test translation" "lt" "" 0 r)
if(t.len) out t
else out r
;

Function GoogleTranslate
Code:
Copy      Help
;/
function'str $text $langTo [$langFrom] [flags] [str&fullResponse] ;;flags: 1 text is HTML

;Translates text using Google Translate.
;Returns translated text.
;If fails, returns empty string. fullResponse may contain error description.

;text - text to translate. Text length is limited to ~5100.
;langTo - output language.
;langFrom - input language. If omitted or "", autodetects.
;fullResponse - if used, receives full response in JSON format.

;Languages are like "en", "es". <link "http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray">Reference</link>.
;<link "http://code.google.com/apis/ajaxlanguage/documentation/reference.html#_intro_fonje">More info</link>.
;QM should run in Unicode mode. If not, some characters in result may be incorect, or some words not translated.

;EXAMPLE
;str t r
;t=GoogleTranslate("test translation" "lt" "" 0 r)
;if(t.len) out t
;else out r


ARRAY(POSTFIELD) a.create(5)
a[0].name="v"; a[0].value="1.0"
a[1].name="userip"; GetIpAddress "" a[1].value
a[2].name="langpair"; a[2].value.from(langFrom "|" langTo)
a[3].name="format"; a[3].value=iif(flags&1 "html" "text")
a[4].name="q"; a[4].value=text
if(!_unicode) a[4].value.ConvertEncoding(CP_ACP CP_UTF8)

str s r
Http h.Connect("ajax.googleapis.com")
h.PostFormData("ajax/services/language/translate" a s "Referer: http:\\www.quickmacros.com\[]")
err+ s=_error.description
;out s
if(&fullResponse) fullResponse=s

if(findrx(s "''translatedText'' *: *''(.+?[^\\])''" 0 1 r 1)<0) ret
if(!_unicode) r.ConvertEncoding(CP_UTF8 CP_ACP)

if findc(r '\')>=0
,r.findreplace("\r" "[13]")
,r.findreplace("\n" "[10]")
,r.findreplace("\''" "''")
,r.findreplace("\\" "\")
,r.findreplace("\/" "/")
,r.findreplace("\u0026" "&")
,r.findreplace("\u003c" "<")
,r.findreplace("\u003d" "=")
,r.findreplace("\u003e" ">")

ret r
#2
This function is dead, because the free Google translation service that it used now does not exist. The new Google translate API is paid.
#3
Hi BK!

Please, take a look at this post: Translator
#4
Thank you for the hint. Big Grin
But, this long text and Asia language output error.
#5
Can you update the function GoogleTranslate using https://ctrlq.org/code/19909-google-translate-api?
#6
Hey Lucas, nice catch!

CTRLQ is a wonderful source of scripts. Thanks for sharing.

Function GoogleTranslator
Code:
Copy      Help
;/
function'str str'sourceText str'targetLang [str'sourceLang]

lpstr PatternLanguageCode="^(af|ga|sq|it|ar|ja|az|kn|eu|ko|bn|la|be|lv|bg|lt|ca|mk|zh\-CN|ms|zh\-TW|mt|hr|no|cs|fa|da|pl|nl|pt|en|ro|eo|ru|et|sr|tl|sk|fi|sl|fr|es|gl|sw|ka|sv|de|ta|el|te|gu|th|ht|tr|iw|uk|hi|ur|hu|vi|is|cy|id|yi)$"

if findrx(targetLang PatternLanguageCode)<0
,end F"targetLang: '{targetLang}' Language Code not supported"

if empty(sourceLang)
,sourceLang="auto"

if sourceLang<>"auto" and findrx(sourceLang PatternLanguageCode)<0
,end F"sourceLang: '{sourceLang}' Language Code not supported"

sourceText.escape(11)

str s s1

IntGetFile F"https://translate.googleapis.com/translate_a/single?client=gtx&sl={sourceLang}&tl={targetLang}&dt=t&q={sourceText}" s

if findrx(s "\[\[\[''(.+)'',''" 0 1 s1 1)<0
,ret "not found"

ret s1.escape(10)

Macro Use GoogleTranslator
Code:
Copy      Help
lpstr ssourceText=
;Here is Mr. Gintaras, our dear teacher living in Lithuania

out GoogleTranslator(ssourceText "es")
#7
Long length, a sentence not translated properly.
#8
I try this:
Function GoogleTranslator_Test
Code:
Copy      Help
str Text=
;A fragment of plane wing discovered in Mauritius in May has been confirmed as coming from missing plane Malaysia Airlines MH370, Australia's Transport Safety Bureau (ATSB) said in a statement on Friday.
;It is the third piece of debris to be definitively linked to MH370, Australia's transport minister Darren Chester said Friday.
;
;"It does not, however, provide information that can be used to determine a specific location of the aircraft," Chester said.
;
;The plane wing fragment was discovered in Mauritius on 10 May 2016, and was delivered to the ATSB for investigation. An "'OL' part identifier" was legible on the plane piece, the ATSB said, which allowed investigators to definitively identify the wreckage. Malaysian Airlines Flight 370 vanished on March 8, 2014, after taking off from Kuala Lumpur on a flight towards Beijing with 239 people on board. The investigation into its disappearance is ongoing.

Text.findreplace(". " ".[]" 8)
int i; str s
ARRAY(str) a=Text
for i 0 a.len
,s+=_s.from(GoogleTranslator(a[i] "ko" "en") "[]")
s.findreplace("\''" "''" 8)
spe 20
mes s
#9
http://www.codeproject.com/Articles/127 ... Translator


// Initialize
this.Error = null;
this.TranslationSpeechUrl = null;
this.TranslationTime = TimeSpan.Zero;
DateTime tmStart = DateTime.Now;
string translation = string.Empty;

try {
// Download translation
string url = string.Format ("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
Translator.LanguageEnumToIdentifier (sourceLanguage),
Translator.LanguageEnumToIdentifier (targetLanguage),
HttpUtility.UrlEncode (sourceText));
string outputFile = Path.GetTempFileName();
using (WebClient wc = new WebClient ()) {
wc.Headers.Add ("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
wc.DownloadFile(url, outputFile);
}

// Get translated text
if (File.Exists (outputFile)) {

// Get phrase collection
string text = File.ReadAllText(outputFile);
int index = text.IndexOf (string.Format(",,\"{0}\"", Translator.LanguageEnumToIdentifier (sourceLanguage)));
if (index == -1) {
// Translation of single word
int startQuote = text.IndexOf('\"');
if (startQuote != -1) {
int endQuote = text.IndexOf('\"', startQuote + 1);
if (endQuote != -1) {
translation = text.Substring(startQuote + 1, endQuote - startQuote - 1);
}
}
}
else {
// Translation of phrase
text = text.Substring(0, index);
text = text.Replace("],[", ",");
text = text.Replace("]", string.Empty);
text = text.Replace("[", string.Empty);
text = text.Replace("\",\"", "\"");
}

// Get translated phrases
string[] phrases = text.Split (new[] { '\"' }, StringSplitOptions.RemoveEmptyEntries);
for (int i=0; (i < phrases.Count()); i += 2) {
string translatedPhrase = phrases[i];
if (translatedPhrase.StartsWith(",,")) {
i--;
continue;
}
translation += translatedPhrase + " ";
}

// Fix up translation
translation = translation.Trim();
translation = translation.Replace(" ?", "?");
translation = translation.Replace(" !", "!");
translation = translation.Replace(" ,", ",");
translation = translation.Replace(" .", ".");
translation = translation.Replace(" ;", ";");

// And translation speech URL
this.TranslationSpeechUrl = string.Format ("https://translate.googleapis.com/translate_tts?ie=UTF-8&q={0}&tl={1}&total=1&idx=0&textlen={2}&client=gtx",
HttpUtility.UrlEncode (translation),
Translator.LanguageEnumToIdentifier (targetLanguage),
translation.Length);
}
}
catch (Exception ex) {
this.Error = ex;
}

// Return result
this.TranslationTime = DateTime.Now - tmStart;
return translation;
#10
I added more parsing to the AlexZ function, now should work well with long text.

Function GoogleTranslator
Code:
Copy      Help
;/
function'str str'sourceText str'targetLang [str'sourceLang]

lpstr PatternLanguageCode="^(af|ga|sq|it|ar|ja|az|kn|eu|ko|bn|la|be|lv|bg|lt|ca|mk|zh\-CN|ms|zh\-TW|mt|hr|no|cs|fa|da|pl|nl|pt|en|ro|eo|ru|et|sr|tl|sk|fi|sl|fr|es|gl|sw|ka|sv|de|ta|el|te|gu|th|ht|tr|iw|uk|hi|ur|hu|vi|is|cy|id|yi)$"

if findrx(targetLang PatternLanguageCode)<0
,end F"targetLang: '{targetLang}' Language Code not supported"

if empty(sourceLang)
,sourceLang="auto"

if sourceLang<>"auto" and findrx(sourceLang PatternLanguageCode)<0
,end F"sourceLang: '{sourceLang}' Language Code not supported"

sourceText.escape(11)

str s

IntGetFile F"https://translate.googleapis.com/translate_a/single?client=gtx&sl={sourceLang}&tl={targetLang}&dt=t&q={sourceText}" s

int i=find(s "]],,''")
if(i>0) s.get(s 2 i) ;;phrase, else word

s.findreplace("\''" "[1]")
s.replacerx("\[''([^'']+)'',''[^'']+'',,,\d\](,|\]$)" "$1")
s.findreplace("[1]" "''")
s.findreplace("\r" "[13]")
s.findreplace("\n" "[10]")
s.findreplace("\t" "[9]")
s.findreplace("\\" "\")
s.findreplace("\u003c" "<")
s.findreplace("\u003e" ">")
if(!_unicode) s.ConvertEncoding(CP_UTF8 CP_ACP)

ret s
#11
Do you know why:

out GoogleTranslator("España" "en" "es")

--->

Espaà ± a

?
#12
The Google service does not know that the text in URL is UTF8, and I don't know how to tell it.

See also:
http://stackoverflow.com/questions/3245 ... ranslation
#13
Thank you for the help.

I've updated the Translator.

Regards Smile


Forum Jump:


Users browsing this thread: 1 Guest(s)