Installing LibreTranslate on an Ubuntu Server

I discovered this great docker package the other day. I think it would be great if OpenSim users or grid owners could provide their own translation services .

If someone wants to offer this as a free service, they would be a real hero. 🙂

Below are the steps I followed to install it on an Ubuntu 18.04 server.

apt update
apt install docker-compose

a2enmod proxy_http
a2enmod proxy

git clone https://github.com/uav4geo/LibreTranslate
cd LibreTranslate
docker-compose up -d --build

docker run -ti --rm -p 5000:5000 libretranslate/libretranslate

To set up a reverse-proxy in Apache 2…





###In /etc/apache2/sites-enabled/your-domain.conf

  #add thins inside your virtual host
  #below   ServerName ...  ServerAlias ... etc.
  
  
    ProxyPreserveHost On
    ProxyRequests Off
    ProxyVia On

    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/


# I think it needs an SSL certificate to run right
#
#  certbot --apache -d yourdomain.com
#  service apache2 restart

Below is an example translation HUD script that I use with my own translation service. It can definitely be improved.

This was adapted from an old Yandex translator that was no longer working.

key owner = NULL_KEY;
string baseurl="https://changetoyourdomain.com/translate";
string fromlang = "en";
string tolang = "it";
integer menuchanA = 14856350;
integer menuchanB = 16871861;
string speaker = "";
string msg = "";
string cname;
key req;

default
{
    state_entry()
    {
        llSetText("Select your language", <1,1,0>, 1);
        owner = llGetOwner();
        llListen(0, "", NULL_KEY, "");
        llListen(menuchanA, "", owner, "");
        llListen(menuchanB, "", owner, "");
        llDialog(owner, "\n\nPlease choose your Native Language:", ["en", "de", "it", "es", "fr", "pt", "nl", "sv", "sq", "ar", "Page 2", "Page 3"], menuchanA);
    }
    
    on_rez(integer num)
    {
        llResetScript();
    } 
    
    touch_start(integer num)
    {
        key id = llDetectedKey(0);
        if (id == owner)
            llDialog(owner, "\n\nPlease choose your Native Language:", ["en", "de", "it", "es", "fr", "pt", "nl", "sv", "sq", "ar", "Page 2", "Page 3"], menuchanA);
    }
    
    listen(integer channel, string name, key id, string message)
    {
        if (channel == 0)
        {
            if (id == owner)
            {
                if (fromlang != tolang)
                {
                    
        
        
        string json;        
        json = llJsonSetValue(json,["q"],message);
        json = llJsonSetValue(json,["source"],fromlang);
        json = llJsonSetValue(json,["target"],tolang);
        json = llJsonSetValue(json,["format"],"text");
        json = llJsonSetValue(json,["api_key"],"");       
        list httphead = [HTTP_VERIFY_CERT,FALSE,HTTP_METHOD,"POST",HTTP_MIMETYPE,"application/json"];
        cname = name;
        req=llHTTPRequest(baseurl,httphead,json); 
        
                }
            }

        }
        else if (channel == menuchanA)
        {
            if (message == "Page 2")
                llDialog(owner, "\n\nPlease choose your Native Language:", ["hy", "sr", "bg", "ca", "hr", "cs", "zh", "da", "fi", "el", "Page 1", "Page 3"], menuchanA);
            else if (message == "Page 1")
                llDialog(owner, "\n\nPlease choose your Native Language:", ["en", "de", "it", "es", "fr", "pt", "nl", "sv", "sq", "ar", "Page 2", "Page 3"], menuchanA);
            else if (message == "Page 3")
                llDialog(owner, "\n\nPlease choose your Native Language:", ["he", "hu", "id", "ja", "ko", "no", "pl", "ro", "ru", "tr", "Page 1", "Page 2"], menuchanA);
            else
            {
                llSetText("Choose a translation language", <1,1,0>, 1);
                fromlang = message;
                llDialog(owner, "\n\nPlease choose a Translation Language:", ["en", "de", "it", "es", "fr", "pt", "nl", "sv", "sq", "ar", "Page 2", "Page 3"], menuchanB);
            }
        }
        else if (channel == menuchanB)
        {
            if (message == "Page 2")
                llDialog(owner, "\n\nPlease choose a Translation Language:", ["hy", "sr", "bg", "ca", "hr", "cs", "zh", "da", "fi", "el", "Page 1", "Page 3"], menuchanB);
            else if (message == "Page 1")
                llDialog(owner, "\n\nPlease choose a Translation Language:", ["en", "de", "it", "es", "fr", "pt", "nl", "sv", "sq", "ar", "Page 2", "Page 3"], menuchanB);
            else if (message == "Page 3")
                llDialog(owner, "\n\nPlease choose a Translation Language:", ["he", "hu", "id", "ja", "ko", "no", "pl", "ro", "ru", "tr", "Page 1", "Page 2"], menuchanB);
            else
            {
                tolang = message;
                llSetText("Ready", <0,1,0>, 1);
                llSleep(0.5);
                llSetText("", <1,1,1>, 1);
            }
        }
    }
    
    http_response(key id, integer status, list metadata, string body)
    {

            string myname = llGetObjectName();
            string newname = llKey2Name(owner) + " (" + fromlang + " -> " + tolang + ")";
            llSetObjectName(newname);
            list li = llJson2List(body);

            llSay(0,llUnescapeURL(llList2String(li,1)));
            llSetObjectName(myname);

    }
}