locked
Addtranslation - how does it work? RRS feed

  • Question

  • I am trying to use the "AddTranslation" function. I get no errors, but it doesn't seem to do anything. I started with the PHP example at http://msdn.microsoft.com/en-us/library/ff512408.aspx, and added a test translation to see if the "AddTranslation" worked. The program reports that the "AddTranslation" was successful, but then when I call "Translate" it ignores the added translation, and just gives the default translation.

    Am I using the API incorrectly? Is there some example code anywhere that both adds translations AND uses the results?

    Anyway, here is the relevant part of my PHP code. The omitted part is identical to the sample code on the page referenced above.

    try {
      $clientID     = "AbiTalkTranslator";
      $clientSecret = "FQesig5mFkOopme0+eNsFnZlSddf/Msgy/9IwLkj0yI=";
      $authUrl      = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/";
      $scopeUrl     = "http://api.microsofttranslator.com";
      $grantType    = "client_credentials";
      $authObj      = new AccessTokenAuthentication();
      $accessToken  = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl);
      $authHeader = "Authorization: Bearer ". $accessToken;
    
      $originalText   = "booger";
      $translatedText = "concreciones nasales";
      $fromLanguage   = "en";
      $toLanguage     = "es";
      $user           = 'TestUser';
      $category       = 'general';
      $uri            = '';
      $rating         = 3;
      $contentType    ="text/plain";
    
      $params = "originaltext=".urlencode($originalText).
        "&translatedtext=".urlencode($translatedText).
        "&from=".$fromLanguage.
        "&to=".$toLanguage.
        "&user=".$user.
        "&uri=".$uri.
        "&rating=".$rating.
        "&contentType=".$contentType.
        "&category=".$category;
    
      echo("params=$params\n");
    
      $addTranslationArr = "http://api.microsofttranslator.com/V2/Http.svc/AddTranslation?$params";
        
      $translatorObj = new HTTPTranslator();
      $translatorObj->curlRequest($addTranslationArr, $authHeader);
      echo "Translation for <b>'$originalText'</b> added successfully.".PHP_EOL;
      // ------------  Now try to use the added translation -------------------------
      $inputStr     = "booger";
      $contentType  = 'text/plain';
      $category     = 'general';
      $params = "text=".urlencode($inputStr)."&to=".$toLanguage."&from=".$fromLanguage;
      echo("params=$params\n");
      $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?$params";
      $curlResponse = $translatorObj->curlRequest($translateUrl, $authHeader);
      $xmlObj = simplexml_load_string($curlResponse);
      foreach((array)$xmlObj[0] as $val){
        echo "translation={$val}\n";  // This prints "moco" (mucus) instead of "concreciones nasales" (booger)
      }
    } catch (Exception $e) {
        echo "Exception: " . $e->getMessage() . PHP_EOL;
    }
    

    Thanks for any help.

    Friday, December 14, 2012 8:55 PM

Answers

  • Hi AbiTalk,

    Can we change the value of $rating to 6 or higher?  By default, Microsoft Translator output gets the rating of 5 and your added string may not be able to overwrite the output from our translator.   Also, try using GetTranslations().  This method (as opposed to Translate()) allows you to get multiple translation results.  Hope that this is helpful.  Let me know how it goes.  Takako (Microsoft Translator team)

    • Marked as answer by AbiTalk Friday, December 14, 2012 11:42 PM
    Friday, December 14, 2012 11:25 PM

All replies

  • Hi AbiTalk,

    Can we change the value of $rating to 6 or higher?  By default, Microsoft Translator output gets the rating of 5 and your added string may not be able to overwrite the output from our translator.   Also, try using GetTranslations().  This method (as opposed to Translate()) allows you to get multiple translation results.  Hope that this is helpful.  Let me know how it goes.  Takako (Microsoft Translator team)

    • Marked as answer by AbiTalk Friday, December 14, 2012 11:42 PM
    Friday, December 14, 2012 11:25 PM
  • @Takako:  Yes, that worked!  I changed the rating to 9, and now it works as expected.  Thanks.

    Friday, December 14, 2012 11:45 PM