TranslateSitePlugin.py 3.5 KB

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