localization.py 975 B

1234567891011121314151617181920212223242526272829303132333435
  1. import json
  2. import os
  3. from modules import errors
  4. localizations = {}
  5. def list_localizations(dirname):
  6. localizations.clear()
  7. for file in os.listdir(dirname):
  8. fn, ext = os.path.splitext(file)
  9. if ext.lower() != ".json":
  10. continue
  11. localizations[fn] = os.path.join(dirname, file)
  12. from modules import scripts
  13. for file in scripts.list_scripts("localizations", ".json"):
  14. fn, ext = os.path.splitext(file.filename)
  15. localizations[fn] = file.path
  16. def localization_js(current_localization_name: str) -> str:
  17. fn = localizations.get(current_localization_name, None)
  18. data = {}
  19. if fn is not None:
  20. try:
  21. with open(fn, "r", encoding="utf8") as file:
  22. data = json.load(file)
  23. except Exception:
  24. errors.report(f"Error loading localization from {fn}", exc_info=True)
  25. return f"window.localization = {json.dumps(data)}"