Changeset 7705
- Timestamp:
- Nov 22, 2008, 12:04:14 AM (15 years ago)
- Location:
- trunk/trac
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/trac/util/translation.py
r7498 r7705 15 15 16 16 import re 17 import sys 17 18 try: 18 19 import threading … … 53 54 return _tag_kwargs(string, kwargs) 54 55 56 def add_domain(domain, env_path, locale_dir): 57 pass 58 59 def domain_functions(domain, *symbols): 60 _functions = { 61 'gettext': gettext_noop, 62 '_': gettext_noop, 63 'N_': gettext_noop, 64 'ngettext': ngettext_noop, 65 'tgettext': tgettext_noop, 66 'tag_': tgettext_noop, 67 'tngettext': tngettext_noop, 68 'add_domain': add_domain, 69 } 70 return [_functions(s) for s in symbols] 71 55 72 56 73 try: … … 58 75 from gettext import NullTranslations 59 76 60 _current = threading.local() 61 62 def gettext(string, **kwargs): 63 def _gettext(): 64 trans = get_translations().ugettext(string) 65 return kwargs and trans % kwargs or trans 66 if not hasattr(_current, 'translations'): 67 return LazyProxy(_gettext) 68 return _gettext() 69 _ = gettext 70 71 def ngettext(singular, plural, num, **kwargs): 72 kwargs = kwargs.copy() 73 kwargs.setdefault('num', num) 74 def _ngettext(): 75 trans = get_translations().ungettext(singular, plural, num) 76 return trans % kwargs 77 if not hasattr(_current, 'translations'): 78 return LazyProxy(_ngettext) 79 return _ngettext() 80 81 def tgettext(string, **kwargs): 82 def _tgettext(): 83 trans = get_translations().ugettext(string) 84 return kwargs and _tag_kwargs(trans, kwargs) or trans 85 if not hasattr(_current, 'translations'): 86 return LazyProxy(_tgettext) 87 return _tgettext() 88 tag_ = tgettext 89 90 def tngettext(singular, plural, num, **kwargs): 91 kwargs = kwargs.copy() 92 kwargs.setdefault('num', num) 93 def _tngettext(): 94 trans = get_translations().ungettext(singular, plural, num) 95 return _tag_kwargs(trans, kwargs) 96 if not hasattr(_current, 'translations'): 97 return LazyProxy(_tngettext) 98 return _tngettext() 99 100 def activate(locale): 101 locale_dir = pkg_resources.resource_filename(__name__, '../locale') 102 _current.translations = Translations.load(locale_dir, locale) 103 104 _null_translations = NullTranslations() 77 class TranslationsProxy(object): 78 """Delegate Translations calls to the currently active Translations. 79 80 If there's none, wrap those calls in LazyProxy objects. 81 """ 82 83 def __init__(self): 84 self._current = threading.local() 85 self._null_translations = NullTranslations() 86 self._plugin_domains = {} 87 self._plugin_domains_lock = threading.RLock() 88 89 # Public API 90 91 def add_domain(self, domain, env_path, locales_dir): 92 self._plugin_domains_lock.acquire() 93 try: 94 if env_path not in self._plugin_domains: 95 self._plugin_domains[env_path] = [] 96 self._plugin_domains[env_path].append((domain, locales_dir)) 97 finally: 98 self._plugin_domains_lock.release() 99 100 def activate(self, locale, env_path=None): 101 locale_dir = pkg_resources.resource_filename(__name__, '../locale') 102 t = Translations.load(locale_dir, locale) 103 if env_path: 104 self._plugin_domains_lock.acquire() 105 try: 106 domains = list(self._plugin_domains.get(env_path, [])) 107 finally: 108 self._plugin_domains_lock.release() 109 for domain, dirname in domains: 110 t.add(Translations.load(dirname, locale, domain)) 111 self._current.translations = t 112 113 def deactivate(self): 114 del self._current.translations 115 116 @property 117 def active(self): 118 return getattr(self._current, 'translations', 119 self._null_translations) 120 121 @property 122 def isactive(self): 123 return hasattr(self._current, 'translations') 124 125 # Delegated methods 126 127 def __getattr__(self, name): 128 return getattr(self.active, name) 129 130 def gettext(self, string, **kwargs): 131 def _gettext(): 132 trans = self.active.ugettext(string) 133 return kwargs and trans % kwargs or trans 134 if not self.isactive: 135 return LazyProxy(_gettext) 136 return _gettext() 137 138 def dgettext(self, domain, string, **kwargs): 139 def _dgettext(): 140 trans = self.active.dugettext(domain, string) 141 return kwargs and trans % kwargs or trans 142 if not self.isactive: 143 return LazyProxy(_dgettext) 144 return _dgettext() 145 146 def ngettext(self, singular, plural, num, **kwargs): 147 kwargs = kwargs.copy() 148 kwargs.setdefault('num', num) 149 def _ngettext(): 150 trans = self.active.ungettext(singular, plural, num) 151 return trans % kwargs 152 if not self.isactive: 153 return LazyProxy(_ngettext) 154 return _ngettext() 155 156 def dngettext(self, domain, singular, plural, num, **kwargs): 157 kwargs = kwargs.copy() 158 kwargs.setdefault('num', num) 159 def _dngettext(): 160 trans = self.active.dungettext(domain, singular, plural, num) 161 return trans % kwargs 162 if not self.isactive: 163 return LazyProxy(_dngettext) 164 return _dngettext() 165 166 def tgettext(self, string, **kwargs): 167 def _tgettext(): 168 trans = self.active.ugettext(string) 169 return kwargs and _tag_kwargs(trans, kwargs) or trans 170 if not self.isactive: 171 return LazyProxy(_tgettext) 172 return _tgettext() 173 174 def dtgettext(self, domain, string, **kwargs): 175 def _dtgettext(): 176 trans = self.active.dugettext(domain, string) 177 return kwargs and _tag_kwargs(trans, kwargs) or trans 178 if not self.isactive: 179 return LazyProxy(_dtgettext) 180 return _dtgettext() 181 182 def tngettext(self, singular, plural, num, **kwargs): 183 kwargs = kwargs.copy() 184 kwargs.setdefault('num', num) 185 def _tngettext(): 186 trans = self.active.ungettext(singular, plural, num) 187 return _tag_kwargs(trans, kwargs) 188 if not self.isactive: 189 return LazyProxy(_tngettext) 190 return _tngettext() 191 192 def dtngettext(self, domain, singular, plural, num, **kwargs): 193 kwargs = kwargs.copy() 194 def _dtngettext(): 195 trans = self.active.dungettext(domain, singular, plural, num) 196 if '%(num)' in trans: 197 kwargs.update(num=num) 198 return kwargs and _tag_kwargs(trans, kwargs) or trans 199 if not self.isactive: 200 return LazyProxy(_dtngettext) 201 return _dtngettext() 202 203 204 translations = TranslationsProxy() 205 206 def domain_functions(domain, *symbols): 207 _functions = { 208 'gettext': translations.dgettext, 209 '_': translations.dgettext, 210 'ngettext': translations.dngettext, 211 'tgettext': translations.dtgettext, 212 'tag_': translations.dtgettext, 213 'tngettext': translations.dtngettext, 214 'add_domain': translations.add_domain, 215 } 216 def wrapdomain(symbol): 217 if symbol == 'N_': 218 return gettext_noop 219 return lambda *args, **kw: _functions[symbol](domain, *args, **kw) 220 return [wrapdomain(s) for s in symbols] 221 222 gettext = translations.gettext 223 _ = gettext 224 dgettext = translations.dgettext 225 ngettext = translations.ngettext 226 dngettext = translations.dngettext 227 tgettext = translations.tgettext 228 tag_ = tgettext 229 dtgettext = translations.dtgettext 230 tngettext = translations.tngettext 231 dtngettext = translations.dtngettext 232 233 def deactivate(): 234 translations.deactivate() 235 236 def activate(locale, env_path=None): 237 translations.activate(locale, env_path) 238 239 def add_domain(domain, env_path, locale_dir): 240 translations.add_domain(domain, env_path, locale_dir) 105 241 106 242 def get_translations(): 107 return getattr(_current, 'translations', _null_translations) 108 109 def deactivate(): 110 del _current.translations 243 return translations 111 244 112 245 def get_available_locales(): -
trunk/trac/web/api.py
r7560 r7705 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, -
trunk/trac/web/chrome.py
r7629 r7705 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 … … 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, … … 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(), -
trunk/trac/web/main.py
r7703 r7705 166 166 try: 167 167 try: 168 translation.activate(req.locale )168 translation.activate(req.locale, self.env.path) 169 169 170 170 # Select the component that should handle the request
Note:
See TracChangeset
for help on using the changeset viewer.