Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

i18n change #14

Closed
wants to merge 3 commits into from

3 participants

jeffmad Chris McDonough Rocky Burt
jeffmad

This change corresponds to my other change for i18n. I appreciate any feedback.

jeffmad added some commits
jeffmad in order to get i18n to work with pyramid / jinja2, the translations …
…object must be passed to the jinja2 environment. added code to check if i18n extension is enabled, then if it is enabled, we pull the localizer from the request and install the translations object into the jinja2 environment.
188943d
jeffmad fix broken unit tests 96f8b9b
jeffmad adding i18 in less intrusive way 54ea176
Chris McDonough
Owner

Hi Jeff, sorry I haven't gotten around to reviewing this yet.. trying to get Pyramid core out the door, and once I do, I'll come back to this.

Chris McDonough
Owner

Moved from the Pyramid bugtracker at https://github.com/Pylons/pyramid/issues#issue/102 (by jeffmad) to here, as this is not a Pyramid core issue:

Hi,
Please consider this change coupled with another forthcoming change i have for pyramid_jinja2 to get i18n working with jinja2. As it stands the way that get_localizer works, a default Translation object is created and all other translations are added to this. The consequences of this is that you must pass in the domain to get a translation out, which jinja2 does not do. My proposed fix will postpones the creation of the Translations object until after the translation dirs are searched. This allows one to do lookups without passing in the domain for at least one of the translation objects.

Then in pyramid_jinja2/init.py I install the localizer.translations into the Jinja2 enviornment.

I tried to make the docs as close in tone as i could. I appreciate any feedback that you have.
--jeff

Chris McDonough
Owner

Note that there is a pull request attached to Pylons/pyramid#102 ( jeffmad/pyramid@e718d34 ) that includes Pyramid core changes. These may need to be incorporated.

Rocky Burt
Collaborator

Once (if) the pyramid core changes pull request is accepted, I would definitely consider including the changes here into pyramid_jinja2. But... as it stands, this set of commits lack test coverage. So my suggestion would be to be to create a new pull request by first updating changes to go along with current pyramid_jinja2 master and then adding tests.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 16, 2011
  1. in order to get i18n to work with pyramid / jinja2, the translations …

    jeffmad authored
    …object must be passed to the jinja2 environment. added code to check if i18n extension is enabled, then if it is enabled, we pull the localizer from the request and install the translations object into the jinja2 environment.
  2. fix broken unit tests

    jeffmad authored
  3. adding i18 in less intrusive way

    jeffmad authored
This page is out of date. Refresh to see the latest.
Showing with 8 additions and 2 deletions.
  1. +8 −2 pyramid_jinja2/__init__.py
10 pyramid_jinja2/__init__.py
View
@@ -9,6 +9,7 @@
from pyramid.exceptions import ConfigurationError
from pyramid.resource import abspath_from_resource_spec
+from pyramid.i18n import get_localizer
class IJinja2Environment(Interface):
pass
@@ -51,6 +52,7 @@ def parse_extensions(extensions):
extensions = splitlines(extensions)
return [ maybe_import_string(x) for x in extensions ]
+
def renderer_factory(info):
registry = info.registry
settings = info.settings
@@ -78,14 +80,15 @@ def renderer_factory(info):
extensions=extensions)
environment.filters.update(filters)
registry.registerUtility(environment, IJinja2Environment)
- return Jinja2TemplateRenderer(info, environment)
+ return Jinja2TemplateRenderer(info, environment, 'jinja2.ext.i18n' in settings.get('jinja2.extensions',''))
class Jinja2TemplateRenderer(object):
implements(ITemplateRenderer)
template = None
- def __init__(self, info, environment):
+ def __init__(self, info, environment, i18n_enabled=False):
self.info = info
self.environment = environment
+ self.jinja2_i18n_ext = i18n_enabled
def implementation(self):
return self.template
@@ -99,6 +102,9 @@ def __call__(self, value, system):
system.update(value)
except (TypeError, ValueError):
raise ValueError('renderer was passed non-dictionary as value')
+ if self.jinja2_i18n_ext:
+ localizer = get_localizer(system['request'])
+ self.environment.install_gettext_translations(localizer.translations, newstyle=True)
result = self.template.render(system)
return result
Something went wrong with that request. Please try again.