Ticket #7497: plugin_i18n_support-r7676.patch
| File plugin_i18n_support-r7676.patch, 7.4 KB (added by cboos, 8 weeks ago) |
|---|
-
trac/web/api.py
369 369 from trac.web.chrome import Chrome 370 370 from trac.util import translation 371 371 if hasattr(self, 'locale'): 372 translation.activate(self.locale )372 translation.activate(self.locale, env.path) 373 373 try: 374 374 data = Chrome(env).render_template(self, template, 375 375 data, 'text/html') -
trac/web/chrome.py
26 26 27 27 from genshi import Markup 28 28 from genshi.builder import tag, Element 29 from genshi.filters import Translator 29 from genshi.filters import Translator, setup_i18n 30 30 from genshi.input import HTML, ParseError 31 31 from genshi.core import Attrs, START 32 32 from genshi.output import DocType … … 293 293 'classes': presentation.classes, 294 294 'date': datetime.date, 295 295 'datetime': datetime.datetime, 296 'dgettext': translation.dgettext, 297 'dngettext': translation.dngettext, 296 298 'first_last': presentation.first_last, 297 299 'get_reporter_id': get_reporter_id, 298 300 'gettext': translation.gettext, … … 660 662 """ 661 663 if not self.templates: 662 664 def _template_loaded(template): 663 template.filters.insert(0, Translator(translation.gettext)) 665 translator = Translator(translation.get_translations()) 666 setup_i18n(template, translator) 664 667 665 668 self.templates = TemplateLoader(self.get_all_templates_dirs(), 666 669 auto_reload=self.auto_reload, -
trac/web/main.py
164 164 try: 165 165 try: 166 166 try: 167 translation.activate(req.locale )167 translation.activate(req.locale, self.env.path) 168 168 169 169 # Select the component that should handle the request 170 170 chosen_handler = None -
trac/util/translation.py
14 14 """Utilities for text translation with gettext.""" 15 15 16 16 import re 17 import sys 17 18 try: 18 19 import threading 19 20 except ImportError: … … 63 64 def _gettext(): 64 65 trans = get_translations().ugettext(string) 65 66 return kwargs and trans % kwargs or trans 67 domain = sys._getframe(1).f_globals.get('domain') 68 if domain: 69 # It has, use that domain 70 return dgettext(domain, string, **kwargs) 66 71 if not hasattr(_current, 'translations'): 67 72 return LazyProxy(_gettext) 68 73 return _gettext() 69 74 _ = gettext 75 76 def dgettext(domain, string, **kwargs): 77 def _dgettext(): 78 trans = get_translations().dugettext(domain, string) 79 return kwargs and trans % kwargs or trans 80 if not hasattr(_current, 'translations'): 81 return LazyProxy(_dgettext) 82 return _dgettext() 70 83 71 84 def ngettext(singular, plural, num, **kwargs): 72 85 kwargs = kwargs.copy() 73 86 kwargs.setdefault('num', num) 87 # The caller is a plugin with domain variable defined ??? 88 domain = sys._getframe(1).f_globals.get('domain') 89 if domain: 90 # It has, use that domain 91 return dngettext(domain, singular, plural, num, **kwargs) 74 92 def _ngettext(): 75 93 trans = get_translations().ungettext(singular, plural, num) 76 94 return trans % kwargs … … 78 96 return LazyProxy(_ngettext) 79 97 return _ngettext() 80 98 99 def dngettext(domain, singular, plural, num, **kwargs): 100 kwargs = kwargs.copy() 101 kwargs.setdefault('num', num) 102 def _dngettext(): 103 trans = get_translations().dungettext(domain, singular, plural, num) 104 return trans % kwargs 105 if not hasattr(_current, 'translations'): 106 return LazyProxy(_dngettext) 107 return _dngettext() 108 81 109 def tgettext(string, **kwargs): 82 110 def _tgettext(): 83 111 trans = get_translations().ugettext(string) 84 112 return kwargs and _tag_kwargs(trans, kwargs) or trans 113 # The caller is a plugin with domain variable defined ??? 114 domain = sys._getframe(1).f_globals.get('domain') 115 if domain: 116 # It has, use that domain 117 return dtgettext(domain, string, **kwargs) 85 118 if not hasattr(_current, 'translations'): 86 119 return LazyProxy(_tgettext) 87 120 return _tgettext() 88 121 tag_ = tgettext 122 123 def dtgettext(domain, string, **kwargs): 124 def _dtgettext(): 125 trans = get_translations().dugettext(domain, string) 126 return kwargs and _tag_kwargs(trans, kwargs) or trans 127 if not hasattr(_current, 'translations'): 128 return LazyProxy(_dtgettext) 129 return _dtgettext() 89 130 90 131 def tngettext(singular, plural, num, **kwargs): 91 132 kwargs = kwargs.copy() … … 93 134 def _tngettext(): 94 135 trans = get_translations().ungettext(singular, plural, num) 95 136 return _tag_kwargs(trans, kwargs) 137 # The caller is a plugin with domain variable defined ??? 138 domain = sys._getframe(1).f_globals.get('domain') 139 if domain: 140 # It has, use that domain 141 return dtngettext(domain, singular, plural, num, **kwargs) 96 142 if not hasattr(_current, 'translations'): 97 143 return LazyProxy(_tngettext) 98 144 return _tngettext() 99 145 100 def activate(locale): 146 def dtngettext(domain, singular, plural, num, **kwargs): 147 kwargs = kwargs.copy() 148 def _dtngettext(): 149 trans = get_translations().dungettext(domain, singular, plural, num) 150 if '%(num)' in trans: 151 kwargs.update(num=num) 152 return kwargs and _tag_kwargs(trans, kwargs) or trans 153 if not hasattr(_current, 'translations'): 154 return LazyProxy(_dtngettext) 155 return _dtngettext() 156 157 _plugin_domains = {} 158 159 def activate(locale, env_path=None): 101 160 locale_dir = pkg_resources.resource_filename(__name__, '../locale') 102 161 _current.translations = Translations.load(locale_dir, locale) 103 162 if env_path: 163 plugin_domains = _plugin_domains.get(env_path) 164 if plugin_domains: 165 for domain, dirname in plugin_domains: 166 dt = Translations.load(dirname, locale, domain) 167 _current.translations.add(dt) 168 104 169 _null_translations = NullTranslations() 105 170 106 171 def get_translations(): … … 108 173 109 174 def deactivate(): 110 175 del _current.translations 176 177 def add_domain(env_path, domain, locales_dir): 178 if env_path not in _plugin_domains: 179 _plugin_domains[env_path] = [] 180 _plugin_domains[env_path].append((domain, locales_dir)) 111 181 112 182 def get_available_locales(): 113 183 """Return a list of locale identifiers of the locales for which
