Kodi Community Forum

Full Version: Automatic translation of a content in a textbox
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is it possible inside a textbox (or other) to translate its content using a google translate API or other solutions?
This is used to automatically translate the contents of the texbox or by using a variable to do this.
Is it possible somehow?

To translate content taken from the scraper available in a single language...


Thanks to those who will help me
Well, doing some searching I see there is python package googletrans which seems pretty straightforward to use if it works.  You could either make a JSON call to the library to get an existing text string or interrogate a Kodi infolabel, then either store back the translated string into the library with a JSON call or display to the user in a dialog.

scott s.
.
@scott967

I'm glad there is a way to do it.
I could not understand well how to implement it, i am not able to know how to make JSON calls, you can give me an example of its use (googletrans) in a simple textbox?
I'll explain what I already have:
I have a textbox already filled with text in another language, I should simply translate its content and insert it in another textbox.
(so without URL, without showing dialogs or anything else and then I'll think about how to get the textbox displayed).

But I would need an example code on how to make this JSON call with googletrans on a textbox.
(i'm in android 11, i hope it can work the same.)

Can you show me how to do it? 🙏

I think this could help other users too.


Thank you very much for the help
The JSON calls will not interact with googletrans on their own.

What is needed is some kind of addon that is built to use the googletrans python library, adding the library as noted here:

https://kodi.wiki/view/Python_libraries

In building such an addon, you just need to decide if the addon will work with text data from a Kodi infolable or from a JSON call:

1) If from a kodi infolable, then you do not need JSON, you could take that infolable data, send to the new addon and get back your translated text.

2) If (for some reason) you need/want to use JSON to get the text to translate, then use whatever JSON call you need, get the text, pass it to the new addon and then handle the translated text as needed.

JSON info available at:

https://kodi.wiki/view/JSON-RPC_API/v12

To sum up, you first need an Addon, and in developing the addon decide if you just need to access text within kodi infolables , or from JSON
@kcook_shield

ok, so I should create an add-on that allows to make this link from the text to be translated to the translated text.

Unfortunately it is not in my ability to create an add-on, but now I understand how to proceed.

I only manage the XML part, I hope that someone who has a good knowledge of python will be able to develop it, because I believe it can be useful and is a great help for everyone.


Thank you for your support
@"alberto1998" 

I'm not sure development of an addon using googletrans for use by many would be wise, as noted on the details page about it:

https://pypi.org/project/googletrans/

"However, this could be blocked at any time."

Basically it is using the web API, and as such it could change at any time and then ( possibly ) not usable anymore (currently they hacked a way to get web based free credentials to use the web API).

The only real path I see to setup (using Google Translate) would be through their official API ( https://cloud.google.com/translate/pricing ) , but from what I see there, a user of such an addon would need API keys, and those API keys have to have an account with a payment option.

it is free when you go under 500,000 characters translated per month, but then charges beyond that usage, and in any event, requires payment setup to get the API credentials, which I would think would limit the appeal to many potential users :-(
@"alberto1998" 

There are other translation options, like https://github.com/LibreTranslate/LibreTranslate

They have a paid option (per month), but they also offer code to install your own local copy for free ( via docker, etc) to where you have a completely offline local server running for free.

And, there are also mirrors that offer free access (of course, mirrors could change, go away, etc).

Based on a free mirror I see, I tested some basic code that works (still would need to develop a full addon, with settings, etc), but this option makes sense for more users (use free mirror or setup own local server for translations or use paid API server) all with the same code (configured with url and optional API key.

Basic python code for kodi addon is as simple as:
------------------------

import xbmc, xbmcgui
import sys
import urllib.request
import json

win = xbmcgui.Window(10000)
win.setProperty("translated_text", '')

def main():
    url = "https://translate.argosopentech.com/translate"
    values = {'q': sys.argv[1], 'source': 'auto', 'target': 'en', 'format': 'text'}
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json",
    }    
    data = json.dumps(values).encode("utf-8")
    
    try:
        req = urllib.request.Request(url, data, headers)
        with urllib.request.urlopen(req) as f:
            res = f.read()
            result = json.loads(res)
            translated_text = str(result['translatedText'])
    except Exception as e:
        translated_text = str(e)

    win.setProperty("translated_text", translated_text)
    return ''


if __name__ == '__main__':
    main()

---------------------

And the code could be run via skin :

<onload>RunScript(script.XXXXXXXX,$INFO[ListItem.Label])</onload>
( script.XXXXXXXX =  whatever name the final addon script is built as )

And then access the result with :

$INFO[Window(Home).Property(translated_text)]
(2022-10-18, 23:48)kcook_shield Wrote: [ -> ]@"alberto1998" 

There are other translation options, like https://github.com/LibreTranslate/LibreTranslate

They have a paid option (per month), but they also offer code to install your own local copy for free ( via docker, etc) to where you have a completely offline local server running for free.

And, there are also mirrors that offer free access (of course, mirrors could change, go away, etc).

Based on a free mirror I see, I tested some basic code that works (still would need to develop a full addon, with settings, etc), but this option makes sense for more users (use free mirror or setup own local server for translations or use paid API server) all with the same code (configured with url and optional API key.

Basic python code for kodi addon is as simple as:
------------------------

import xbmc, xbmcgui
import sys
import urllib.request
import json

win = xbmcgui.Window(10000)
win.setProperty("translated_text", '')

def main():
    url = "https://translate.argosopentech.com/translate"
    values = {'q': sys.argv[1], 'source': 'auto', 'target': 'en', 'format': 'text'}
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json",
    }    
    data = json.dumps(values).encode("utf-8")
    
    try:
        req = urllib.request.Request(url, data, headers)
        with urllib.request.urlopen(req) as f:
            res = f.read()
            result = json.loads(res)
            translated_text = str(result['translatedText'])
    except Exception as e:
        translated_text = str(e)

    win.setProperty("translated_text", translated_text)
    return ''


if __name__ == '__main__':
    main()

---------------------

And the code could be run via skin :

<onload>RunScript(script.XXXXXXXX,$INFO[ListItem.Label])</onload>
( script.XXXXXXXX =  whatever name the final addon script is built as )

And then access the result with :

$INFO[Window(Home).Property(translated_text)]
ok, thank you very much for your help.

But this python code is enough to build the final addon, or is it just a part?
however I thought it was something more feasible, but instead it is much more complicated than expected.

In any case I will try this code and hope to be able to automatically translate some text without adding any other parameters.
I hope it has automatic recognition of the language of origin.

@kcook_shield thank you so much for your help 🙏
(+1 deserved)
(2022-10-19, 12:43)alberto1998 Wrote: [ -> ]
(2022-10-18, 23:48)kcook_shield Wrote: [ -> ] 
ok, thank you very much for your help.

But this python code is enough to build the final addon, or is it just a part?
however I thought it was something more feasible, but instead it is much more complicated than expected.

In any case I will try this code and hope to be able to automatically translate some text without adding any other parameters.
I hope it has automatic recognition of the language of origin.

@kcook_shield thank you so much for your help 🙏
(+1 deserved)

This is not a final addon, but shows an example of code that could be used in an addon.

See https://kodi.wiki/view/Add-on_development for documentation on building addons.

As for the detection of language, this API says it can do that (though there are options to set "from" and "to" languages.
@"alberto1998" 

well, I took the plunge and setup an addon for this code:

https://forum.kodi.tv/showthread.php?tid=370094

Please feel free to give me feedback/questions in that thread and I will see how I can help/modify the addon for you.
@kcook_shield

Thank you very much!!🙏

Forgive me but I was unable to build an add-on.

Thanks to your help, many users and others will be able to use it within their skins.
This is a very important addon, it should be there by default in kodi.
(many +1 deserved 🙏)
@"alberto1998" 

I have fixed the links to the repository in that thread. You should be able to get it loaded now.

Please do continue questions/suggestions for it in the other thread and I will do my best to adjust it for you.