babel.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # ********************************************************************************
  4. # Copyright © 2018 jianglin
  5. # File Name: babel.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2018-02-11 14:52:25 (CST)
  9. # Last Update: Wednesday 2019-05-08 16:25:29 (CST)
  10. # By:
  11. # Description:
  12. # ********************************************************************************
  13. from flask import request, g
  14. from flask_babel import Babel
  15. from forums.default import LANGUAGES
  16. babel = Babel()
  17. @babel.localeselector
  18. def locale():
  19. user = getattr(g, 'user', None)
  20. if user is not None:
  21. if request.path.startswith('/admin'):
  22. return 'zh_Hans_CN'
  23. if g.user.is_authenticated:
  24. return user.setting.locale or 'zh'
  25. return request.accept_languages.best_match(LANGUAGES.keys())
  26. @babel.timezoneselector
  27. def timezone():
  28. user = getattr(g, 'user', None)
  29. if user is not None:
  30. if g.user.is_authenticated:
  31. return user.setting.timezone or 'UTC'
  32. return 'UTC'
  33. def init_app(app):
  34. babel.init_app(app)