Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.
| File tw-fe-i18n.diff,
5.1 KB
(added by TimurIzhbulatov, 3 years ago) |
|
|
-
|
|
|
|
| 163 | 163 | if validation is None: |
| 164 | 164 | return params |
| 165 | 165 | |
| | 166 | # An object used by FormEncode to get translator function |
| | 167 | state = type('state', (), |
| | 168 | {'_': staticmethod(pylons_formencode_gettext)}) |
| | 169 | |
| 166 | 170 | #Initialize new_params -- if it never gets updated just return params |
| 167 | 171 | new_params = {} |
| 168 | 172 | |
| … |
… |
|
| 177 | 181 | errors = {} |
| 178 | 182 | for field, validator in validation.validators.iteritems(): |
| 179 | 183 | try: |
| 180 | | validator.to_python(params.get(field)) |
| 181 | | new_params[field] = validator.to_python(params.get(field)) |
| | 184 | # XXX: Is this necessary to call twice? |
| | 185 | #validator.to_python(params.get(field), state) |
| | 186 | new_params[field] = validator.to_python(params.get(field), |
| | 187 | state) |
| 182 | 188 | # catch individual validation errors into the errors dictionary |
| 183 | 189 | except formencode.api.Invalid, inv: |
| 184 | 190 | errors[field] = inv |
| … |
… |
|
| 198 | 204 | elif isinstance(validation.validators, formencode.Schema): |
| 199 | 205 | # A FormEncode Schema object - to_python converts the incoming |
| 200 | 206 | # parameters to sanitized Python values |
| 201 | | new_params = validation.validators.to_python(params) |
| | 207 | new_params = validation.validators.to_python(params, state) |
| 202 | 208 | |
| 203 | | elif hasattr(validation.validators, 'validate') and hasattr(validation, 'needs_controller') and validation.needs_controller: |
| | 209 | elif (hasattr(validation.validators, 'validate') |
| | 210 | and getattr(validation, 'needs_controller', False)): |
| 204 | 211 | # An object with a "validate" method - call it with the parameters |
| 205 | | new_params = validation.validators.validate(controller, params) |
| | 212 | new_params = validation.validators.validate(controller, params, |
| | 213 | state) |
| 206 | 214 | |
| 207 | 215 | elif hasattr(validation.validators, 'validate'): |
| 208 | 216 | # An object with a "validate" method - call it with the parameters |
| 209 | | new_params = validation.validators.validate(params) |
| | 217 | new_params = validation.validators.validate(params, state) |
| 210 | 218 | |
| 211 | 219 | # Theoretically this should not happen... |
| 212 | 220 | if new_params is None: |
| … |
… |
|
| 781 | 789 | return wsgi_app(pylons.request.environ, pylons.request.start_response) |
| 782 | 790 | |
| 783 | 791 | |
| | 792 | def set_formencode_translation(languages): |
| | 793 | ''' |
| | 794 | Set request specific translation of FormEncode |
| | 795 | ''' |
| | 796 | from gettext import translation |
| | 797 | from pylons.i18n import LanguageError |
| | 798 | try: |
| | 799 | t = translation('FormEncode', languages=languages, |
| | 800 | localedir=formencode.api.get_localedir()) |
| | 801 | except IOError, ioe: |
| | 802 | raise LanguageError('IOError: %s' % ioe) |
| | 803 | pylons.c.formencode_translation = t |
| | 804 | |
| | 805 | |
| | 806 | # Idea stolen from Pylons |
| | 807 | def pylons_formencode_gettext(value): |
| | 808 | from pylons.i18n import ugettext as pylons_gettext |
| | 809 | from gettext import NullTranslations |
| | 810 | |
| | 811 | trans = pylons_gettext(value) |
| | 812 | |
| | 813 | # Translation failed, try formencode |
| | 814 | if trans == value: |
| | 815 | fetrans = pylons.c.formencode_translation |
| | 816 | if not fetrans: |
| | 817 | fetrans = NullTranslations() |
| | 818 | trans = fetrans.ugettext(value) |
| | 819 | |
| | 820 | return trans |
| | 821 | |
| | 822 | |
| 784 | 823 | def setup_i18n(): |
| 785 | 824 | from pylons.i18n import add_fallback, set_lang, LanguageError |
| 786 | 825 | languages = pylons.request.accept_language.best_matches() |
| … |
… |
|
| 799 | 838 | |
| 800 | 839 | # if any language is left, set the best match as a default |
| 801 | 840 | if languages: |
| 802 | | set_lang(languages[0]) |
| 803 | | log.info("Set request language to %s", languages[0]) |
| | 841 | try: |
| | 842 | set_lang(languages[0]) |
| | 843 | except LanguageError: |
| | 844 | log.debug("Language %s: not supported", languages[0]) |
| | 845 | else: |
| | 846 | log.info("Set request language to %s", languages[0]) |
| 804 | 847 | |
| | 848 | try: |
| | 849 | set_formencode_translation(languages) |
| | 850 | except LanguageError: |
| | 851 | log.debug("Language %s: not supported by FormEncode", |
| | 852 | languages[0]) |
| | 853 | else: |
| | 854 | log.info("Set request language for FormEncode to %s", |
| | 855 | languages[0]) |
| | 856 | |
| | 857 | |
| 805 | 858 | __all__ = [ |
| 806 | 859 | "DecoratedController", "ObjectDispatchController", "TGController", |
| 807 | 860 | "url", "redirect", "RestController" |
-
|
|
|
|
| 501 | 501 | def add_tosca_middleware(self, app): |
| 502 | 502 | """Configure the ToscaWidgets middleware.""" |
| 503 | 503 | app = tw_middleware(app, { |
| 504 | | 'toscawidgets.framework.default_view': |
| 505 | | self.default_renderer, |
| | 504 | 'toscawidgets.framework.default_view': self.default_renderer, |
| | 505 | 'toscawidgets.framework.translator': ugettext, |
| 506 | 506 | 'toscawidgets.middleware.inject_resources': True, |
| 507 | 507 | }) |
| 508 | 508 | return app |
Download in other formats: