plugin.description='Translate text between languages'
plugin.commands={'translate','tl'}
plugin.help='/translate [lang] <text> - Translate text to the specified language (default: en). Reply to a message to translate it, or provide text directly.'
localhttps=require('ssl.https')
localjson=require('dkjson')
localurl=require('socket.url')
localltn12=require('ltn12')
localtools=require('telegram-bot-lua.tools')
localBASE_URL='https://libretranslate.com'
-- Common language code aliases
localLANG_ALIASES={
english='en',en='en',
spanish='es',es='es',
french='fr',fr='fr',
german='de',de='de',
italian='it',it='it',
portuguese='pt',pt='pt',
russian='ru',ru='ru',
chinese='zh',zh='zh',
japanese='ja',ja='ja',
korean='ko',ko='ko',
arabic='ar',ar='ar',
hindi='hi',hi='hi',
dutch='nl',nl='nl',
polish='pl',pl='pl',
turkish='tr',tr='tr',
swedish='sv',sv='sv',
czech='cs',cs='cs',
romanian='ro',ro='ro',
hungarian='hu',hu='hu',
ukrainian='uk',uk='uk',
indonesian='id',id='id',
finnish='fi',fi='fi',
hebrew='he',he='he',
thai='th',th='th',
vietnamese='vi',vi='vi',
greek='el',el='el'
}
localfunctiontranslate_text(text,target,source)
source=sourceor'auto'
localrequest_body=json.encode({
q=text,
source=source,
target=target,
format='text'
})
localbody={}
local_,code=https.request({
url=BASE_URL..'/translate',
method='POST',
headers={
['Content-Type']='application/json',
['Content-Length']=tostring(#request_body)
},
source=ltn12.source.string(request_body),
sink=ltn12.sink.table(body)
})
ifcode~=200then
returnnil,'Translation service returned an error (HTTP '..tostring(code)..'). The public instance may be rate-limited; try again shortly.'