File size: 1,182 Bytes
fb83c5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import gradio as gr
import kohya_gui.localization as localization


def file_path(fn):
    return f"file={os.path.abspath(fn)}?{os.path.getmtime(fn)}"


def js_html_str(language):
    head = f'<script type="text/javascript">{localization.load_language_js(language)}</script>\n'
    head += (
        f'<script type="text/javascript">{open("./assets/js/script.js", "r", encoding="utf-8").read()}</script>\n'
    )
    head += f'<script type="text/javascript">{open("./assets/js/localization.js", "r", encoding="utf-8").read()}</script>\n'
    return head


def add_javascript(language):
    if language is None:
        # print('no language')
        return
    jsStr = js_html_str(language)

    def template_response(*args, **kwargs):
        res = localization.GrRoutesTemplateResponse(*args, **kwargs)
        res.body = res.body.replace(b"</head>", f"{jsStr}</head>".encode("utf-8"))
        res.init_headers()
        return res

    gr.routes.templates.TemplateResponse = template_response


if not hasattr(localization, "GrRoutesTemplateResponse"):
    localization.GrRoutesTemplateResponse = gr.routes.templates.TemplateResponse