Aiden Richards
Aiden Richards

Reputation: 3

How do you code a translator with language user can choose

Im creating a webapp that takes an image, reads the text inside of it, and translates that text into another language. However right now you can't chnage the language the text is going to be changed into on the website. How would I do this

Right now I've got the source language as english and the target language as german (which is working). However, i've created a dictionary that has all languages and language codes supported by google translate, and ive put this in a dropdown form. how to I link the input from this back to python and make it the target language

PYTHON

def Lang_target():
    language_targ={
        'af': 'Afrikaans',              'sq':'Albanian',    'ar': 'Arabic',     'az': 'Azerbaijani',
        'be': 'Belarusian',             'bn': 'Bengali',    'ca': 'Catalan',    'zh-CN': 'Chinese Simplified',
        'zh-TW': 'Chinese Traditional', 'hr': 'Croatian',   'cs': 'Czech',      'da': 'Danish',     
        'nl': 'Dutch',                  'en': 'English',    'eo': 'Esperanto',  'et': 'Estonian',
        'tl': 'Filipino',               'fi': 'Finnish',    'fr': 'French',     'gl': 'Galician',   
        'ka': 'Georgian',               'de': 'German',     'el': 'Greek',      'gu': 'Gujarati',
        'ht': 'Haitian Creole',         'iw': 'Hebrew',     'hi': 'Hindi',      'hu': 'Hungarian',
        'is': 'Icelandic',              'ga': 'Irish',      'it': 'Italian',    'id': 'Indonesian',
        'ja': 'Japanese',               'kn': 'Kannada',    'ko': 'Korean',     'la': 'Latin',
        'lv': 'Latvian',                'lt': 'Lithuanian', 'mk': 'Macedonian', 'ms': 'Malay',
        'mt': 'Maltese',                'no': 'Norwegian',  'fa': 'Persian',    'pl': 'Polish', 
        'pt': 'Portuguese',             'ro': 'Romanian',   'ru': 'Russian',    'sr': 'Serbian',
        'sk': 'Slovak',                 'es': 'Spanish',    'sl': 'Slovenian',  'sw': 'Swahili',
        'sv': 'Swedish',                'ta': 'Tamil',      'te': 'Telugu',     'th': 'Thai',
        'tr': 'Turkish',                'uk': 'Ukrainian',  'ur': 'Urdu',       'vi': 'Vietnamese',             
        'cy': 'Welsh',                  'yi': 'Yiddish',
    }
    return language_targ

@app.route('/selectImage')
def selectImage():
    fn = image_name()
    language_target = Lang_target()
    return render_template("selectImage.html", image_name=image_name, fn=fn, language_target=language_target)

@app.route('/getfileHelper', methods=['GET','POST'])
def getfileHelper():
    if request.method == 'POST':
        file = request.files['imgfile']
        filename = secure_filename(file.filename)   #from werkzeug import secure_filename
selectImage.html page
            if file.filename == '':
                flash("No file selected. Please select an image file")
                return render_template('selectImage.html')
            texts = detect_text('static/images/'+filename)

        text_translations = [] #emty list for dictionary of original text and translation
        for text in texts:

            translate_client = translate.Client()  
            translate_text = text.description  
            source = 'en'  
            target = 'de'   

            translation = translate_client.translate(translate_text, source_language=source, target_language=target)

            text_translations.append({'text':translate_text, 'translation':translation['translatedText']})

            db_append(filename, translate_text, translation['translatedText'])


    return render_template('home.html', filename=filename, text_translations=text_translations)

HTML

<form>
    <select>
    {% for x in language_target%} 
    <option> {{ language_target[x] }}</option>
    {% endfor %}
    </select>
    <input type="submit" value="Submit">
</form>

Upvotes: 0

Views: 107

Answers (1)

DarthCadeus
DarthCadeus

Reputation: 372

If you add a name attribute to the select (for example, name="lang_target"), you can retrieve the value of the dropdown from the request in request.args["lang_target"] for GET (as you did not specify POST). I am not sure which application route performs the translations, but you should direct the request to that route.

Upvotes: 2

Related Questions