Reactive Multi-language Gradio App with minimal effort
Project description
gradio-i18n
Reactive Multi-language Gradio App with minimal effort. Enables Gradio app displayiing localized UI responding to the browser language settings.
Installation
pip install gradio-i18n
Usage
- Prepare a translation dict like examples below.
- Wrap text intended to be localized with
gradio_i18n.gettext()
- Invoke
gradio_i18n.translate_blocks()
, within the context of gradio blocks.
import gradio as gr
from gradio_i18n import gettext, translate_blocks
def greet(name):
return f"Hello {name}!"
lang_store = {
"en": {
"Submit": "Submit✅",
"Name": "Name 📛",
"Greeting": "Greeting 🎉",
"Input your name here.": "Input your name here. 📝",
},
"zh": {
"Submit": "提交",
"Name": "名字",
"Greeting": "问候",
"Input your name here.": "在这里输入你的名字。",
},
}
with gr.Interface(
fn=greet,
inputs=gr.Textbox(
label=gettext("Name"), placeholder=gettext("Input your name here.")
),
outputs=gr.Textbox(label=gettext("Greeting")),
submit_btn=gettext("Submit"),
) as demo:
translate_blocks(demo, lang_store)
demo.launch()
[!NOTE] Keep in mind that the translate_blocks() function MUST BE called in the gradio block context (
with
)
Build translation dictionary
To build the transtion dictionary to be passed to translate_blocks
, we provide a simple helper function to dump all the i18n texts from the gradio blocks object.
This is an example of using yaml to persist the translation.
import gradio_i18n
import yaml
trans_file = "translations.yaml"
if not os.path.exists(trans_file):
lang_store = {}
else:
lang_store = yaml.safe_load(open(trans_file))
# define your gradio block here....
# with gr.Blocks() as block:
# ....
# gradio_i18n.translate_blocks(block, lang_store)
collected_texts = gradio_i18n.dump_blocks(block, langs=["zh", "en"], include_translations=lang_store)
yaml.safe_dump(collected_texts, open(trans_file, "w"))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gradio_i18n-0.0.1.tar.gz
.
File metadata
- Download URL: gradio_i18n-0.0.1.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | abe26ce657471bdf875b9b49b3a08b53a79975307894232853e7a74b087fef85 |
|
MD5 | eba6a368b48d69a541dd96de1b2f35ed |
|
BLAKE2b-256 | 25bbdbd14711d59ecc0d036177e942d4841d953baa9d04e5fdd5cf1df527c5ce |
File details
Details for the file gradio_i18n-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: gradio_i18n-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99042a6dc4bf1d89ef210342c42acf083271bbd8c14b22caa4859e60149b2ef1 |
|
MD5 | 56f66136727c6841ec08cac111af2815 |
|
BLAKE2b-256 | 04e38f676116d8b6ebb699de011689b2aa7eeb4c7f56314fee471184c86c6acc |