Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F620009
translate.lua
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None
translate.lua
View Options
--[[
mattata v2.0 - Translate Plugin
Translates text using LibreTranslate public API.
Supports auto-detection of source language.
]]
local
plugin
=
{}
plugin
.
name
=
'translate'
plugin
.
category
=
'utility'
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.'
local
http
=
require
(
'src.core.http'
)
local
json
=
require
(
'dkjson'
)
local
tools
=
require
(
'telegram-bot-lua.tools'
)
local
BASE_URL
=
'https://libretranslate.com'
-- Common language code aliases
local
LANG_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'
}
local
function
translate_text
(
text
,
target
,
source
)
source
=
source
or
'auto'
local
request_body
=
json
.
encode
({
q
=
text
,
source
=
source
,
target
=
target
,
format
=
'text'
})
local
body
,
code
=
http
.
post
(
BASE_URL
..
'/translate'
,
request_body
,
'application/json'
)
if
code
~=
200
then
return
nil
,
'Translation service returned an error (HTTP '
..
tostring
(
code
)
..
'). The public instance may be rate-limited; try again shortly.'
end
local
data
=
json
.
decode
(
body
)
if
not
data
then
return
nil
,
'Failed to parse translation response.'
end
if
data
.
error
then
return
nil
,
'Translation error: '
..
tostring
(
data
.
error
)
end
return
{
translated
=
data
.
translatedText
,
source_lang
=
data
.
detectedLanguage
and
data
.
detectedLanguage
.
language
or
source
}
end
function
plugin
.
on_message
(
api
,
message
,
ctx
)
local
input
=
message
.
args
local
text_to_translate
local
target_lang
=
'en'
-- If replying to a message, use that text
if
message
.
reply
and
message
.
reply
.
text
and
message
.
reply
.
text
~=
''
then
text_to_translate
=
message
.
reply
.
text
-- If args given, treat as target language
if
input
and
input
~=
''
then
local
lang
=
input
:
match
(
'^(%S+)'
)
if
lang
then
lang
=
lang
:
lower
()
target_lang
=
LANG_ALIASES
[
lang
]
or
lang
end
end
elseif
input
and
input
~=
''
then
-- Parse: /translate [lang] <text>
local
first_word
,
rest
=
input
:
match
(
'^(%S+)%s+(.+)$'
)
if
first_word
then
local
resolved
=
LANG_ALIASES
[
first_word
:
lower
()]
if
resolved
then
target_lang
=
resolved
text_to_translate
=
rest
elseif
first_word
:
match
(
'^%a%a$'
)
or
first_word
:
match
(
'^%a%a%a$'
)
then
-- Assume it's a language code even if not in our alias table
target_lang
=
first_word
:
lower
()
text_to_translate
=
rest
else
-- No language specified, translate the whole input to English
text_to_translate
=
input
end
else
text_to_translate
=
input
end
end
if
not
text_to_translate
or
text_to_translate
==
''
then
return
api
.
send_message
(
message
.
chat
.
id
,
'Please provide text to translate.
\n
Usage: <code>/translate [lang] text</code>
\n
Or reply to a message with <code>/translate [lang]</code>'
,
{
parse_mode
=
'html'
}
)
end
local
result
,
err
=
translate_text
(
text_to_translate
,
target_lang
)
if
not
result
then
return
api
.
send_message
(
message
.
chat
.
id
,
err
)
end
local
source_label
=
result
.
source_lang
~=
'auto'
and
result
.
source_lang
:
upper
()
or
'??'
local
output
=
string.format
(
'<b>Translation</b> [%s -> %s]
\n\n
%s'
,
tools
.
escape_html
(
source_label
),
tools
.
escape_html
(
target_lang
:
upper
()),
tools
.
escape_html
(
result
.
translated
)
)
return
api
.
send_message
(
message
.
chat
.
id
,
output
,
{
parse_mode
=
'html'
})
end
return
plugin
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Nov 14, 5:27 PM (12 h, 36 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
111407
Default Alt Text
translate.lua (4 KB)
Attached To
Mode
R69 mattata
Attached
Detach File
Event Timeline