diff --git a/trac/admin/console.py b/trac/admin/console.py
|
a
|
b
|
Type: '?' or 'help' for help on command |
| 169 | 169 | # fixup language according to env settings |
| 170 | 170 | if has_babel: |
| 171 | 171 | default = env.config.get('trac', 'default_language', '') |
| 172 | | negotiated = get_negotiated_locale([LANG, default]) |
| | 172 | negotiated = get_negotiated_locale([LANG, default], env.log) |
| 173 | 173 | if negotiated: |
| 174 | | translation.activate(negotiated) |
| | 174 | translation.activate(negotiated, env.path, env.log) |
| 175 | 175 | |
| 176 | 176 | ## |
| 177 | 177 | ## Utility methods |
diff --git a/trac/prefs/web_ui.py b/trac/prefs/web_ui.py
|
a
|
b
|
class PreferencesModule(Component): |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | if Locale: |
| 109 | | locales = map(Locale.parse, get_available_locales()) |
| | 109 | locales = map(Locale.parse, get_available_locales(self.log)) |
| 110 | 110 | languages = sorted([(str(locale).replace('_','-'), |
| 111 | 111 | locale.display_name) for locale in locales]) |
| 112 | 112 | data['locales'] = locales |
diff --git a/trac/util/translation.py b/trac/util/translation.py
|
a
|
b
|
try: |
| 135 | 135 | finally: |
| 136 | 136 | self._plugin_domains_lock.release() |
| 137 | 137 | |
| 138 | | def make_activable(self, get_locale, env_path=None): |
| 139 | | self._current.args = (get_locale, env_path) |
| | 138 | def make_activable(self, get_locale, env_path=None, log=None): |
| | 139 | self._current.args = (get_locale, env_path, log) |
| 140 | 140 | |
| 141 | | def activate(self, locale, env_path=None): |
| | 141 | def activate(self, locale, env_path=None, log=None): |
| 142 | 142 | try: |
| 143 | 143 | locale_dir = pkg_resources.resource_filename('trac', 'locale') |
| 144 | 144 | except pkg_resources.ExtractionError: |
| 145 | 145 | return # delay extraction |
| 146 | | except KeyError: |
| 147 | | return # No locale data in egg |
| | 146 | except Exception, e: |
| | 147 | if log is not None: |
| | 148 | from trac.util.text import exception_to_unicode |
| | 149 | log.error("Unable to find locales: %s", |
| | 150 | exception_to_unicode(e)) |
| | 151 | return |
| 148 | 152 | t = Translations.load(locale_dir, locale or 'en_US') |
| 149 | 153 | if not t or t.__class__ is NullTranslations: |
| 150 | 154 | t = self._null_translations |
| … |
… |
try: |
| 174 | 178 | @property |
| 175 | 179 | def isactive(self): |
| 176 | 180 | if self._current.args is not None: |
| 177 | | get_locale, env_path = self._current.args |
| | 181 | get_locale, env_path, log = self._current.args |
| 178 | 182 | self._current.args = None |
| 179 | | self.activate(get_locale(), env_path) |
| | 183 | self.activate(get_locale(), env_path, log) |
| 180 | 184 | return self._current.translations is not None |
| 181 | 185 | |
| 182 | 186 | # Delegated methods |
| … |
… |
try: |
| 309 | 313 | """ |
| 310 | 314 | return translations.reactivate(t) |
| 311 | 315 | |
| 312 | | def make_activable(get_locale, env_path=None): |
| | 316 | def make_activable(get_locale, env_path=None, log=None): |
| 313 | 317 | """Defer activation of translations. |
| 314 | 318 | :param get_locale: a callable returning a Babel Locale object |
| 315 | 319 | :param env_path: the environment to use for looking up catalogs |
| 316 | 320 | """ |
| 317 | | translations.make_activable(get_locale, env_path) |
| | 321 | translations.make_activable(get_locale, env_path, log) |
| 318 | 322 | |
| 319 | | def activate(locale, env_path=None): |
| 320 | | translations.activate(locale, env_path) |
| | 323 | def activate(locale, env_path=None, log=None): |
| | 324 | translations.activate(locale, env_path, log) |
| 321 | 325 | |
| 322 | 326 | def add_domain(domain, env_path, locale_dir): |
| 323 | 327 | translations.add_domain(domain, env_path, locale_dir) |
| … |
… |
try: |
| 325 | 329 | def get_translations(): |
| 326 | 330 | return translations |
| 327 | 331 | |
| 328 | | def get_available_locales(): |
| | 332 | def get_available_locales(log=None): |
| 329 | 333 | """Return a list of locale identifiers of the locales for which |
| 330 | 334 | translations are available. |
| 331 | 335 | """ |
| 332 | | return [dirname for dirname |
| 333 | | in pkg_resources.resource_listdir('trac', 'locale') |
| 334 | | if '.' not in dirname] |
| | 336 | try: |
| | 337 | return [dirname for dirname |
| | 338 | in pkg_resources.resource_listdir('trac', 'locale') |
| | 339 | if '.' not in dirname] |
| | 340 | except Exception, e: |
| | 341 | if log is not None: |
| | 342 | from trac.util.text import exception_to_unicode |
| | 343 | log.error("Unable to find locales: %s", |
| | 344 | exception_to_unicode(e)) |
| | 345 | return [] |
| 335 | 346 | |
| 336 | | def get_negotiated_locale(preferred_locales): |
| | 347 | def get_negotiated_locale(preferred_locales, log=None): |
| 337 | 348 | def normalize(locale_ids): |
| 338 | 349 | return [id.replace('_', '-') for id in locale_ids if id] |
| 339 | 350 | return Locale.negotiate(normalize(preferred_locales), |
| 340 | | normalize(get_available_locales()), sep='-') |
| | 351 | normalize(get_available_locales(log)), sep='-') |
| 341 | 352 | |
| 342 | 353 | has_babel = True |
| 343 | 354 | |
| … |
… |
except ImportError: # fall back on 0.11 |
| 353 | 364 | |
| 354 | 365 | translations = NullTranslationsBabel() |
| 355 | 366 | |
| 356 | | def activate(locale, env_path=None): |
| | 367 | def activate(locale, env_path=None, log=None): |
| 357 | 368 | pass |
| 358 | 369 | |
| 359 | 370 | def deactivate(): |
| … |
… |
except ImportError: # fall back on 0.11 |
| 362 | 373 | def reactivate(t): |
| 363 | 374 | pass |
| 364 | 375 | |
| 365 | | def make_activable(get_locale, env_path=None): |
| | 376 | def make_activable(get_locale, env_path=None, log=None): |
| 366 | 377 | pass |
| 367 | 378 | |
| 368 | 379 | def get_translations(): |
| 369 | 380 | return translations |
| 370 | 381 | |
| 371 | | def get_available_locales(): |
| | 382 | def get_available_locales(log=None): |
| 372 | 383 | return [] |
| 373 | 384 | |
| 374 | | def get_negotiated_locale(preferred=None, default=None): |
| | 385 | def get_negotiated_locale(preferred=None, log=None): |
| 375 | 386 | return None |
diff --git a/trac/web/main.py b/trac/web/main.py
|
a
|
b
|
class RequestDispatcher(Component): |
| 308 | 308 | preferred = req.session.get('language') |
| 309 | 309 | default = self.env.config.get('trac', 'default_language', '') |
| 310 | 310 | negotiated = get_negotiated_locale([preferred, default] + |
| 311 | | req.languages) |
| | 311 | req.languages, self.log) |
| 312 | 312 | self.log.debug("Negotiated locale: %s -> %s", preferred, negotiated) |
| 313 | 313 | return negotiated |
| 314 | 314 | |
| … |
… |
def dispatch_request(environ, start_resp |
| 474 | 474 | env_error = e |
| 475 | 475 | |
| 476 | 476 | req = Request(environ, start_response) |
| 477 | | translation.make_activable(lambda: req.locale, env and env.path or None) |
| | 477 | translation.make_activable(lambda: req.locale, env and env.path or None, |
| | 478 | env and env.log or None) |
| 478 | 479 | try: |
| 479 | 480 | return _dispatch_request(req, env, env_error) |
| 480 | 481 | finally: |