谷歌翻译c

时间:2011-01-05 04:48:49

标签: c api google-translate

任何人都知道如何使用c

来使用Google翻译API

2 个答案:

答案 0 :(得分:5)

有一个描述here的REST API。你应该能够从C语言中轻松访问它。

答案 1 :(得分:1)

感谢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.