Pyramid Translationstring Not Working On Json Renderer
In a test I am doing in a pyramid application, I am trying to send a translatable text via JSON, but the translation is not working. At the beginning of the file I am importing the
Solution 1:
You need to explicitly translate your message string, using get_localizer()
and Localizer.translate()
:
from pyramid.i18n import get_localizer
@view_config(route_name='transtest', renderer='json')deftranstest_view(request):
message = _('temp-test', default='Temporary test', domain='myapp')
return {'myvar': get_localizer(request).translate(message)}
Normally, templates take care of these steps for you, but for JSON you'll need to do so yourself.
You probably want to define a TranslationStringFactory
for your project, and reuse that to produce your message strings. Add the following to your project:
from pyramid.i18n importTranslationStringFactorymyapp_domain= TranslationStringFactory(domain='myapp')
then use:
from my.project import myapp_domain as _
# ....
message = _('temp-test', default='Temporary test')
Post a Comment for "Pyramid Translationstring Not Working On Json Renderer"