1
0

TranslateSitePlugin.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import time
  2. from Plugin import PluginManager
  3. from Translate import translate
  4. @PluginManager.registerTo("UiRequest")
  5. class UiRequestPlugin(object):
  6. def actionSiteMedia(self, path, **kwargs):
  7. file_name = path.split("/")[-1]
  8. if not file_name: # Path ends with /
  9. file_name = "index.html"
  10. extension = file_name.split(".")[-1]
  11. if translate.lang != "en" and extension in ["js", "html"]:
  12. path_parts = self.parsePath(path)
  13. kwargs["header_length"] = False
  14. file_generator = super(UiRequestPlugin, self).actionSiteMedia(path, **kwargs)
  15. if "next" in dir(file_generator): # File found and generator returned
  16. site = self.server.sites.get(path_parts["address"])
  17. return self.actionPatchFile(site, path_parts["inner_path"], file_generator)
  18. else:
  19. return file_generator
  20. else:
  21. return super(UiRequestPlugin, self).actionSiteMedia(path, **kwargs)
  22. def actionUiMedia(self, path):
  23. file_generator = super(UiRequestPlugin, self).actionUiMedia(path)
  24. if translate.lang != "en" and path.endswith(".js"):
  25. s = time.time()
  26. data = "".join(list(file_generator))
  27. data = translate.translateData(data)
  28. self.log.debug("Patched %s (%s bytes) in %.3fs" % (path, len(data), time.time() - s))
  29. return iter([data])
  30. else:
  31. return file_generator
  32. def actionPatchFile(self, site, inner_path, file_generator):
  33. content_json = site.content_manager.contents["content.json"]
  34. lang_file = "languages/%s.json" % translate.lang
  35. lang_file_exist = False
  36. if site.settings.get("own"): # My site, check if the file is exist (allow to add new lang without signing)
  37. if site.storage.isFile(lang_file):
  38. lang_file_exist = True
  39. else: # Not my site the reference in content.json is enough (will wait for download later)
  40. if lang_file in content_json.get("files", {}):
  41. lang_file_exist = True
  42. if not lang_file_exist or inner_path not in content_json.get("translate", []):
  43. for part in file_generator:
  44. if inner_path.endswith(".html"):
  45. yield part.replace("lang={lang}", "lang=" + str(translate.lang)) # lang get parameter to .js file to avoid cache
  46. else:
  47. yield part
  48. else:
  49. s = time.time()
  50. data = "".join(list(file_generator))
  51. # if site.content_manager.contents["content.json"]["files"].get(lang_file):
  52. site.needFile(lang_file, priority=10)
  53. try:
  54. if inner_path.endswith("js"):
  55. data = translate.translateData(data, site.storage.loadJson(lang_file), "js")
  56. else:
  57. data = translate.translateData(data, site.storage.loadJson(lang_file), "html")
  58. data = data.replace("lang={lang}", "lang=" + str(translate.lang)) # lang get parameter to .js file to avoid cache
  59. except Exception as err:
  60. site.log.error("Error loading translation file %s: %s" % (lang_file, err))
  61. self.log.debug("Patched %s (%s bytes) in %.3fs" % (inner_path, len(data), time.time() - s))
  62. yield data