2
任何人都知道如何使用谷歌翻譯API使用c谷歌翻譯與c
任何人都知道如何使用谷歌翻譯API使用c谷歌翻譯與c
有一個REST API可用描述here。你應該能夠
感謝訪問,可以輕鬆地從夠℃到pwc
我用自己的資源,並與管道創建它,這裏是它
char chrptr_GoogleResponse [0x1000];
char* chrptr_pos2 = NULL;
char* translate_text = search_str;
char* lang_pairs = "&langpair=es%7Cen'"; // language pairs
bool boNoError = true;
strcpy(chrarray_GoogleCommand, "curl -s -e http://www.my-ajax-site.com \
'https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=");
strcat(chrarray_GoogleCommand, translate_text);
strcat(chrarray_GoogleCommand, lang_pairs);
popen(chrarray_GoogleCommand, "r");
FILE* fileptrFile = popen(chrarray_GoogleCommand, "r");
if (fileptrFile == NULL)
{
printf("Error on opening pipe\n.");
exit(EXIT_FAILURE);
}
while (!feof (fileptrFile))
{
fgets (chrptr_GoogleResponse , 0x1000 , fileptrFile) ;
chrptr_pos1 = strstr(chrptr_GoogleResponse, "{\"translatedText\":\"") ;
if (chrptr_pos1)
{
chrptr_pos1 = chrptr_pos1 + strlen("{\"translatedText\":\"") ;
chrptr_pos2 = strstr(chrptr_GoogleResponse, "\"}, \"responseDetails\": null, \"responseStatus\": 200}") ;
if (chrptr_pos2)
{
memcpy(chrptr_temp, chrptr_pos1, chrptr_pos2 - chrptr_pos1);
memset((void*) ((unsigned long) chrptr_temp + (unsigned long) chrptr_pos2 - (unsigned long) chrptr_pos1), 0, 1);
}
else
boNoError = false ;
}
else
boNoError = false ;
if (feof (fileptrFile))
break;
}
pclose(fileptrFile);
if (boNoError)
strcpy(search_str, chrptr_temp); //copy translated text.
源代碼