Package: transurlvania
IntroductionThis application provides a collection of URL-related utilities for
multi-lingual projects.FeaturesLocalizable URLs- the same page can have a different url in a different
language. (eg /products/ can be /produits/ in French)Language-in-Path- a replacement for Django’s language cookie that
makes URLs language-specific by storing the language code in the URL path.Language-in-Domain- a replacement for Django’s language cookie that
makes URLs language-specific by mapping each domain for the site onto a
language.InstallationAddtransurlvaniatoINSTALLED_APPSin your settings fileAdd the following middlewares toMIDDLEWARE_CLASSESin your settings file:transurlvania.middleware.URLCacheResetMiddleware(must be before theSessionMiddleware)transurlvania.middleware.URLTransMiddleware(must be before theCommonMiddlewarein order for APPEND_SLASH to work)Addtransurlvania.context_processors.translatetoTEMPLATE_CONTEXT_PROCESSORS.UsageLocalizing URLsReplace the usual:from django.conf.urls.defaults import *with:from transurlvania.defaults import *You will need the ugettext_noop function if you want to mark any URL patterns
for localization:from django.utils.translation import ugettext_noop as _To make an URL pattern localizable, first ensure that it is in theurl(...)form, then wrap the URL pattern itself in a gettext function
call:url(_(r'^about-us/$'), 'about_us', name='about_us'),Now, when you next run themakemessagesmanagement command, these URL
patterns will be collected in the .po file along with all the other
localizable strings.Notes:because the strings in the po file are not raw strings, some regex
characters will be escaped, so the URL patterns are sometimes less readableWhen providing a translation for a URL pattern that includes regex elements,
ensure that the translation contains the same regex elements, otherwise the
pattern matching behaviour may vary from language to language.Localizingget_absolute_urlAny language-aware models that defineget_absolute_urlshould decorate it
withpermalink_in_lang, fromtransurlvania.decoratorsso that the
returned URLs will be properly translated to the language of the object.permalink_in_langaccepts the same tuple values aspermalinkexcept
that the language code to be used for the URL should be inserted between the
name of the view or URL and theview_argsparameter:@permalink_in_lang
def get_absolute_url(self):
    ('name_of_view_or_url', self.language, (), {})Making URLs Language-Specifictransurlvania provides two ways for making URLs language-specific, meaning that
the URL itself will indicate what language to use when generating the
response.Language in PathAddtransurlvania.middleware.LangInPathMiddlewaretoMIDDLEWARE_CLASSESafterLocaleMiddleware.Make these changes to your root URL conf module:If you haven’t already done so, replacefrom django.conf.urls.defaults import *withfrom transurlvania.defaults import *.Replace the call topatternsthat populates theurlpatternsvariable with a call tolang_prefixed_patterns.To handle requests to the root URL itself (“/”) or any URLs you wish to
keep outside of the language prefixing, declare the URL patterns as
normal inside a call topatternsand append the result to theurlpatternsvariable.Here’s an example of what a root URLconf might look like before adding
language prefixing:from django.contrib import admin
from django.utils.translation import ugettext_noop as _

from transurlvania.defaults import *

admin.autodiscover()

urlpatterns = patterns('example.views',
    url(r'^$', 'home'),
    url(r'^admin/', include(admin.site.urls)),
    url(_(r'^about-us/$'), 'about_us', name='about_us'),
)And here’s what it would look like after it’s been converted:from django.contrib import admin
from django.utils.translation import ugettext_noop as _

from transurlvania.defaults import *

admin.autodiscover()

urlpatterns = lang_prefixed_patterns('example.views',
    url(r'^$', 'home'),
    url(r'^admin/', include(admin.site.urls)),
    url(_(r'^about-us/$'), 'about_us', name='about_us'),
)


urlpatterns += patterns('example.views',
    url(r'^$', 'language_selection_splash'),
    )Language in DomainAddtransurlvania.middleware.LangInDomainMiddlewaretoMIDDLEWARE_CLASSESafterLocaleMiddleware.AddMULTILANG_LANGUAGE_DOMAINSto the project’s settings module.This settings should be a dictionary mapping language codes to two-element
tuples, where the first element is the domain for that language, and the
second element is the name of the site this represents.Example:MULTILANG_LANGUAGE_DOMAINS = {
    'en': ('www.example-en.com', 'English Site'),
    'fr': ('www.example-fr.com', 'French Site')
}Language SwitchingDjango’s language switching view is incompatible with transurlvania’s
techniques for setting site language using the URL. transurlvania provides its
own language switching tools that make it possible to link directly to the
loaded page’s alternate-language equivalent.The main requirement for this functionality is thattransurlvania.middleware.URLTransMiddlewareis inMIDDLEWARE_CLASSES, andtransurlvania.context_processors.translateis inTEMPLATE_CONTEXT_PROCESSORS. With these installed you can then use thethis_page_in_langtemplate tag to get the URL for the page currently being
viewed in the language requested.So,{% this_page_in_lang "fr" %}would return the URL to the French
version of the page being displayed.The language switching code has two schemes for determining the URL to use:1. If there’s a variable namedobjectin the context, and that variable
implements a method namedget_translation, the switcher will call the
method with the requsted language, callget_absolute_urlon what’s
returned and then use that URL for the translation.2. If the first method fails, the switcher will call transurlvania’s
reverse_for_language function using the view name and the parameters that were
resolved from the current request.There are cases where neither of these schemes will work such as when the
object isn’t namedobject, or when the same view is used by multiple URLs.
In those cases, you can use the decorators provided by thetranslatorsmodule to decorate the view and change which URL look-up scheme is used. You
can also define your own look-up schemes.Language Based BlockingTheBlockLocaleMiddlewarewill block non-admins from accessing the site in any language
listed in theBLOCKED_LANGUAGESsetting in the settings file.