Question

Any one know how to use the Google translate API using c

Was it helpful?

Solution

There is a REST API available described here. You should be able to access that easily enough from C.

OTHER TIPS

Thanks to pwc i used his resource and created it with pipes and here is the source code for it

  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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top