I'm always looking up words/phrases (usually en español ou em português).
(I know this is pointless since a thing called Google Translate exists, but this is a way for me to mess around with Go on something I might use at one point or another.)
go install
go build
./translate-on-the-go
- test it out on port 5000 (or whatever endpoint/port you decide to use) with one of the routes
- GET
/
- Index route - responds with the available routes - GET
/list-languages?target={target_language_code}
- lists all possible languages to translate to - POST
/translate
- translate text to a target language
target language code info: https://cloud.google.com/translate/docs/languages
1 - list languages with a target of english (en
)
request:
curl --request GET \
--url 'http://localhost:5000/list-languages?target=en'
response (truncated here for display purposes):
[
{
"Name": "Afrikaans",
"Tag": "af"
},
{
"Name": "Albanian",
"Tag": "sq"
},
{
"Name": "Amharic",
"Tag": "am"
},
{
"Name": "Arabic",
"Tag": "ar"
}
]
2 - translate the word "hello" from english to português
request:
curl --request POST \
--url http://localhost:5000/translate \
--header 'content-type: application/json' \
--data '{
"lang": "pt",
"text": "hello"
}'
response:
{
"response": {
"sourceLanguage": "en",
"targetLanguage": "pt",
"translatedText": "Olá"
}
}
3 - the index/base route to list the available routes
request:
curl --request GET --url http://localhost:5000/
response:
{
"routes": {
"/list-languages": "GET",
"/translate": "POST"
}
}
- Google Translate
- Google Cloud Translation API - responsible for all translations
- webapp-with-golang-anti-textbook
- Getting Started with Redis and Go - Tutorial
- just for func