Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
# create an i18n instance with translations for different languages
|
4 |
+
i18n = gr.I18n(
|
5 |
+
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."},
|
6 |
+
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."},
|
7 |
+
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."},
|
8 |
+
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."},
|
9 |
+
)
|
10 |
+
|
11 |
+
def add_hello_world(name):
|
12 |
+
return "hello " + name
|
13 |
+
|
14 |
+
with gr.Blocks() as demo:
|
15 |
+
markdown_text = gr.Markdown(value=i18n("markdown_text"))
|
16 |
+
|
17 |
+
with gr.Row():
|
18 |
+
# use i18n() for any string that should be translated
|
19 |
+
name_input = gr.Textbox(label=i18n("name_label"), value=i18n("john_doe"))
|
20 |
+
|
21 |
+
with gr.Row():
|
22 |
+
output_text = gr.Textbox(label=i18n("result_label"))
|
23 |
+
|
24 |
+
with gr.Row():
|
25 |
+
greet_btn = gr.Button(value=i18n("submit_button"))
|
26 |
+
|
27 |
+
with gr.Row():
|
28 |
+
reset_btn = gr.Button("Reset Name")
|
29 |
+
|
30 |
+
greet_btn.click(fn=add_hello_world, inputs=name_input, outputs=output_text)
|
31 |
+
reset_btn.click(fn=lambda: i18n("john_doe"), inputs=None, outputs=name_input)
|
32 |
+
|
33 |
+
gr.Markdown("""
|
34 |
+
This demo shows Gradio's internationalization (i18n) functionality.
|
35 |
+
The interface automatically displays text in the user's browser language
|
36 |
+
(if available in our translations), or falls back to English.
|
37 |
+
""")
|
38 |
+
|
39 |
+
if __name__ == "__main__":
|
40 |
+
# pass i18n to the launch function
|
41 |
+
demo.launch(i18n=i18n)
|