Skip to content

Commit

Permalink
fix: use translation for aspect ratios label (#3001)
Browse files Browse the repository at this point in the history
use javascript code instead of python handling for updates for #2590
  • Loading branch information
mashb1t authored May 26, 2024
1 parent 12dc239 commit de34023
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
6 changes: 6 additions & 0 deletions javascript/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ function refresh_style_localization() {
processNode(document.querySelector('.style_selections'));
}

function refresh_aspect_ratios_label(value) {
label = document.querySelector('#aspect_ratios_accordion div span[data-original-text="Aspect Ratios"]')
translation = getTranslation("Aspect Ratios")
label.textContent = translation + " " + htmlDecode(value)
}

function localizeWholePage() {
processNode(gradioApp());

Expand Down
5 changes: 5 additions & 0 deletions javascript/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,8 @@ function set_theme(theme) {
window.location.replace(gradioURL + '?__theme=' + theme);
}
}

function htmlDecode(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
12 changes: 3 additions & 9 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,21 +277,15 @@ def trigger_metadata_preview(filepath):
choices=flags.Performance.list(),
value=modules.config.default_performance,
elem_classes=['performance_selection'])
with gr.Accordion(label='Aspect Ratios', open=False) as aspect_ratios_accordion:
with gr.Accordion(label='Aspect Ratios', open=False, elem_id='aspect_ratios_accordion') as aspect_ratios_accordion:
aspect_ratios_selection = gr.Radio(label='Aspect Ratios', show_label=False,
choices=modules.config.available_aspect_ratios_labels,
value=modules.config.default_aspect_ratio,
info='width × height',
elem_classes='aspect_ratios')

def change_aspect_ratio(text):
import re
regex = re.compile('<.*?>')
cleaned_text = re.sub(regex, '', text)
return gr.update(label='Aspect Ratios ' + cleaned_text)

aspect_ratios_selection.change(change_aspect_ratio, inputs=aspect_ratios_selection, outputs=aspect_ratios_accordion, queue=False, show_progress=False)
shared.gradio_root.load(change_aspect_ratio, inputs=aspect_ratios_selection, outputs=aspect_ratios_accordion, queue=False, show_progress=False)
aspect_ratios_selection.change(lambda x: None, inputs=aspect_ratios_selection, queue=False, show_progress=False, _js='(x)=>{refresh_aspect_ratios_label(x);}')
shared.gradio_root.load(lambda x: None, inputs=aspect_ratios_selection, queue=False, show_progress=False, _js='(x)=>{refresh_aspect_ratios_label(x);}')

image_number = gr.Slider(label='Image Number', minimum=1, maximum=modules.config.default_max_image_number, step=1, value=modules.config.default_image_number)

Expand Down

0 comments on commit de34023

Please sign in to comment.