def deactivate(): """ Deinstalls the currently active translation object so that further _ calls will resolve against the default translation object, again. """ global _active if currentThread() in _active: del _active[currentThread()]
def get_script_prefix(): """ Returns the currently active script prefix. Useful for client code that wishes to construct their own URLs manually (although accessing the request instance is normally going to be a lot cleaner). """ return _prefixes.get(currentThread(), '/')
def get_script_prefix(): """ Returns the currently active script prefix. Useful for client code that wishes to construct their own URLs manually (although accessing the request instance is normally going to be a lot cleaner). """ return _prefixes.get(currentThread(), u'/')
def deactivate_all(): """ Makes the active translation object a NullTranslations() instance. This is useful when we want delayed translations to appear as the original string for some reason. """ _active[currentThread()] = gettext_module.NullTranslations()
def add_device_locale(device=None): global device_translations from django.conf import settings if not device or not settings.USE_I18N: return current_translation = trans._active[currentThread()] lang = current_translation.language() key = "%s#%s" % (lang, device.id) loc = trans.to_locale(lang) klass = trans.DjangoTranslation if sys.version_info < (2, 4): klass = trans.DjangoTranslation23 device_trans = device_translations.get(key, None) if device_trans is None: translations_paths = get_translations_paths(device) for translations_path in translations_paths: try: t = gettext_module.translation('django', translations_path, [loc], klass) t.set_language(lang) if device_trans is None: device_trans = t else: device_trans.merge(t) except IOError, e: pass device_translations[key] = device_trans fallback = getattr(device_trans, '_fallback', None) if not fallback and hasattr(device_trans, 'add_fallback'): device_trans.add_fallback(current_translation)
def activate(language): """ Fetches the translation object for a given tuple of application name and language and installs it as the current translation object for the current thread. """ _active[currentThread()] = translation(language)
def activate(locale): """ Override django's utils.translation.activate(). Django forces files to be named django.mo (http://code.djangoproject.com/ticket/6376). Since that's dumb and we want to be able to load different files depending on what part of the site the user is in, we'll make our own function here. """ class Translation(object): """ We pass this object to jinja so it can find our gettext implementation. If we pass the GNUTranslation object directly, it won't have our context and whitespace stripping action. """ ugettext = staticmethod(ugettext) ungettext = staticmethod(ungettext) import jingo jingo.env.install_gettext_translations(Translation) if django.VERSION >= (1, 3): django_trans._active.value = _activate(locale) else: from django.utils.thread_support import currentThread django_trans._active[currentThread()] = _activate(locale)
def get_timezone(): t = _active.get(currentThread(), None) if t is not None: return t from django.conf import settings return utils.coerce_timezone_value(settings.TIME_ZONE)
def set_script_prefix(prefix): """ Sets the script prefix for the current thread. """ if not prefix.endswith('/'): prefix += '/' _prefixes[currentThread()] = prefix
def get_urlconf(default=None): """ Returns the root URLconf to use for the current thread if it has been changed from the default one. """ thread = currentThread() if thread in _urlconfs: return _urlconfs[thread] return default
def do_ntranslate(singular, plural, number, translation_function): global _default, _active t = _active.get(currentThread(), None) if t is not None: return getattr(t, translation_function)(singular, plural, number) if _default is None: from django.conf import settings _default = translation(settings.LANGUAGE_CODE) return getattr(_default, translation_function)(singular, plural, number)
def get_language(): """Returns the currently selected language.""" t = _active.get(currentThread(), None) if t is not None: try: return to_language(t.language()) except AttributeError: pass # If we don't have a real translation object, assume it's the default language. from django.conf import settings return settings.LANGUAGE_CODE
def activate(language): """ Fetches the translation object for a given tuple of application name and language and installs it as the current translation object for the current thread. """ if isinstance(language, basestring) and language == 'no': warnings.warn( "The use of the language code 'no' is deprecated. " "Please use the 'nb' translation instead.", DeprecationWarning) _active[currentThread()] = translation(language)
def set_urlconf(urlconf_name): """ Sets the URLconf for the current thread (overriding the default one in settings). Set to None to revert back to the default. """ thread = currentThread() if urlconf_name: _urlconfs[thread] = urlconf_name else: # faster than wrapping in a try/except if thread in _urlconfs: del _urlconfs[thread]
def activate(language): """ Fetches the translation object for a given tuple of application name and language and installs it as the current translation object for the current thread. """ if isinstance(language, basestring) and language == "no": warnings.warn( "The use of the language code 'no' is deprecated. " "Please use the 'nb' translation instead.", DeprecationWarning, ) _active[currentThread()] = translation(language)
def process(self, request): # prepare storage for rendered application contents if not hasattr(request, '_feincms_applicationcontents'): request._feincms_applicationcontents = {} request._feincms_applicationcontents_fragments = {} page_url = self.parent.get_absolute_url() # Get the rest of the URL path = re.sub('^' + re.escape(page_url[:-1]), '', request.path) # Change the prefix and urlconf for the monkey-patched reverse function ... _urlconfs[currentThread()] = (self.urlconf_path, page_url) try: fn, args, kwargs = resolve(path, self.urlconf_path) except (ValueError, Resolver404): # Silent failure if resolving failed del _urlconfs[currentThread()] return try: output = fn(request, *args, **kwargs) except: # We want exceptions to propagate, but we cannot allow the # modifications to reverse() to stay here. del _urlconfs[currentThread()] raise # ... and restore it after processing the view del _urlconfs[currentThread()] if isinstance(output, HttpResponse): if output.status_code == 200: request._feincms_applicationcontents[self.id] = mark_safe(output.content) # return response if view returned a HttpResponse, but not a 200 return output else: request._feincms_applicationcontents[self.id] = mark_safe(output)
def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, *vargs, **vkwargs): ct = currentThread() if ct in _urlconfs: # Special handling inside ApplicationContent.render; override urlconf # and prefix variables so that reverse works as expected. urlconf1, prefix1 = _urlconfs[ct] try: return _reverse(viewname, urlconf1, args, kwargs, prefix1, *vargs, **vkwargs) except NoReverseMatch: # fall through to calling reverse with default arguments pass return _reverse(viewname, urlconf, args, kwargs, prefix, *vargs, **vkwargs)
def catalog(): """ Returns the current active catalog for further processing. This can be used if you need to modify the catalog or want to access the whole message catalog instead of just translating one string. """ global _default, _active t = _active.get(currentThread(), None) if t is not None: return t if _default is None: from django.conf import settings _default = translation(settings.LANGUAGE_CODE) return _default
def activate(locale): """ Override django's utils.translation.activate(). Django forces files to be named django.mo (http://code.djangoproject.com/ticket/6376). Since that's dumb and we want to be able to load different files depending on what part of the site the user is in, we'll make our own function here. """ if INSTALL_JINJA_TRANSLATIONS: install_jinja_translations() if django.VERSION >= (1, 3): django_trans._active.value = _activate(locale) else: from django.utils.thread_support import currentThread django_trans._active[currentThread()] = _activate(locale)
def do_translate(message, translation_function): """ Translates 'message' using the given 'translation_function' name -- which will be either gettext or ugettext. It uses the current thread to find the translation object to use. If no current translation is activated, the message will be run through the default translation object. """ global _default, _active t = _active.get(currentThread(), None) if t is not None: result = getattr(t, translation_function)(message) else: if _default is None: from django.conf import settings _default = translation(settings.LANGUAGE_CODE) result = getattr(_default, translation_function)(message) if isinstance(message, SafeData): return mark_safe(result) return result
def deactivate(): global _active if currentThread() in _active: del _active[currentThread()]
def get_remote_addr(): return _requests.get(currentThread())
def activate(tz): _active[currentThread()] = tz
def clear_gettext_cache(): gettext._translations = {} trans_real._translations = {} trans_real._default = None prev = trans_real._active.pop(currentThread(), None) if prev: activate(prev.language())
def get_user(): if currentThread() not in _active: return None return _active[currentThread()]
def get_url_prefix(): """Get the prefix for the current thread, or None.""" return _prefixes.get(currentThread())
def get_written(): return _requests.get(currentThread())
def set_url_prefix(prefix): """Set the ``prefix`` for the current thread.""" _prefixes[currentThread()] = prefix
def set_remote_addr(addr): _requests[currentThread()] = addr
def activate(request): if request and request.user: _active[currentThread()] = request.user
def deactivate(request): global _active if currentThread() in _active: del _active[currentThread()]
def set_written(): _requests[currentThread()] = True
try: t = gettext_module.translation('django', translations_path, [loc], klass) t.set_language(lang) if device_trans is None: device_trans = t else: device_trans.merge(t) except IOError, e: pass device_translations[key] = device_trans fallback = getattr(device_trans, '_fallback', None) if not fallback and hasattr(device_trans, 'add_fallback'): device_trans.add_fallback(current_translation) if device_trans: trans._active[currentThread()] = device_trans else: trans._active[currentThread()] = current_translation def get_locale_paths(device, path): locale_paths = [] device_directories = get_device_directories(device) for device_directory in device_directories: locale_path = os.path.join(path, device_directory) if os.path.isdir(locale_path): locale_paths.append(locale_path) return locale_paths def get_translations_paths(device):
def clear_written(): try: del _requests[currentThread()] except KeyError: pass