class AutodetectScheme(BasicScheme): """ Tries to translate using an "object" entry in the context, or, failing that, tries to find the URL for the view function it was given in the requested language. """ def __init__(self, object_name=None): self.object_translator = ObjectBasedScheme(object_name) self.view_translator = DirectToURLScheme() def get_url(self, lang, view_info, context=None): """ Tries translating with the object based scheme and falls back to the direct-to-URL based scheme if that fails. """ try: return self.object_translator.get_url(lang, view_info, context) except NoTranslationError: try: return self.view_translator.get_url(lang, view_info, context) except NoTranslationError: return super(AutodetectScheme, self).get_url(lang, view_info, context)
def __init__(self, object_name=None): self.object_translator = ObjectBasedScheme(object_name) self.view_translator = DirectToURLScheme()
def translate_using_url(url_name): return _translate_using(DirectToURLScheme(url_name))