【问题标题】:Google translate with c谷歌翻译用 c
【发布时间】:2011-01-05 04:48:49
【问题描述】:

任何人都知道如何使用 c 使用 Google 翻译 API

【问题讨论】:

    标签: c api google-translate


    【解决方案1】:

    有一个可用的 REST API,描述为 here。你应该可以很容易地从 C 中访问它。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2012-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-26
        • 2011-12-26
        • 2015-05-19
        • 2011-06-08
        相关资源
        最近更新 更多