Skip to content

Commit

Permalink
Update localization.py
Browse files Browse the repository at this point in the history
  • Loading branch information
liubo0902 committed Sep 5, 2023
1 parent 5ef669d commit 0c1c9e7
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions modules/localization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import os

from modules import errors, scripts
from modules import errors

localizations = {}

Expand All @@ -14,21 +14,27 @@ def list_localizations(dirname):
if ext.lower() != ".json":
continue

localizations[fn] = os.path.join(dirname, file)
fn = fn.replace(" ", "").replace("(", "_").replace(")","")
localizations[fn] = [os.path.join(dirname, file)]

from modules import scripts
for file in scripts.list_scripts("localizations", ".json"):
fn, ext = os.path.splitext(file.filename)
localizations[fn] = file.path
fn = fn.replace(" ", "").replace("(", "_").replace(")","")
if fn not in localizations:
localizations[fn] = []
localizations[fn].append(file.path)


def localization_js(current_localization_name: str) -> str:
fn = localizations.get(current_localization_name, None)
fns = localizations.get(current_localization_name, None)
data = {}
if fn is not None:
try:
with open(fn, "r", encoding="utf8") as file:
data = json.load(file)
except Exception:
errors.report(f"Error loading localization from {fn}", exc_info=True)
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)

return f"window.localization = {json.dumps(data)}"

0 comments on commit 0c1c9e7

Please sign in to comment.