File size: 1,361 Bytes
ce83d96
 
 
 
 
 
 
 
 
 
0240bbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce83d96
0240bbc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import streamlit as st
from translate import Translator

# Function to translate text
def translate_text(text, from_lang, to_lang):
    translator = Translator(from_lang=from_lang, to_lang=to_lang)
    translation = translator.translate(text)
    return translation

# Streamlit app
st.title('Multilingual Text Translator')

st.write('Translate text from one language to another with this simple app.')

from_lang = st.selectbox('Select the source language:', 
                         ['English', 'Spanish', 'French', 'German', 'Chinese', 'Japanese', 'Hindi'],
                         index=0)
to_lang = st.selectbox('Select the target language:', 
                       ['Spanish', 'English', 'French', 'German', 'Chinese', 'Japanese', 'Hindi'],
                       index=1)

lang_codes = {
    'English': 'en',
    'Spanish': 'es',
    'French': 'fr',
    'German': 'de',
    'Chinese': 'zh',
    'Japanese': 'ja',
    'Hindi': 'hi'
}

text = st.text_area('Enter the text you want to translate:')

if st.button('Translate'):
    if text:
        from_lang_code = lang_codes[from_lang]
        to_lang_code = lang_codes[to_lang]
        translation = translate_text(text, from_lang_code, to_lang_code)
        st.success(f'Translation ({from_lang} to {to_lang}): {translation}')
    else:
        st.error('Please enter the text you want to translate.')