As the title suggests, recently, ChatGPT is becoming popular. However, ChatGPT is more accurate in responding when asked in English rather than in Japanese. Therefore, there are times when you want to quickly translate from Japanese to English to ask questions to ChatGPT.
Snippet to call DeepL translation from Crystsal language
By giving reference code and reference documents to ChatGPT from Crystal language, a language to call DeepL's API was written. An API Key is required for use.
require "http/client"
require "json"
module Deepl
class ApiKeyError < Exception; end
class RequestError < Exception; end
class Translator
API_ENDPOINT = "https://api-free.deepl.com/v2/translate"
@http_headers : HTTP::Headers
def initialize
@http_headers = build_http_headers
end
def build_http_headers
HTTP::Headers{
"Authorization" => "DeepL-Auth-Key #{get_api_key}",
"Content-Type" => "application/x-www-form-urlencoded",
}
end
def get_api_key
if ENV.has_key?("DEEPL_API_KEY")
ENV["DEEPL_API_KEY"]
else
raise ApiKeyError.new
end
end
def request_translation(text : String, target_lang : String)
request_payload = "text=#{URI.encode_www_form(text)}&target_lang=#{URI.encode_www_form(target_lang)}"
send_post_request(request_payload)
end
def send_post_request(request_data : String)
HTTP::Client.post(API_ENDPOINT, body: request_data, headers: @http_headers)
rescue error
raise RequestError.new("Error: #{error} #{error.message}")
end
def translate(text, target_lang)
response = request_translation(text, target_lang)
parsed_response = JSON.parse(response.body)
parsed_response.dig("translations", 0, "text")
end
end
end
The Example Response looks like this:
{
"translations": [
{
"detected_source_language": "EN",
"text": "Hallo, Welt!"
}
]
}
You can use it like this:
input_text = ARGV[0]
target_lang = "ZH"
translator = Deepl::Translator.new
translated_text = translator.translate(input_text, target_lang)
puts translated_text
./deepl Hello.
你好。
That's all. Have a nice day.
Top comments (0)