Posted in
Solutions
,
SQL SERVER
, tagged
API
,
google translate
,
OPENJSON
,
sp_OACreate
,
sp_OAMethod
,
SQL Server
,
stored procedure
,
Translate text
on April 5, 2023|
Leave a Comment »
CREATE OR ALTER PROCEDURE sp_translate_text
@TextToBeTranslated NVARCHAR(500),
@SourceLanguage NVARCHAR(50),
@TargetLanguage NVARCHAR(50),
@Key NVARCHAR(250) = 'Please use Translate API key provided by google'
DECLARE @URL NVARCHAR(250) = 'https://translation.googleapis.com/language/translate/v2';
DECLARE @ConsolidatedURL NVARCHAR(250) = CONCAT(@URL,'?key=',@Key);
DECLARE @Object INT;
DECLARE @Json TABLE(DATA NVARCHAR(MAX));
DECLARE @ResponseText NVARCHAR(MAX);
DECLARE @Body NVARCHAR(MAX) = CONCAT('{','"q":','"',@TextToBeTranslated,'"',',','"target":','"',@TargetLanguage,'"',',','"source":','"',@SourceLanguage,'"','}');
EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
EXEC sp_OAMethod @Object, 'OPEN', NULL, 'POST', @ConsolidatedURL, 'FALSE';
EXEC sp_OAMethod @Object, 'SETREQUESTHEADER', NULL, 'CONTENT-TYPE', 'APPLICATION/JSON';
EXEC sp_OAMethod @Object, 'SEND', NULL, @Body;
EXEC sp_OAMethod @Object, 'RESPONSETEXT', @ResponseText OUTPUT , @Json OUTPUT;
INSERT INTO @Json exec sp_OAGetProperty @Object, 'RESPONSETEXT';
DECLARE @Data NVARCHAR(MAX) = (SELECT DATA FROM @Json);
SELECT @SourceLanguage AS [Source Language]
, @TargetLanguage AS [Target Language]
, TranslatedText AS [Translated Text]
FROM OPENJSON(@Data,'$.data.translations')
[TranslatedText] NVARCHAR(500) '$.translatedText'
I found Google translate API very useful whenever I need to convert any text from one language to another. It works very smoothly. Do let me know if you use it and how did you find it.
Read Full Post »
SQL Server – Fix – Error – This backend version is not supported to design database diagrams or tables
SQL Server – How to translate text from one language to another using google translate
SQL Server – How to compare files in SQL Server Management Studio (SSMS)
SQL Server – How to truncate database log file
Archives
April 2023
(5)
March 2023
(9)
January 2023
(1)
December 2022
(12)
November 2022
(17)
August 2020
(1)
June 2016
(1)
December 2015
(1)
September 2015
(2)
August 2015
(3)
July 2015
(1)
June 2015
(3)
May 2015
(3)
April 2015
(1)
March 2015
(3)
January 2015
(5)
December 2014
(8)
November 2014
(2)
July 2014
(4)
June 2014
(3)
May 2014
(4)
April 2014
(5)
March 2014
(10)
February 2014
(13)
January 2014
(14)
December 2013
(13)
November 2013
(13)
October 2013
(8)
September 2013
(23)
August 2013
(15)
July 2013
(26)
June 2013
(25)
May 2013
(28)
April 2013
(27)
March 2013
(26)
February 2013
(25)
January 2013
(22)
December 2012
(7)
November 2012
(2)
October 2012
(24)
September 2012
(11)
August 2012
(9)
July 2012
(5)
June 2012
(1)
May 2012
(1)
April 2012
(5)
February 2012
(3)
January 2012
(1)
December 2011
(2)
October 2011
(1)
Categories
Azure Data Studio
(2)
SQL SERVER
(452)
Audit
(6)
Backup & Restore
(4)
Data Definition Language (DDL) Statements
(1)
Database
(2)
DML
(5)
Dynamic Management Views
(2)
Errors
(70)
Extension
(2)
Features
(10)
Functions
(59)
Performance
(6)
Raresql News
(9)
Security Enhancement
(2)
SET Statements
(1)
Solutions
(228)
System Stored Procedure
(8)
Tables
(6)
Tips
(26)
Tools
(5)
Trainings
(3)
Triggers
(1)
Uncategorized
(2)
Enter your email address to follow this blog and receive notifications of new posts by email.
Email Address:
Disclaimer
This blog/website is a personal blog/website and all articles, postings and opinions contained herein are my own. The contents of this blog/website are not intended to defame, purge or humiliate anyone should they decide to act upon or reuse any information provided by me. Comments left by any independent reader are the sole responsibility of that person. Should you identify any content that is harmful, malicious, sensitive or unnecessary, please contact me via email ([email protected]) so I may rectify the problem.