Skip to main content

Reactive Multi-language Gradio App with minimal effort

Project description

gradio-i18n

PyPI - Version Static Badge

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

  1. Prepare a translation dict like examples below.
  2. Wrap text intended to be localized with gradio_i18n.gettext()
  3. 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"), allow_unicode=True)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gradio_i18n-0.0.1.post1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

gradio_i18n-0.0.1.post1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file gradio_i18n-0.0.1.post1.tar.gz.

File metadata

  • Download URL: gradio_i18n-0.0.1.post1.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for gradio_i18n-0.0.1.post1.tar.gz
Algorithm Hash digest
SHA256 0f3af377a0c09f7f6e913a40e7880d529bd087a41e25337b38d5e57dd7709ded
MD5 61506279afb399ce9032ed4809f69aad
BLAKE2b-256 859b886fb4dbb7fc7526328809d5d95f59ee8ea3e22a13153cee4b798846198a

See more details on using hashes here.

File details

Details for the file gradio_i18n-0.0.1.post1-py3-none-any.whl.

File metadata

File hashes

Hashes for gradio_i18n-0.0.1.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 280136bd1109f5e38527de9fb443f2e11ea29554c9b3c7e805a11742c8cf04cc
MD5 47fe7a688a4b742371d2dc887bc06d2f
BLAKE2b-256 af740d369df8ff77e887cdf617713947d6921ed62ff17d5c10caf18eb1869b19

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page