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 = 'messages' locales = [app.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 # TODO: first babel.get_translations return babel.get_translations().merge(ctx.flask_user_translations)
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: ctx.flask_user_translations = None return ctx.flask_user_translations # Prepare search properties import os import gettext as python_gettext from flask.ext.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 get_full_catalog(): t = get_translations() if t and t._catalog: return t._catalog return {}
def ngettext(self, singular, plural, num): variables = {"num": num} t = get_translations() if t is None: return (singular if num == 1 else plural) % variables return t.ungettext(singular, plural, num) % variables
def gettext(self, string): t = get_translations() if t is None: return string return t.ugettext(string)
def ngettext(self, singular, plural, num): variables = {"num": num} t = get_translations() if t is None: return (singular if num == 1 else plural) % variables return t.ungettext(singular, plural, num) % variables
def gettext(self, string): t = get_translations() if t is None: return string return t.ugettext(string)
def create_app(envar="FLASK_SETTINGS", config_object=None): app = Flask(__name__) if config_object is None: app.config.from_envvar("FLASK_SETTINGS") else: app.config.from_object(config_object) if 'local_timezone' not in app.config: app.config['local_timezone'] = pytz.timezone('Europe/Vienna') if 'type2module' not in app.config: app.config['type2view'] = { 'meetup': 'meetups.view_doc', 'company': 'companies.view_doc', } if 'LOG_FILE' in app.config: handler = logging.handlers.RotatingFileHandler(app.config['LOG_FILE'], backupCount=5, maxBytes=1000000) if 'LOG_LEVEL' in app.config: handler.setLevel(app.config['LOG_LEVEL']) else: handler.setLevel(logging.INFO) app.logger.addHandler(handler) app.jinja_env.filters['time'] = filters.timefilter app.jinja_env.filters['date'] = filters.datefilter app.jinja_env.filters['datecode'] = filters.datecode app.jinja_env.filters['datetime'] = filters.datetimefilter app.jinja_env.filters['rst'] = filters.rst app.jinja_env.filters['urlencode'] = filters.urlencode app.jinja_env.filters['urlize'] = filters.urlize app.jinja_env.filters['in_future'] = filters.in_future app.secret_key = app.config['SECRET_KEY'] db.init_app(app) postmark.init_app(app) auth.init_app(app) compass.init_app(app) from . import models # Register modules from .views.account import module as account_module from .views.admin import module as admin_module from .views.core import module as core_module from .views.meetups import module as meetups_module app.register_blueprint(core_module, url_prefix='') app.register_blueprint(account_module, url_prefix='/account') app.register_blueprint(admin_module, url_prefix='/admin') app.register_blueprint(meetups_module, url_prefix='/meetups') #Register context and request processors app.context_processor(context_processors.add_form_generator) # Register babel's i18n functions globally in order for Flatland to see # them. babel.init_app(app) oid.init_app(app) __builtin__.ugettext = lambda x: get_translations().ugettext(x) __builtin__.ungettext = lambda x,s,p: get_translations().ungettext(x,s,p) app.error_handlers[409] = utils.handle_conflict redis.init_app(app) return app