forked from lingthio/Flask-User
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslations.py
76 lines (63 loc) · 2.88 KB
/
translations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
""" This file contains functions to translate strings for Flask-User.
:copyright: (c) 2013 by Ling Thio
:author: Ling Thio ([email protected])
:license: Simplified BSD License, see LICENSE.txt for more details."""
from flask import _request_ctx_stack, current_app
# To avoid requiring the Flask-Babel, Babel and speaklater packages,
# we check if the app has initialized Flask-Babel or not
def get_translations():
# If there is no context: return None
ctx = _request_ctx_stack.top
if not ctx:
return None
# If context exists and contains a cashed value, return cached value
if hasattr(ctx, 'flask_user_translations'):
return ctx.flask_user_translations
# If App has not initialized Flask-Babel: return None
app_has_initalized_flask_babel = 'babel' in current_app.extensions
if not app_has_initalized_flask_babel: # pragma no cover
ctx.flask_user_translations = None
return ctx.flask_user_translations
# Prepare search properties
import os
import gettext as python_gettext
from flask_babel import get_locale, support
domain = 'flask_user'
locales = [get_locale()]
languages = [str(locale) for locale in locales]
# See if translations exists in Application dir
app_dir = os.path.join(current_app.root_path, 'translations')
filename = python_gettext.find(domain, app_dir, languages)
if filename:
ctx.flask_user_translations = support.Translations.load(app_dir, locales, domain=domain)
# See if translations exists in Flask-User dir
else:
flask_user_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'translations')
ctx.flask_user_translations = support.Translations.load(flask_user_dir, locales, domain=domain)
from flask.ext import babel
return ctx.flask_user_translations.merge(babel.get_translations())
def gettext(string, **variables):
""" Translate specified string."""
translations = get_translations()
if translations:
return translations.ugettext(string) % variables
return string % variables
# def ngettext(singular, plural, num, **variables):
# """ Translate a singular/plural string based on the number 'num'."""
# translations = get_translations()
# variables.setdefault('num', num)
# if translations:
# return translations.ungettext(singular, plural, num) % variables
# return (singular if num == 1 else plural) % variables
def lazy_gettext(string, **variables):
""" Similar to 'gettext' but the string returned is lazy which means
it will be translated when it is used as an actual string."""
try:
from speaklater import make_lazy_string
return make_lazy_string(gettext, string, **variables)
except ImportError:
return string % variables
_ = lazy_gettext
_home_page = _('Home Page')
_profile_page = _('Profile Page')
_special_page = _('Special Page')