# HG changeset patch # User Thierry Florac # Date 1427581523 -3600 # Node ID 0e4957081cd1aa438c67ba8c39f616a8bd21608f # Parent 08740e98e10920142898c75329ad5c9a65e6c014 Renamed TALES extension. Requires updated Chameleon package diff -r 08740e98e109 -r 0e4957081cd1 src/pyams_i18n/expr.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pyams_i18n/expr.py Sat Mar 28 23:25:23 2015 +0100 @@ -0,0 +1,54 @@ +# +# Copyright (c) 2008-2015 Thierry Florac +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# + +__docformat__ = 'restructuredtext' + + +# import standard library + +# import interfaces +from pyams_i18n.interfaces import INegotiator + +# import packages +from chameleon.astutil import Symbol +from chameleon.tales import StringExpr +from pyams_utils.registry import query_utility +from pyams_utils.tales import ContextExprMixin + + +def render_i18n_expression(econtext, name): + """Render an I18n expression""" + + name = name.strip() + if '.' in name: + context_name, attr = name.split('.') + else: + context_name = 'context', + attr = name + context = econtext.get(context_name) + result = getattr(context, attr) + if not isinstance(result, dict): + return result + request = econtext.get('request') + lang = request.locale_name + value = result.get(lang) + if not value: + negotiator = query_utility(INegotiator) + if (negotiator is not None) and (negotiator.server_language != lang): + return result.get(negotiator.server_language) + return value + + +class I18nExpr(ContextExprMixin, StringExpr): + """i18n:context.attribute TALES expression""" + + transform = Symbol(render_i18n_expression) diff -r 08740e98e109 -r 0e4957081cd1 src/pyams_i18n/include.py --- a/src/pyams_i18n/include.py Sat Mar 28 19:27:23 2015 +0100 +++ b/src/pyams_i18n/include.py Sat Mar 28 23:25:23 2015 +0100 @@ -45,5 +45,5 @@ if hasattr(config, 'load_zcml'): config.load_zcml('configure.zcml') - PageTemplateFile.expression_types['intl'] = I18nExpr - Z3cPageTemplateFile.expression_types['intl'] = I18nExpr + PageTemplateFile.expression_types['i18n'] = I18nExpr + Z3cPageTemplateFile.expression_types['i18n'] = I18nExpr