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)

Get current language

Except for automatically translated text value in Gradio components, user may expect to get the current language of the user for localizing contents. To get the current language, simply pass a gradio.State into arguments of translate_blocks(), and you will get the language value in "lang" key in the input state object.

Example:

import gradio as gr
import gradio_i18n

def get_lang(state):
    return state, state["lang"]

with gr.Blocks() as block:
    state = gr.State()
    display_lang = gr.Text()
    gr.Interface(get_lang, inputs=[state], outputs=[state, display_lang])
    gradio_i18n.translate_blocks(block, {}, state)

block.launch()

[!NOTE] Only dict type state is supported at this moment.

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/json to persist the translation.

JSON

import gradio as gr
import gradio_i18n
import json

trans_file = "translations.json"
if not os.path.exists(trans_file):
    lang_store = {}
else:
    lang_store = json.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)
json.dump(collected_texts, open(trans_file, "w"), indent=2, ensure_ascii=False)

YAML

import gradio as gr
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.2.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

gradio_i18n-0.0.2-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file gradio_i18n-0.0.2.tar.gz.

File metadata

  • Download URL: gradio_i18n-0.0.2.tar.gz
  • Upload date:
  • Size: 8.4 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.2.tar.gz
Algorithm Hash digest
SHA256 b8d0bffc3ad864baba0fba6a3a5b86f47f83df22e0a14b4d0982eba821213a9d
MD5 6898ec4d1d6e8bfdf10cd0b65da94571
BLAKE2b-256 cebb63c82ccb1241c3c496eb8761526c0b8341d0b9f4653152d05f46c8e4710d

See more details on using hashes here.

File details

Details for the file gradio_i18n-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: gradio_i18n-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for gradio_i18n-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 430592983926144feb70c16c1f1204c9240ca3e2d3233b0f4e13ca34d7e0add8
MD5 e226d57d6d5ce0abde71cbc1c5203c18
BLAKE2b-256 192d946312fb2edb25c19a7cb4744c5f138aebe7951e2790cf94d7e74090c1c5

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