diff options
| author | Christopher Lambacher <chris@kateandchris.net> | 2011-02-12 14:13:43 -0500 |
|---|---|---|
| committer | Christopher Lambacher <chris@kateandchris.net> | 2011-02-12 16:55:23 -0500 |
| commit | 25168263d244eb7912a3f2fe6eb3dc8061eb4266 (patch) | |
| tree | a687727b4dc2dc6a6be680cc0d7bbf269a0a80d2 | |
| parent | ce6c2e400b8aee1271d8ba506b593c5335af3568 (diff) | |
| download | pyramid-25168263d244eb7912a3f2fe6eb3dc8061eb4266.tar.gz pyramid-25168263d244eb7912a3f2fe6eb3dc8061eb4266.tar.bz2 pyramid-25168263d244eb7912a3f2fe6eb3dc8061eb4266.zip | |
Separate making Localizer from getting cached one
| -rw-r--r-- | pyramid/i18n.py | 50 |
1 files changed, 27 insertions, 23 deletions
diff --git a/pyramid/i18n.py b/pyramid/i18n.py index ee4a693da..2ec0f2f78 100644 --- a/pyramid/i18n.py +++ b/pyramid/i18n.py @@ -145,6 +145,31 @@ def get_locale_name(request): request.locale_name = locale_name return locale_name +def make_localizer(current_locale_name, registry): + translations = Translations() + translations._catalog = {} + tdirs = registry.queryUtility(ITranslationDirectories, default=[]) + for tdir in tdirs: + locale_dirs = [ (lname, os.path.join(tdir, lname)) for lname in + os.listdir(tdir) ] + for locale_name, locale_dir in locale_dirs: + if locale_name != current_locale_name: + continue + messages_dir = os.path.join(locale_dir, 'LC_MESSAGES') + if not os.path.isdir(os.path.realpath(messages_dir)): + continue + for mofile in os.listdir(messages_dir): + mopath = os.path.realpath(os.path.join(messages_dir, + mofile)) + if mofile.endswith('.mo') and os.path.isfile(mopath): + mofp = open(mopath, 'rb') + domain = mofile[:-3] + dtrans = Translations(mofp, domain) + translations.add(dtrans) + + return Localizer(locale_name=current_locale_name, + translations=translations) + def get_localizer(request): """ Retrieve a :class:`pyramid.i18n.Localizer` object corresponding to the current request's locale name. """ @@ -162,29 +187,8 @@ def get_localizer(request): if localizer is None: # no localizer utility registered yet - translations = Translations() - translations._catalog = {} - tdirs = registry.queryUtility(ITranslationDirectories, default=[]) - for tdir in tdirs: - locale_dirs = [ (lname, os.path.join(tdir, lname)) for lname in - os.listdir(tdir) ] - for locale_name, locale_dir in locale_dirs: - if locale_name != current_locale_name: - continue - messages_dir = os.path.join(locale_dir, 'LC_MESSAGES') - if not os.path.isdir(os.path.realpath(messages_dir)): - continue - for mofile in os.listdir(messages_dir): - mopath = os.path.realpath(os.path.join(messages_dir, - mofile)) - if mofile.endswith('.mo') and os.path.isfile(mopath): - mofp = open(mopath, 'rb') - domain = mofile[:-3] - dtrans = Translations(mofp, domain) - translations.add(dtrans) - - localizer = Localizer(locale_name=current_locale_name, - translations=translations) + localizer = make_localizer(current_locale_name, registry) + registry.registerUtility(localizer, ILocalizer, name=current_locale_name) request.localizer = localizer |
