2022-10-17 18:15:32 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
2023-09-05 07:06:47 +00:00
|
|
|
from modules import errors
|
2022-11-06 06:02:25 +00:00
|
|
|
|
2022-10-17 18:15:32 +00:00
|
|
|
localizations = {}
|
|
|
|
|
|
|
|
|
|
|
|
def list_localizations(dirname):
|
|
|
|
localizations.clear()
|
|
|
|
|
|
|
|
for file in os.listdir(dirname):
|
|
|
|
fn, ext = os.path.splitext(file)
|
|
|
|
if ext.lower() != ".json":
|
|
|
|
continue
|
|
|
|
|
2023-09-05 07:06:47 +00:00
|
|
|
fn = fn.replace(" ", "").replace("(", "_").replace(")","")
|
|
|
|
localizations[fn] = [os.path.join(dirname, file)]
|
2022-10-17 18:15:32 +00:00
|
|
|
|
2023-09-05 07:06:47 +00:00
|
|
|
from modules import scripts
|
2022-11-06 06:02:25 +00:00
|
|
|
for file in scripts.list_scripts("localizations", ".json"):
|
|
|
|
fn, ext = os.path.splitext(file.filename)
|
2023-09-05 07:06:47 +00:00
|
|
|
fn = fn.replace(" ", "").replace("(", "_").replace(")","")
|
|
|
|
if fn not in localizations:
|
|
|
|
localizations[fn] = []
|
|
|
|
localizations[fn].append(file.path)
|
2022-11-06 06:02:25 +00:00
|
|
|
|
2022-10-17 18:15:32 +00:00
|
|
|
|
2023-05-13 16:44:55 +00:00
|
|
|
def localization_js(current_localization_name: str) -> str:
|
2023-09-05 07:06:47 +00:00
|
|
|
fns = localizations.get(current_localization_name, None)
|
2022-10-17 18:15:32 +00:00
|
|
|
data = {}
|
2023-09-05 07:06:47 +00:00
|
|
|
if fns is not None:
|
|
|
|
for fn in fns:
|
|
|
|
try:
|
|
|
|
with open(fn, "r", encoding="utf8") as file:
|
|
|
|
data.update(json.load(file))
|
|
|
|
except Exception:
|
|
|
|
errors.report(f"Error loading localization from {fn}", exc_info=True)
|
2022-10-17 18:15:32 +00:00
|
|
|
|
2023-05-13 16:44:55 +00:00
|
|
|
return f"window.localization = {json.dumps(data)}"
|