Merge pull request #10335 from akx/l10n-dis-take-2

Localization fixes
This commit is contained in:
AUTOMATIC1111 2023-05-13 19:44:55 +03:00 committed by GitHub
commit cdb1ffb2f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 35 deletions

View File

@ -109,18 +109,23 @@ function processNode(node){
} }
function dumpTranslations(){ function dumpTranslations(){
if(!hasLocalization()) {
// If we don't have any localization,
// we will not have traversed the app to find
// original_lines, so do that now.
processNode(gradioApp());
}
var dumped = {} var dumped = {}
if (localization.rtl) { if (localization.rtl) {
dumped.rtl = true dumped.rtl = true;
} }
Object.keys(original_lines).forEach(function(text){ for (const text in original_lines) {
if(dumped[text] !== undefined) return if(dumped[text] !== undefined) continue;
dumped[text] = localization[text] || text;
}
dumped[text] = localization[text] || text return dumped;
})
return dumped
} }
function download_localization() { function download_localization() {
@ -137,7 +142,11 @@ function download_localization() {
document.body.removeChild(element); document.body.removeChild(element);
} }
if(hasLocalization()) { document.addEventListener("DOMContentLoaded", function () {
if (!hasLocalization()) {
return;
}
onUiUpdate(function (m) { onUiUpdate(function (m) {
m.forEach(function (mutation) { m.forEach(function (mutation) {
mutation.addedNodes.forEach(function (node) { mutation.addedNodes.forEach(function (node) {
@ -146,26 +155,23 @@ if(hasLocalization()) {
}); });
}) })
processNode(gradioApp())
document.addEventListener("DOMContentLoaded", function () { if (localization.rtl) { // if the language is from right to left,
processNode(gradioApp()) (new MutationObserver((mutations, observer) => { // wait for the style to load
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => {
if (node.tagName === 'STYLE') {
observer.disconnect();
if (localization.rtl) { // if the language is from right to left, for (const x of node.sheet.rules) { // find all rtl media rules
(new MutationObserver((mutations, observer) => { // wait for the style to load if (Array.from(x.media || []).includes('rtl')) {
mutations.forEach(mutation => { x.media.appendMedium('all'); // enable them
mutation.addedNodes.forEach(node => {
if (node.tagName === 'STYLE') {
observer.disconnect();
for (const x of node.sheet.rules) { // find all rtl media rules
if (Array.from(x.media || []).includes('rtl')) {
x.media.appendMedium('all'); // enable them
}
} }
} }
}) }
}); })
})).observe(gradioApp(), { childList: true }); });
} })).observe(gradioApp(), { childList: true });
}) }
} })

View File

@ -23,7 +23,7 @@ def list_localizations(dirname):
localizations[fn] = file.path localizations[fn] = file.path
def localization_js(current_localization_name): def localization_js(current_localization_name: str) -> str:
fn = localizations.get(current_localization_name, None) fn = localizations.get(current_localization_name, None)
data = {} data = {}
if fn is not None: if fn is not None:
@ -34,4 +34,4 @@ def localization_js(current_localization_name):
print(f"Error loading localization from {fn}:", file=sys.stderr) print(f"Error loading localization from {fn}:", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr) print(traceback.format_exc(), file=sys.stderr)
return f"var localization = {json.dumps(data)}\n" return f"window.localization = {json.dumps(data)}"

View File

@ -1771,12 +1771,11 @@ def webpath(fn):
def javascript_html(): def javascript_html():
script_js = os.path.join(script_path, "script.js") # Ensure localization is in `window` before scripts
head = f'<script type="text/javascript" src="{webpath(script_js)}"></script>\n' head = f'<script type="text/javascript">{localization.localization_js(shared.opts.localization)}</script>\n'
inline = f"{localization.localization_js(shared.opts.localization)};" script_js = os.path.join(script_path, "script.js")
if cmd_opts.theme is not None: head += f'<script type="text/javascript" src="{webpath(script_js)}"></script>\n'
inline += f"set_theme('{cmd_opts.theme}');"
for script in modules.scripts.list_scripts("javascript", ".js"): for script in modules.scripts.list_scripts("javascript", ".js"):
head += f'<script type="text/javascript" src="{webpath(script.path)}"></script>\n' head += f'<script type="text/javascript" src="{webpath(script.path)}"></script>\n'
@ -1784,7 +1783,8 @@ def javascript_html():
for script in modules.scripts.list_scripts("javascript", ".mjs"): for script in modules.scripts.list_scripts("javascript", ".mjs"):
head += f'<script type="module" src="{webpath(script.path)}"></script>\n' head += f'<script type="module" src="{webpath(script.path)}"></script>\n'
head += f'<script type="text/javascript">{inline}</script>\n' if cmd_opts.theme:
head += f'<script type="text/javascript">set_theme(\"{cmd_opts.theme}\");</script>\n'
return head return head