test_translations.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. import pytest
  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. @pytest.mark.skip(reason="Plugin transition")
  24. def test_flaskbbdomain_translations(default_settings):
  25. domain = FlaskBBDomain(current_app)
  26. with current_app.test_request_context():
  27. assert domain.get_translations_cache() == {}
  28. # just to be on the safe side that there are really no compiled
  29. # translations available
  30. _remove_compiled_translations()
  31. # no compiled translations are available
  32. assert isinstance(domain.get_translations(), NullTranslations)
  33. # lets compile them and test again
  34. _compile_translations()
  35. # now there should be translations :)
  36. assert isinstance(domain.get_translations(), Translations)