본문 바로가기
IT/Azure

Azure Translator 샘플코드 (C#)

by rapker 2023. 4. 21.
반응형

 

// 샘플코드 https://docs.microsoft.com/ko-kr/azure/cognitive-services/translator/quickstart-translator?tabs=csharp#translate-text 
// 지원언어 https://docs.microsoft.com/ko-kr/azure/cognitive-services/translator/language-support#text-translation 
using System; 
using System.Net.Http; 
using System.Text; 
using System.Threading.Tasks; 
using Newtonsoft.Json; // Install Newtonsoft.Json with NuGet 
namespace translatorExample 
{ 
    class Program 
    { 
        private static readonly string subscriptionKey = "b2951cff1ff34306b1a04d1797802e99"; 
        private static readonly string endpoint = "https://api.cognitive.microsofttranslator.com/"; 
        // Add your location, also known as region. The default is global. 
        // This is required if using a Cognitive Services resource. 
        private static readonly string location = "westeurope"; 
        private static string route = ""; 
        static async Task Main(string[] args) 
        { 
            // Input and output languages are defined as parameters. 
            //string route = "/translate?api-version=3.0&from=en&to=de&to=it";      // 원본언어 지정 
            //string route = "/translate?api-version=3.0&to=de&to=it";              // 원본언어 감지 
            //string route = "/translate?api-version=3.0&to=ko";                    // 원본언어 감지로 한국어 번역해봄 
            //string route = "/detect?api-version=3.0";                             // 번역없이 언어 감지 
            SetRouteLanguage("en", "ko"); 
            await ReqTranslate("Hello, world!"); 
            await ReqTranslate("How are you?"); 
        } 
        static void SetRouteLanguage(string _from, string _to) 
        { 
            route = string.Format("/translate?api-version=3.0&from={0}&to={1}", _from, _to); 
        } 
        static async Task ReqTranslate(string _msg) 
        { 
            object[] body = new object[] { new { Text = _msg } }; 
            var requestBody = JsonConvert.SerializeObject(body); 
            using (var client = new HttpClient()) 
            using (var request = new HttpRequestMessage()) 
            { 
                 
                // Build the request. 
                request.Method = HttpMethod.Post; 
                request.RequestUri = new Uri(endpoint + route); 
                request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json"); 
                request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey); 
                request.Headers.Add("Ocp-Apim-Subscription-Region", location); 
                // Send the request and get response. 
                HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false); 
                // Read response as a string. 
                string result = await response.Content.ReadAsStringAsync(); 
                Console.WriteLine(result); 
            } 
        } 
    } 
}
반응형
LIST

'IT > Azure' 카테고리의 다른 글

Azure SQL MI failover 테스트  (0) 2023.04.24
WebApp Go언어 지원  (0) 2023.04.21
Azure Migrate 자료조사  (0) 2023.04.21
[Azure] Blockchain Service 자료조사  (0) 2023.04.21
App Service (Web App) - 화이트 리스트 구성  (0) 2023.04.21