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 AUTOMATIC
parent 2053745c8f
commit 86ff43b930
3 changed files with 41 additions and 35 deletions

View File

@ -109,18 +109,23 @@ function processNode(node){
}
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 = {}
if (localization.rtl) {
dumped.rtl = true
dumped.rtl = true;
}
Object.keys(original_lines).forEach(function(text){
if(dumped[text] !== undefined) return
for (const text in original_lines) {
if(dumped[text] !== undefined) continue;
dumped[text] = localization[text] || text;
}
dumped[text] = localization[text] || text
})
return dumped
return dumped;
}
function download_localization() {
@ -137,7 +142,11 @@ function download_localization() {
document.body.removeChild(element);
}
if(hasLocalization()) {
document.addEventListener("DOMContentLoaded", function () {
if (!hasLocalization()) {
return;
}
onUiUpdate(function (m) {
m.forEach(function (mutation) {
mutation.addedNodes.forEach(function (node) {
@ -146,8 +155,6 @@ if(hasLocalization()) {
});
})
document.addEventListener("DOMContentLoaded", function () {
processNode(gradioApp())
if (localization.rtl) { // if the language is from right to left,
@ -168,4 +175,3 @@ if(hasLocalization()) {
})).observe(gradioApp(), { childList: true });
}
})
}

View File

@ -23,7 +23,7 @@ def list_localizations(dirname):
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)
data = {}
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(traceback.format_exc(), file=sys.stderr)
return f"var localization = {json.dumps(data)}\n"
return f"window.localization = {json.dumps(data)}"

View File

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