translation
    • PDF

    translation

    • PDF

    Article Summary

    Overview

    Papago Text Translation API is a translation REST API implemented into NAVER Papago. It translates input text into another language (English and Chinese) and outputs it. Because it is a non-login open API, the client ID and client secret value issued when registering the application must be transferred together in the HTTP header when invoking with POST.
    The results of the Papago Text Translation API can be supplemented with glossaries, honorifics, and term replacement options.

    Requests

    • Request in a form format
    curl -i -X POST \
       -H "X-NCP-APIGW-API-KEY-ID:{Client ID issued when registering the app}" \
       -H "X-NCP-APIGW-API-KEY:{Client Secret issued when registering the app}" \
       -H "Content-Type:application/x-www-form-urlencoded" \
       -d "source={source language code}" \
       -d "target={target language code}" \
       -d "text={text to translate}" \
     'https://naveropenapi.apigw.ntruss.com/nmt/v1/translation'
    
    • Request in JSON format
    curl -i -X POST \
       -H "X-NCP-APIGW-API-KEY-ID:{Client ID issued when registering the app}" \
       -H "X-NCP-APIGW-API-KEY:{Client Secret issued when registering the app}" \
       -H "Content-Type:application/json" \
       -d \
    '{
      "source": "{source language code}",
      "target": "{target language code}",
      "text": "{text to translate}"
    }' \
     'https://naveropenapi.apigw.ntruss.com/nmt/v1/translation'
    

    Request parameters

    This API does not require any request parameters.

    Request header

    Header nameDescription
    X-NCP-APIGW-API-KEY-IDClient ID issued when registering the app
    X-NCP-APIGW-API-KEY-ID:{Client ID}
    X-NCP-APIGW-API-KEYClient secret issued when registering the app
    X-NCP-APIGW-API-KEY:{Client Secret}
    Content-TypeType of the content to be transferred
    Content-Type:application/x-www-form-urlencoded
    Content-Type:application/json

    Request body

    ParameterTypeRequirement statusDescription
    sourceStringYSource language code. If set to auto, the source language is automatically detected
    targetStringYTarget language's language code
    textStringY- Text to be translated
    - Translates up to 5000 characters per request
    glossaryKeyStringN- If the glossary feature is used on the Cloud console, it responds to the glossaryKey of the glossary (e.g., 6ae8eba5-3154-4646-bc16-1c96532082e6)
    - Customized translation is applied based on the glossary data
    - Korean⇔English, Korean⇔Japanese, Korean⇔Chinese (simplified/traditional), English⇔Japanese, English⇔Chinese (simplified.traditional), Japanese⇔Chinese (simplified/traditional) available
    - When used simultaneously with replaceInfo, replaceInfo is treated with higher priority
    - Honorifics do not apply to terms registered in the glossary
    replaceInfoStringN- Specifies term replacement (including spaces)
    - Specifies the translation of the designated index If str is not set, original text is provided
    - Korean⇔English, Korean⇔Japanese, Korean⇔Chinese (simplified/traditional), English⇔Japanese, English⇔Chinese (simplified.traditional), Japanese⇔Chinese (simplified/traditional) available
    honorificBooleanN- Honorifics option (False by default)
    Only available in one direction: English⇒Korean, Japanese⇒Korean, Chinese (simplified/traditional)⇒Korean, Korean⇒Japanese, English⇒Japanese, Chinese (simplified/traditional)⇒Japanese

    *In the case of text that should not be translated, the text can be specified using the html span tag. Tags are not included in the character count.
    *Supported languages are the same as glossaryKey and replaceInfo.

     <span translate="no"> </span> or <span class="notranslate"> </span>
    

    Language codes (Source and target)

    LanguageLanguage code
    Koreanko
    Englishen
    Japaneseja
    Chinese (simplified)zh-CN
    Chinese (traditional)zh-TW
    Vietnamesevi
    Thaith
    Indonesianid
    Frenchfr
    Spanishes
    Russianru
    Germande
    Italianit

    Supported languages

    Supported languages
    Korean (ko)
    English(en), Japanese (ja), Chinese (Simplified) (zh-CN) /Chinese (Traditional) (zh-TW),
    Vietnamese (vi), Thai (th), Indonesian (id), French (fr)
    Spanish (es), Russian (ru), German (de), Italian (it)
    English (en)
    Japanese (ja), Chinese (Simplified) (zh-CN) /Chinese (Traditional) (zh-TW),
    Vietnamese (vi), Thai (th), Indonesian (id), French (fr)
    Japanese (ja)
    Chinese (Simplified)/Chinese (Traditional),
    Vietnamese (vi), Thai (th), Indonesian (id), French (fr)
    Simplified Chinese (zh-CN)
    Traditional Chinese (zh-TW)

    Response

    Response bodies

    Field nameTypeDescription
    srcLangTypestringSource language code
    tarLangTypestringTarget language code
    translatedTextstringTranslated sentence

    Examples

    Request examples

    POST /nmt/v1/translation HTTP/1.1
    HOST: naveropenapi.apigw.ntruss.com
    User-Agent: curl/7.49.1
    Accept: */*
    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    X-NCP-APIGW-API-KEY-ID: {Client ID issued when registering the app}
    X-NCP-APIGW-API-KEY: {Client Secret issued when registering the app}
    Content-Length: 51
    
    curl -i -X POST \
       -H "X-NCP-APIGW-API-KEY-ID:kf4a7jfark" \
       -H "X-NCP-APIGW-API-KEY:B8oczJX6DJiorOGcN3UbuylsJFUGqGXFaTxIIemY" \
       -H "Content-Type:application/x-www-form-urlencoded" \
       -d "source=en" \
       -d "target=ko" \
       -d "text=hello world" \
     'https://naveropenapi.apigw.ntruss.com/nmt/v1/translation'
    

    Response examples

    {
        "message": {
            "result": {
                "srcLangType": "en",
                "tarLangType": "ko",
                "translatedText": "Papago is the best translator"
            }
        }
    }
    

    API examples

    // Example of Naver Papago Text Translation API
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLEncoder;
    public class APIExamTranslate {
    
        public static void main(String[] args) {
            String clientId = "YOUR_CLIENT_ID";//Application client ID value";
            String clientSecret = "YOUR_CLIENT_SECRET";//Application client secret value";
            try {
                String text = URLEncoder.encode("Nice to meet you.", "UTF-8");
                String apiURL = "https://naveropenapi.apigw.ntruss.com/nmt/v1/translation";
                URL url = new URL(apiURL);
                HttpURLConnection con = (HttpURLConnection)url.openConnection();
                con.setRequestMethod("POST");
                con.setRequestProperty("X-NCP-APIGW-API-KEY-ID", clientId);
                con.setRequestProperty("X-NCP-APIGW-API-KEY", clientSecret);
                // post request
                String postParams = "source=ko&target=en&text=" + text;
                con.setDoOutput(true);
                DataOutputStream wr = new DataOutputStream(con.getOutputStream());
                wr.writeBytes(postParams);
                wr.flush();
                wr.close();
                int responseCode = con.getResponseCode();
                BufferedReader br;
                if(responseCode==200) { // Successful call
                    br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                } else {  // Error occurred
                    br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
                }
                String inputLine;
                StringBuffer response = new StringBuffer();
                while ((inputLine = br.readLine()) != null) {
                    response.append(inputLine);
                }
                br.close();
                System.out.println(response.toString());
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
    
    // Example of Naver Papago Text Translation API
    <?php
      $client_id = "YOUR_CLIENT_ID";
      $client_secret = "YOUR_CLIENT_SECRET";
      $encText = urlencode("Nice to meet you.");
      $postvars = "source=ko&target=en&text=".$encText;
      $url = "https://naveropenapi.apigw.ntruss.com/nmt/v1/translation";
      $is_post = true;
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_POST, $is_post);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($ch,CURLOPT_POSTFIELDS, $postvars);
      $headers = array();
      $headers[] = "X-NCP-APIGW-API-KEY-ID: ".$client_id;
      $headers[] = "X-NCP-APIGW-API-KEY: ".$client_secret;
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      $response = curl_exec ($ch);
      $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      echo "status_code:".$status_code."<br/>";
      curl_close ($ch);
      if($status_code == 200) {
        echo $response;
      } else {
        echo "Error content:".$response;
      }
    ?>
    
    // Example of Naver Papago Text Translation API
    var express = require('express');
    var app = express();
    var client_id = 'YOUR_CLIENT_ID';
    var client_secret = 'YOUR_CLIENT_SECRET';
    var query = 'Enter a sentence to translate.';
    app.get('/translate', function(req, res) {
      var api_url = 'https://naveropenapi.apigw.ntruss.com/nmt/v1/translation';
      var request = require('request');
      var options = {
        url: api_url,
        form: { source: 'ko', target: 'en', text: query },
        headers: { 'X-NCP-APIGW-API-KEY-ID': client_id, 'X-NCP-APIGW-API-KEY': client_secret },
      };
      request.post(options, function(error, response, body) {
        if (!error && response.statusCode == 200) {
          res.writeHead(200, { 'Content-Type': 'text/json;charset=utf-8' });
          res.end(body);
        } else {
          res.status(response.statusCode).end();
          console.log('error = ' + response.statusCode);
        }
      });
    });
    app.listen(3000, function() {
      console.log('http://127.0.0.1:3000/translate app listening on port 3000!');
    });
    
    // Example of Naver Papago Text Translation API
    import os
    import sys
    import urllib.request
    client_id = "YOUR_CLIENT_ID"
    client_secret = "YOUR_CLIENT_SECRET"
    encText = urllib.parse.quote("Enter a sentence to translate")
    data = "source=ko&target=en&text=" + encText
    url = "https://naveropenapi.apigw.ntruss.com/nmt/v1/translation"
    request = urllib.request.Request(url)
    request.add_header("X-NCP-APIGW-API-KEY-ID",client_id)
    request.add_header("X-NCP-APIGW-API-KEY",client_secret)
    response = urllib.request.urlopen(request, data=data.encode("utf-8"))
    rescode = response.getcode()
    if(rescode==200):
        response_body = response.read()
        print(response_body.decode('utf-8'))
    else:
        print("Error Code:" + rescode)
    
    // Example of Naver Papago Text Translation API
    using System;
    using System.Net;
    using System.Text;
    using System.IO;
    
    namespace NaverAPI_Guide
    {
        public class APIExamTranslate
        {
            static void Main(string[] args)
            {
                string url = "https://naveropenapi.apigw.ntruss.com/nmt/v1/translation";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Headers.Add("X-NCP-APIGW-API-KEY-ID", "YOUR-CLIENT-ID");
                request.Headers.Add("X-NCP-APIGW-API-KEY", "YOUR-CLIENT-SECRET");
                request.Method = "POST";
                string query = "How's the weather today?";
                byte[] byteDataParams = Encoding.UTF8.GetBytes("source=ko&target=en&text=" + query);
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = byteDataParams.Length;
                Stream st = request.GetRequestStream();
                st.Write(byteDataParams, 0, byteDataParams.Length);
                st.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                string text = reader.ReadToEnd();
                stream.Close();
                response.Close();
                reader.Close();
                Console.WriteLine(text);
            }
        }
    }
    

    Example of customize translation option (replaceInfo / html span tag) application

    Indexes 0 – 3 are source text copy and 5 – 8 are designated as test.
    replaceInfo={"infos":[{"begin":0,"length":4},{"begin":5,"length":4,"str":"test"}]}
    
    - I love <span translate="no">#BTS</span>
    

    Errors

    HttpStatusCodeErrorCodeErrorMessageDescription
    400N2MT01source parameter is neededA source parameter is required
    400N2MT02Unsupported source languageThe source language is unsupported
    400N2MT03target parameter is neededA target parameter is required
    400N2MT04Unsupported target languageThe target language is unsupported
    400N2MT05source and target must be differentThe source and target are the same
    400N2MT06There is no source-to-target translatorNo source-to-target translator found
    400N2MT07text parameter is neededA text parameter is required
    400N2MT08text parameter exceeds max lengthThe text parameter exceeds the maximum capacity
    400N2MT09language detection failedFailed to detect language
    400N2MT10invalid glossary keyInvalid glossary key
    500N2MT99Internal server errorsInternal server error

    Was this article helpful?

    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.