def test_lazify(self): from pylons.i18n.translation import lazify def show_str(st): return '%s%s' % (st, len(glob_set)) lazy_show_str = lazify(show_str) result1 = lazy_show_str('fred') result2 = show_str('fred') assert str(result1) == str(result2) glob_set.append('1') assert str(result1) != str(result2)
If the domain name is given, a slightly altered string is returned: a special unicode string stores the domain stored as a property. The domain is then retrieved by :func:`gettext` when translation occurs, ensuring the translation comes from the correct domain. """ if domain is not None: msgid = _TranslateableUnicode(msgid) msgid.domain = domain return msgid N_ = gettext_noop # Lazy functions that evaluate when cast to unicode or str. # These are not to be confused with N_ which returns the msgid unmodified. # AFAIK these aren't currently in use and may be removed. lazy_gettext = lazy_ugettext = lazify(gettext) lazy_ngettext = lazy_ungettext = lazify(ngettext) def format_date(date=None, format='medium'): """Return a date formatted according to the given pattern. This uses the locale of the current request's ``pylons.translator``. :param date: the ``date`` or ``datetime`` object; if `None`, the current date is used :param format: one of "full", "long", "medium", or "short", or a custom date/time pattern :rtype: `unicode` """ return _format_date(date, format, translator.locale)
domain is then retrieved by :func:`gettext` when translation occurs, ensuring the translation comes from the correct domain. """ if domain is not None: msgid = _TranslateableUnicode(msgid) msgid.domain = domain return msgid N_ = gettext_noop # Lazy functions that evaluate when cast to unicode or str. # These are not to be confused with N_ which returns the msgid unmodified. # AFAIK these aren't currently in use and may be removed. lazy_gettext = lazy_ugettext = lazify(gettext) lazy_ngettext = lazy_ungettext = lazify(ngettext) def format_date(date=None, format='medium'): """Return a date formatted according to the given pattern. This uses the locale of the current request's ``pylons.translator``. :param date: the ``date`` or ``datetime`` object; if `None`, the current date is used :param format: one of "full", "long", "medium", or "short", or a custom date/time pattern :rtype: `unicode` """ return _format_date(date, format, pylons.translator.locale)
qualified=True)) raise usage = simplejson.load(usage_req)['lastvalue'] try: usage = float(usage) if pds.max is not None: percent = int(usage / float(pds.max) * 100) else: percent = None usage = convert_with_unit(usage) except (ValueError, TypeError): LOGGER.warning(_("Failed to convert DS %(ds)s on %(host)s: " "value was %(value)s (max: %(max)s)"), { 'ds': pds.name, 'host': host, 'value': usage, 'max': pds.max, }) usage = percent = None return (usage, percent) def ugettext(message): # Lors du passage des tests unitaires, # le code qui définit le traducteur n'est pas exécuté. if not pylons.c.vigilo_turbogears: return message return pylons.c.vigilo_turbogears.ugettext(message) _ = ugettext lazy_ugettext = lazify(ugettext)
def make_app(global_conf, full_stack=True, static_files=True, **app_conf): """Create a Pylons WSGI application and return it ``global_conf`` The inherited configuration for this application. Normally from the [DEFAULT] section of the Paste ini file. ``full_stack`` Whether this application provides a full WSGI stack (by default, meaning it handles its own exceptions and errors). Disable full_stack when this application is "managed" by another WSGI middleware. ``static_files`` Whether this application serves its own static files; disable when another web server is responsible for serving them. ``app_conf`` The application's local configuration. Normally specified in the [app:<name>] section of the Paste ini file (where <name> defaults to main). """ # Configure the Pylons environment config = load_environment(global_conf, app_conf) # The Pylons WSGI app app = PylonsApp(config=config) # Routing/Session/Cache Middleware app = RoutesMiddleware(app, config['routes.map'], singleton=False) app = SessionMiddleware(app, config) # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares) # Set up repoze.what-quickstart authentication: # http://wiki.pylonshq.com/display/pylonscookbook/Authorization+with+repoze.what app = add_auth(app, config) # Set up the TW middleware, as per errors and instructions at: # http://groups.google.com/group/toscawidgets-discuss/browse_thread/thread/c06950b8d1f62db9 # http://toscawidgets.org/documentation/ToscaWidgets/install/pylons_app.html def enable_i18n_for_template(template): template.filters.insert(0, Translator(ugettext)) app = tw.api.make_middleware(app, { 'toscawidgets.framework': 'pylons', 'toscawidgets.framework.default_view': 'genshi', 'toscawidgets.framework.translator': lazify(ugettext), 'toscawidgets.framework.engine_options': {'genshi.loader_callback': enable_i18n_for_template}, }) # If enabled, set up the proxy prefix for routing behind # fastcgi and mod_proxy based deployments. if config.get('proxy_prefix', None): app = setup_prefix_middleware(app, global_conf, config['proxy_prefix']) app = DBSessionRemoverMiddleware(app) # END CUSTOM MIDDLEWARE if asbool(full_stack): # Handle Python exceptions app = ErrorHandler(app, global_conf, **config['pylons.errorware']) # Display error documents for 401, 403, 404 status codes (and # 500 when debug is disabled) if asbool(config['debug']): app = StatusCodeRedirect(app) else: app = StatusCodeRedirect(app, [400, 401, 403, 404, 500]) # Establish the Registry for this application app = RegistryManager(app) if asbool(static_files): # Serve static files static_app = StaticURLParser(config['pylons.paths']['static_files']) app = Cascade([static_app, app]) app.config = config return app
# -*- coding: UTF-8 -*- # This file is a part of the YouTube Import plugin for MediaCore CE. # The source code contained in this file is licensed under the GPL v3 (or at # your option any later version). # See LICENSE.txt in the main project directory for more information. from pylons.i18n.translation import lazify from mediacore.lib.i18n import _ as mcore_ugettext __all__ = ['_'] gettext_domain = 'youtube_import' _ = lazify(lambda msgid: mcore_ugettext(msgid, domain=gettext_domain))