Skip to content

Instantly share code, notes, and snippets.

@dwt
Created May 6, 2013 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwt/5524039 to your computer and use it in GitHub Desktop.
Save dwt/5524039 to your computer and use it in GitHub Desktop.
Workaround for the inability of TranslationString to combine multiple TranslationStrings
from pyramid.i18n import TranslationStringFactory
_ = TranslationStringFactory('yeepa')
def _get_localizer():
from pyramid.i18n import get_localizer, make_localizer
from pyramid.threadlocal import get_current_request
if get_current_request():
# pyramid context
return get_localizer(get_current_request())
else:
# most likely tornado context, may also be cron or whatever
from yeepa.gameserver.application import configuration
# relying on the fact that this is memoized and initialized correctly from the first call.
config = configuration()
locale_name = config.get('app:yeepa_backend', 'pyramid.default_locale_name')
import pkg_resources
translation_dir = pkg_resources.resource_filename('yeepa', 'locale')
return make_localizer(locale_name, [translation_dir])
def translate_immediately(a_string):
localizer = _get_localizer()
return localizer.translate(_(a_string))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment