Watch, Follow, &
Connect with Us

Welcome, Guest
Guest Settings
Help

Thread: Accessing portal.azure translation API



Permlink Replies: 4 - Last Post: Apr 20, 2017 6:10 AM Last Post By: Bernd Maierhofer Threads: [ Previous | Next ]
Bernd Maierhofer

Posts: 122
Registered: 9/27/99
Accessing portal.azure translation API
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 3, 2017 2:14 AM
Hi + tx for every comment!

I used to access the MS translator API according
https://delphiaball.co.uk/2015/06/26/using-azure-translator-services-wit
h-delphi/

However, MS phased https://datamarket.azure.com out and replaced it
with https://portal.azure.com

Has anybody some infos on how to translate text using the new API?

Many thanks!
Bernd

--
Dave Nottage

Posts: 1,354
Registered: 1/7/00
Re: Accessing portal.azure translation API
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 3, 2017 2:23 AM   in response to: Bernd Maierhofer in response to: Bernd Maierhofer
Bernd Maierhofer wrote:

I used to access the MS translator API according
https://delphiaball.co.uk/2015/06/26/using-azure-translator-services-wit
h-delphi/

However, MS phased https://datamarket.azure.com out and replaced it
with https://portal.azure.com

Has anybody some infos on how to translate text using the new API?

I might be able to help if I had a login. Does the new portal not use REST APIs? If it does, it might just be a case of
going through the same process of discovering the end points and the required parameters (as per Stephen Ball's article)

--
Dave Nottage [MVP, TeamB]
Hints, tips and tricks at: http://www.delphiworlds.com/blog
Bernd Maierhofer

Posts: 122
Registered: 9/27/99
Re: Accessing portal.azure translation API
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 4, 2017 2:02 AM   in response to: Dave Nottage in response to: Dave Nottage
Dave Nottage wrote:

Bernd Maierhofer wrote:

I used to access the MS translator API according
https://delphiaball.co.uk/2015/06/26/using-azure-translator-services
-wit h-delphi/

However, MS phased https://datamarket.azure.com out and replaced it
with https://portal.azure.com

Has anybody some infos on how to translate text using the new API?

I might be able to help if I had a login. Does the new portal not use
REST APIs? If it does, it might just be a case of going through the
same process of discovering the end points and the required
parameters (as per Stephen Ball's article)

Yes, it still is an REST-Api.

--
Bernd Maierhofer

Posts: 122
Registered: 9/27/99
Re: Accessing portal.azure translation API
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 4, 2017 10:56 PM   in response to: Dave Nottage in response to: Dave Nottage
Dave Nottage wrote:

Bernd Maierhofer wrote:

I used to access the MS translator API according
https://delphiaball.co.uk/2015/06/26/using-azure-translator-services
-wit h-delphi/

However, MS phased https://datamarket.azure.com out and replaced it
with https://portal.azure.com

Has anybody some infos on how to translate text using the new API?

I might be able to help if I had a login. Does the new portal not use
REST APIs? If it does, it might just be a case of going through the
same process of discovering the end points and the required
parameters (as per Stephen Ball's article)

Yes, it still is an REST-Api.

--
Bernd Maierhofer

Posts: 122
Registered: 9/27/99
Re: Accessing portal.azure translation API
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 20, 2017 6:10 AM   in response to: Bernd Maierhofer in response to: Bernd Maierhofer
If someone is interested - see below. Access to the other services is
straightforward.

Cheers Bernd

unit uAzureTranslatorApi;

interface

function TranslateText(const AText, SourceLng, DestLng: string): string;

implementation
uses
MSXML,
ActiveX,
ComObj,
Variants,
IdURI,
SysUtils;

const
n10min: real = 1 / 24 / 6;

var
sActiveToken: string;
dtTokenDate: TDateTime;

function translate(sText, sFrom, sTo: string): string;
const
URIToken = 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken';
URITranslate =
'https://api.microsofttranslator.com/v2/http.svc/Translate';
SubscriptionKey = '<key of service in azure>';
COMPLETED = 4;
OK = 200;
var
XMLHTTPRequest: IXMLHTTPRequest;
sToken: string;
Uri: string;
XMLDOMDocument: IXMLDOMDocument;
begin
Result := '';
try
XMLHTTPRequest := CreateOleObject('MSXML2.XMLHTTP') as
IXMLHTTPRequest;
if (sActiveToken = '') or (Now - dtTokenDate >= n10min) then begin
dtTokenDate:=Now;
XMLHTTPRequest.Open('POST', URIToken, False, '', '');
XMLHTTPRequest.setRequestHeader('Ocp-Apim-Subscription-Key',
SubscriptionKey);
XMLHTTPRequest.send('');
if (XMLHTTPRequest.readyState = COMPLETED) and
(XMLHTTPRequest.status = OK) then
begin
sActiveToken := XMLHTTPRequest.responseText;
end else begin
sActiveToken := '';
end;
end;

Uri := UriTranslate + '?appid=Bearer ' + sActiveToken + '&text=' +
sText + '&from=' + sFrom + '&to=' + sTo;
Uri := TIdURI.PathEncode(Uri);
XMLHTTPRequest.Open('GET', URI, False, '', '');
XMLHTTPRequest.send('');
if (XMLHTTPRequest.readyState = COMPLETED) and
(XMLHTTPRequest.status = OK) then
begin
sToken := XMLHTTPRequest.responseText;
try
XMLDOMDocument := CoDOMDocument.Create;
XMLDOMDocument.loadXML(sToken);
sToken := XMLDOMDocument.Text;
Result := sToken;
finally
XMLDOMDocument := nil;
end;
end;
finally
end;
end;

function TranslateText(const AText, SourceLng, DestLng: string): string;
begin
try
CoInitialize(nil);
finally
Result := translate(AText, SourceLng, DestLng);
CoUninitialize;
end;
end;

end;
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02