Skip to content
This repository

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.

added some commits
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
fix broken unit tests 96f8b9b
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

Showing 3 unique commits by 1 author.

Jan 15, 2011
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
fix broken unit tests 96f8b9b
Jan 16, 2011
adding i18 in less intrusive way 54ea176
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 8 additions and 2 deletions. Show diff stats Hide diff stats

  1. 10  pyramid_jinja2/__init__.py
10  pyramid_jinja2/__init__.py
@@ -9,6 +9,7 @@
9 9
 
10 10
 from pyramid.exceptions import ConfigurationError
11 11
 from pyramid.resource import abspath_from_resource_spec
  12
+from pyramid.i18n import get_localizer
12 13
 
13 14
 class IJinja2Environment(Interface):
14 15
     pass
@@ -51,6 +52,7 @@ def parse_extensions(extensions):
51 52
         extensions = splitlines(extensions)
52 53
     return [ maybe_import_string(x) for x in extensions ]
53 54
 
  55
+
54 56
 def renderer_factory(info):
55 57
     registry = info.registry
56 58
     settings = info.settings
@@ -78,14 +80,15 @@ def renderer_factory(info):
78 80
                                   extensions=extensions)
79 81
         environment.filters.update(filters)
80 82
         registry.registerUtility(environment, IJinja2Environment)
81  
-    return Jinja2TemplateRenderer(info, environment)
  83
+    return Jinja2TemplateRenderer(info, environment, 'jinja2.ext.i18n' in settings.get('jinja2.extensions',''))
82 84
 
83 85
 class Jinja2TemplateRenderer(object):
84 86
     implements(ITemplateRenderer)
85 87
     template = None
86  
-    def __init__(self, info, environment):
  88
+    def __init__(self, info, environment, i18n_enabled=False):
87 89
         self.info = info
88 90
         self.environment = environment
  91
+        self.jinja2_i18n_ext = i18n_enabled
89 92
  
90 93
     def implementation(self):
91 94
         return self.template
@@ -99,6 +102,9 @@ def __call__(self, value, system):
99 102
             system.update(value)
100 103
         except (TypeError, ValueError):
101 104
             raise ValueError('renderer was passed non-dictionary as value')
  105
+        if self.jinja2_i18n_ext:
  106
+            localizer = get_localizer(system['request'])
  107
+            self.environment.install_gettext_translations(localizer.translations, newstyle=True)
102 108
         result = self.template.render(system)
103 109
         return result
104 110
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.