Gradio provides internationalization support through the I18n and I18nData classes.
I18n
Handles internationalization (i18n) for Gradio applications. Stores translation dictionaries and provides a method to retrieve translation keys.
Parameters
translations
dict[str, dict[str, str]]
Each keyword argument should be a locale code (e.g., “en”, “fr”) with a dictionary value, which maps translation keys to translated strings.
Methods
call
Returns an I18nData object containing the translation key. This metadata object will be serialized and sent to the frontend, where it will be translated by the frontend’s i18n system.
The key to identify the translation string (e.g., “submit_button”).
A I18nData object containing the translation key.
Properties
translations_dict
dict[str, dict[str, str]]
Returns the dictionary of translations provided during initialization. These can be passed to the frontend for use in its translation system.
Example
import gradio as gr
i18n = gr.I18n(
en={"greeting": "Hello", "submit": "Submit"},
es={"greeting": "Hola", "submit": "Enviar"},
fr={"greeting": "Bonjour", "submit": "Soumettre"}
)
with gr.Blocks() as demo:
gr.Textbox(label=i18n("greeting"))
gr.Button(i18n("submit"))
demo.launch()
I18nData
A class that wraps a translation key with metadata. This object will be serialized and sent to the frontend, where the actual translation will happen using the frontend’s i18n system.
Constructor
The translation key to be translated in the frontend.
Methods
to_dict
Convert the I18nData object to a dictionary for serialization. This allows the frontend to recognize it as a translatable object.
A dictionary representation of the I18nData object.
Properties
Usage Notes
- Locale codes should follow BCP 47 format (e.g., ‘en’, ‘en-US’, ‘fr’, ‘es’)
- Translation lookup happens on the frontend based on the browser’s locale and the provided translation dictionaries
- You can use I18nData objects anywhere you would use a string in Gradio components
- The actual translation is performed in the frontend, allowing for dynamic language switching based on user preferences
Advanced Example
import gradio as gr
i18n = gr.I18n(
en={
"app_title": "My Application",
"input_label": "Enter your text",
"output_label": "Result",
"submit_btn": "Process",
"description": "This is a sample application"
},
es={
"app_title": "Mi Aplicación",
"input_label": "Ingrese su texto",
"output_label": "Resultado",
"submit_btn": "Procesar",
"description": "Esta es una aplicación de muestra"
},
fr={
"app_title": "Mon Application",
"input_label": "Entrez votre texte",
"output_label": "Résultat",
"submit_btn": "Traiter",
"description": "Ceci est une application exemple"
}
)
def process_text(text):
return text.upper()
with gr.Blocks(title=i18n("app_title")) as demo:
gr.Markdown(i18n("description"))
with gr.Row():
input_text = gr.Textbox(label=i18n("input_label"))
output_text = gr.Textbox(label=i18n("output_label"))
btn = gr.Button(i18n("submit_btn"))
btn.click(process_text, input_text, output_text)
demo.launch()