Python flask_babel.get_locale() Examples

The following are 11 code examples of flask_babel.get_locale(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flask_babel , or try the search function .
Example #1
Source File: i18n.py    From jbox with MIT License 6 votes vote down vote up
def _get_translations():
    """Returns the correct gettext translations.
    Copy from flask-babel with some modifications.
    """
    ctx = _request_ctx_stack.top
    if ctx is None:
        return None
    # babel should be in extensions for get_locale
    if 'babel' not in ctx.app.extensions:
        return None
    translations = getattr(ctx, 'wtforms_translations', None)
    if translations is None:
        dirname = messages_path()
        translations = support.Translations.load(
            dirname, [get_locale()], domain='wtforms'
        )
        ctx.wtforms_translations = translations
    return translations 
Example #2
Source File: i18n.py    From RSSNewsGAE with Apache License 2.0 6 votes vote down vote up
def _get_translations():
    """Returns the correct gettext translations.
    Copy from flask-babel with some modifications.
    """
    ctx = _request_ctx_stack.top
    if ctx is None:
        return None
    # babel should be in extensions for get_locale
    if 'babel' not in ctx.app.extensions:
        return None
    translations = getattr(ctx, 'wtforms_translations', None)
    if translations is None:
        dirname = messages_path()
        translations = support.Translations.load(
            dirname, [get_locale()], domain='wtforms'
        )
        ctx.wtforms_translations = translations
    return translations 
Example #3
Source File: jinjia.py    From calibre-web with GNU General Public License v3.0 5 votes vote down vote up
def formatdate_filter(val):
    try:
        conformed_timestamp = re.sub(r"[:]|([-](?!((\d{2}[:]\d{2})|(\d{4}))$))", '', val)
        formatdate = datetime.datetime.strptime(conformed_timestamp[:15], "%Y%m%d %H%M%S")
        return format_date(formatdate, format='medium', locale=get_locale())
    except AttributeError as e:
        log.error('Babel error: %s, Current user locale: %s, Current User: %s', e,
                  current_user.locale,
                  current_user.nickname
                  )
        return formatdate 
Example #4
Source File: routes.py    From AUCR with GNU General Public License v3.0 5 votes vote down vote up
def before_request() -> None:
    """Set user last seen time user."""
    if current_user.is_authenticated:
        current_user.last_seen = udatetime.utcnow().replace(tzinfo=None)
        db.session.commit()
        g.search_form = SearchForm()
    g.locale = str(get_locale()) 
Example #5
Source File: app.py    From mattermost-poll with GNU General Public License v3.0 5 votes vote down vote up
def get_locale():
    """Returns the locale for the current request."""
    try:
        return user_locale(request.user_id)
    except AttributeError as e:
        app.logger.warning(e)
    return "en" 
Example #6
Source File: requests.py    From evesrp with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def title(self):
        current_locale = get_locale()
        if current_locale is None:
            current_locale = babel.Locale('en')
        # Special case possesive form in English
        if current_locale.language.lower() == 'en' and \
                current_user.name[:-1] == u's':
            return u"{}' Requests".format(current_user.name)
        else:
            # TRANS: The title of the page listing all requests an individual
            # TRANS: user has made.
            return gettext(u"%(name)s's Requests", name=current_user.name) 
Example #7
Source File: locale.py    From evesrp with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def currencyfmt(currency):
    # We're only formatting ISK, so it has a bit of a special format
    number = numbers.format_decimal(currency, format="#,##0.00;-#",
            locale=get_locale())
    return number 
Example #8
Source File: locale.py    From evesrp with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def numberfmt(number):
    return numbers.format_number(number, locale=get_locale()) 
Example #9
Source File: locale.py    From evesrp with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def percentfmt(percent):
    return numbers.format_percent(percent, locale=get_locale()) 
Example #10
Source File: engine.py    From appkernel with Apache License 2.0 5 votes vote down vote up
def __init_web_layer(self):
        self.app.json_encoder = AppKernelJSONEncoder
        self.app.register_error_handler(Exception, self.generic_error_handler)
        for code in default_exceptions.keys():
            # add a default error handler for everything is unhandled
            self.app.register_error_handler(code, self.generic_error_handler)

            def set_locale_on_request():
                g.locale = str(get_locale())

            self.app.before_request(set_locale_on_request) 
Example #11
Source File: base.py    From incubator-superset with Apache License 2.0 5 votes vote down vote up
def common_bootstrap_payload() -> Dict[str, Any]:
    """Common data always sent to the client"""
    messages = get_flashed_messages(with_categories=True)
    locale = str(get_locale())

    return {
        "flash_messages": messages,
        "conf": {k: conf.get(k) for k in FRONTEND_CONF_KEYS},
        "locale": locale,
        "language_pack": get_language_pack(locale),
        "feature_flags": get_feature_flags(),
        "menu_data": menu_data(),
    }