test_translations.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import subprocess
  2. import os
  3. from flask import current_app
  4. from babel.support import Translations, NullTranslations
  5. from flaskbb.utils.translations import FlaskBBDomain
  6. from flaskbb.extensions import plugin_manager
  7. def _remove_compiled_translations():
  8. translations_folder = os.path.join(current_app.root_path, "translations")
  9. # walks through the translations folder and deletes all files
  10. # ending with .mo
  11. for root, dirs, files in os.walk(translations_folder):
  12. for name in files:
  13. if name.endswith(".mo"):
  14. os.unlink(os.path.join(root, name))
  15. def _compile_translations():
  16. PLUGINS_FOLDER = os.path.join(current_app.root_path, "plugins")
  17. translations_folder = os.path.join(current_app.root_path, "translations")
  18. subprocess.call(["pybabel", "compile", "-d", translations_folder])
  19. for plugin in plugin_manager.all_plugins:
  20. plugin_folder = os.path.join(PLUGINS_FOLDER, plugin)
  21. translations_folder = os.path.join(plugin_folder, "translations")
  22. subprocess.call(["pybabel", "compile", "-d", translations_folder])
  23. def test_flaskbbdomain_translations(default_settings):
  24. domain = FlaskBBDomain(current_app)
  25. with current_app.test_request_context():
  26. assert domain.get_translations_cache() == {}
  27. # just to be on the safe side that there are really no compiled
  28. # translations available
  29. _remove_compiled_translations()
  30. # no compiled translations are available
  31. assert isinstance(domain.get_translations(), NullTranslations)
  32. # lets compile them and test again
  33. _compile_translations()
  34. # now there should be translations :)
  35. assert isinstance(domain.get_translations(), Translations)