File size: 2,322 Bytes
ee69eb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

# create an i18n instance with translations for different languages
i18n = gr.I18n(
    en={"name_label": "Your Name", "submit_button": "Greet", "john_doe": "John English", "result_label": "Result", "markdown_text": "This demo shows Gradio's internationalization (i18n) functionality. The interface automatically displays text in the user's browser language (if available in our translations), or falls back to English."},
    es={"name_label": "Tu Nombre", "submit_button": "Saludar", "john_doe": "John Spanish", "result_label": "Resultado", "markdown_text": "Este demo muestra la funcionalidad de internacionalización (i18n) de Gradio. La interfaz muestra texto en el idioma del navegador del usuario (si está disponible en nuestras traducciones), o cae de nuevo a inglés."},
    fr={"name_label": "Votre Nom", "submit_button": "Saluer", "john_doe": "John French", "result_label": "Résultat", "markdown_text": "Ce démonstration montre la fonctionnalité d'internationalisation (i18n) de Gradio. L'interface affiche le texte dans la langue du navigateur de l'utilisateur (si disponible dans nos traductions), ou retombe à l'anglais."},
    de={"name_label": "Dein Name", "submit_button": "Grüßen", "john_doe": "John German", "result_label": "Ergebnis", "markdown_text": "Dieses Demo zeigt die Funktionalität der Internationalisierung (i18n) von Gradio. Die Schnittstelle zeigt Text im Browser-Sprache des Benutzers an (falls verfügbar in unseren Übersetzungen), oder fällt auf Englisch zurück."},
)

def add_hello_world(name):
    return "hello " + name

with gr.Blocks() as demo:
    markdown_text = gr.Markdown(value=i18n("markdown_text"))

    with gr.Row():
        # use i18n() for any string that should be translated
        name_input = gr.Textbox(label=i18n("name_label"), value=i18n("john_doe"))

    with gr.Row():
        output_text = gr.Textbox(label=i18n("result_label"))

    with gr.Row():
        greet_btn = gr.Button(value=i18n("submit_button"))

    with gr.Row():
        reset_btn = gr.Button("Reset Name")

    greet_btn.click(fn=add_hello_world, inputs=name_input, outputs=output_text)
    reset_btn.click(fn=lambda: i18n("john_doe"), inputs=None, outputs=name_input)

if __name__ == "__main__":
    # pass i18n to the launch function
    demo.launch(i18n=i18n)