Using get_localizer() within a SQLAlchemy model

24 views
Skip to first unread message

Lele Gaifax

unread,
Apr 9, 2013, 8:12:46 AM4/9/13
to pylons-...@googlegroups.com
Hi all,

I'm "modernizing" my Pylons based SoL application, porting it to
Python 3, rebasing it on Pyramid 1.4.

One thing that's not clear to me is how I should recode the i18n from
the inside of a SQLAlchemy model.

Consider for example the "caption()" method of the Player class[1],
which returns a localized description of the player: that string may be
used by a template to build an HTML page, or returned to an ExtJS
application within a JSON response. In either cases, it seems reasonable
that the method in question returns the already translated string, *not*
an instance of (say) TranslationString, because otherwise the caller
would be required to know that particularity and appropriately call
.translate() on it, basically something like the following:


from pyramid.i18n import TranslationStringFactory, get_localizer

translatable_string = TranslationStringFactory('ex')

def caption():
"Description of the player, made up concatenating his names."

_ = translatable_string

nickname = u"“Lele”"
firstname = u"Emanuele"
lastname = u"Gaifax"

return _(u'<b>$lastname</b> $firstname $nickname',
mapping=dict(firstname=firstname,
lastname=lastname,
nickname=nickname))


@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
localizer = get_localizer(request)
return {'player': localizer.translate(caption())}


One way out would be using "get_current_request()" in the caption()
method, but the documentation explicitly discourages the approach.

Another way could be attaching the right localizer (or the request
itself) to the SQLAlchemy session as soon as a request handler creates
one, but this does not look very elegant neither.

Is there a better/recommended way to do this?

Thank you for any hint,
bye, lele.

[1] http://progetti.metapensiero.it/sol/browser/sol/sol/model/player.py#L128
--
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
le...@metapensiero.it | -- Fortunato Depero, 1929.

Reply all
Reply to author
Forward
0 new messages