File size: 2,346 Bytes
616b201 |
1 |
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: i18n"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "# create an i18n instance with translations for different languages\n", "i18n = gr.I18n(\n", " en={\"name_label\": \"Your Name\", \"submit_button\": \"Greet\", \"john_doe\": \"John English\", \"result_label\": \"Result\"},\n", " es={\"name_label\": \"Tu Nombre\", \"submit_button\": \"Saludar\", \"john_doe\": \"John Spanish\", \"result_label\": \"Resultado\"},\n", " fr={\"name_label\": \"Votre Nom\", \"submit_button\": \"Saluer\", \"john_doe\": \"John French\", \"result_label\": \"R\u00e9sultat\"},\n", " de={\"name_label\": \"Dein Name\", \"submit_button\": \"Gr\u00fc\u00dfen\", \"john_doe\": \"John German\", \"result_label\": \"Ergebnis\"},\n", ")\n", "\n", "def add_hello_world(name):\n", " return \"hello \" + name\n", "\n", "with gr.Blocks() as demo:\n", " with gr.Row():\n", " # use i18n() for any string that should be translated\n", " name_input = gr.Textbox(label=i18n(\"name_label\"), value=i18n(\"john_doe\"))\n", "\n", " with gr.Row():\n", " output_text = gr.Textbox(label=i18n(\"result_label\"))\n", "\n", " with gr.Row():\n", " greet_btn = gr.Button(value=i18n(\"submit_button\"))\n", "\n", " with gr.Row():\n", " reset_btn = gr.Button(\"Reset Name\")\n", "\n", " greet_btn.click(fn=add_hello_world, inputs=name_input, outputs=output_text)\n", " reset_btn.click(fn=lambda: i18n(\"john_doe\"), inputs=None, outputs=name_input)\n", "\n", " gr.Markdown(\"\"\"\n", " This demo shows Gradio's internationalization (i18n) functionality. \n", " The interface automatically displays text in the user's browser language \n", " (if available in our translations), or falls back to English.\n", " \"\"\")\n", "\n", "if __name__ == \"__main__\":\n", " # pass i18n to the launch function\n", " demo.launch(i18n=i18n)\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} |