Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
digi604 committed Sep 24, 2012
1 parent b7c17c2 commit 86fd1a1
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 119 deletions.
112 changes: 25 additions & 87 deletions docs/advanced/i18n.rst
Expand Up @@ -3,36 +3,39 @@ Internationalization
####################


***************************
Multilingual URL Middleware
***************************
*****************
Multilingual URLs
*****************

The multilingual URL middleware adds a language prefix to every URL.
If you use more than one language, django-CMS urls need
to be referenced via a `i18n_patterns`_. For more information about this see the
official django documentation.

Example::
Main `urls.py` example:

.. code-block:: html+django

/de/account/login/
/fr/account/login/
from django.conf import settings
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
from django.conf.urls.i18n import i18n_patterns
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

It also adds this prefix automatically to every ``href`` and ``form`` tag.
To install it, include
``'cms.middleware.multilingual.MultilingualURLMiddleware'`` in your project's
:setting:`django:MIDDLEWARE_CLASSES` setting.
admin.autodiscover()

.. note::
urlpatterns = patterns('',
url(r'^jsi18n/(?P<packages>\S+?)/$', 'django.views.i18n.javascript_catalog'),
)

This middleware must be put before ``cms.middleware.page.CurrentPageMiddleware``
urlpatterns += staticfiles_urlpatterns()

Example::
urlpatterns += i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')), # <--------- include the django cms urls via i18n_patterns
)

MIDDLEWARE_CLASSES = (
...
'cms.middleware.multilingual.MultilingualURLMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware'
...
)

.. _i18n_patterns: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#internationalization-in-url-patterns

****************
Language Chooser
Expand All @@ -49,71 +52,6 @@ Example:
{% load menu_tags %}
{% language_chooser "myapp/language_chooser.html" %}

If the current URL is not handled by the CMS and you have some i18n slugs in the
URL you may use the :func:`menus.utils.set_language_changer` function in the
view that handles the current URL.

In the models of the current object add an optional language parameter to the
:meth:`~django.db.models.Model.get_absolute_url` method::

from django.utils.translation import get_language

def get_absolute_url(self, language=None):
if not language:
language = get_language()
return reverse("product_view", args=[self.get_slug(language=language)])


In the view pass the :meth:`get_absolute_url` method to the
:func:`~menus.utils.set_language_changer` function::

from menus.utils import set_language_changer

def get_product(request, slug):
item = get_object_or_404(Product, slug=slug, published=True)
set_language_changer(request, item.get_absolute_url)
# ...

This allows the language chooser to have another URL than the current one.
If the current URL is not handled by the CMS and no
:func:`~menus.utils.set_language_changer` function is provided it will take the
exact same URL as the current one and will only change the language prefix.

For class-based generic views :func:`~menus.utils.set_language_changer` can be
used as follows::

from menus.utils import set_language_changer

class ProductDetailView(DetailView):
model = Product

def get_context_data(self, **kwargs):
context = super(ProductDetailView, self).get_context_data(**kwargs)
set_language_changer(self.request, self.object.get_absolute_url)
return context

For the language chooser to work the
:class:`cms.middleware.multilingual.MultilingualURLMiddleware` must be enabled.


simple_language_changer
=======================

.. deprecated:: 2.3.1

``simple_language_changer`` will be removed in 2.5.

If the URLs of your views don't actually change besides the language prefix,
you can use the :func:`menus.utils.simple_language_changer` view decorator,
instead of manually using :func:`~menus.utils.set_language_changer`::


from menus.utils import simple_language_changer

@simple_language_changer
def get_prodcut(request, slug):
# ...


*****************
page_language_url
Expand Down
2 changes: 2 additions & 0 deletions docs/concepts/multiple_languages.rst
Expand Up @@ -20,6 +20,8 @@ django CMS determines the user's language based on (in order of priority):
* the last language the user chose in the language chooser
* the language that the browser says its user prefers

It uses the django built in capabilities for this.

How django CMS determines what language to serve
================================================

Expand Down
37 changes: 8 additions & 29 deletions docs/extending_cms/app_integration.rst
Expand Up @@ -211,30 +211,8 @@ The ``main_view`` should now be available at ``/hello/world/`` and the
default :class:`~django.template.Context` instance.


Language Namespaces
-------------------

An additional feature of apphooks is that if you use the
:class:`cms.middleware.multilingual.MultilingualURLMiddleware` all apphook urls
are language namespaced.

What this means:

To reverse the first url from above you would use something like this in your
template:

.. code-block:: html+django

{% url app_main %}

If you want to access the same url but in a different language use a language
namespace:

.. code-block:: html+django

{% url de:app_main %}
{% url en:app_main %}
{% url fr:app_main %}
Apphook Menus
-------------

If you want to add a menu to that page as well that may represent some views
in your app add it to your apphook like this::
Expand Down Expand Up @@ -331,14 +309,15 @@ As seen for Language Namespaces, you can reverse namespaced apps similarly:

{% url myapp_namespace:app_main %}

If you want to access the same url but in a different language use a language
namespace:
If you want to access the same url but in a different language use the language
templatetag:

.. code-block:: html+django

{% url de:myapp_namespace:app_main %}
{% url en:myapp_namespace:app_main %}
{% url fr:myapp_namespace:app_main %}
{% load i18n %}
{% language "de" %}
{% url myapp_namespace:app_main %}
{% endlanguage %}

What makes namespaced app hooks really interesting is the fact that you can
hook them up to more than one page and reverse their URLs by using their
Expand Down
2 changes: 2 additions & 0 deletions docs/getting_started/installation.rst
Expand Up @@ -17,6 +17,7 @@ Requirements
* `django-mptt`_ 0.5.2 (strict due to API compatibility issues)
* `django-sekizai`_ 0.6.1 or higher
* `html5lib`_ 0.90 or higher
* `django-i18nurls`_ (if using django 1.3.X)
* An installed and working instance of one of the databases listed in the
`Databases`_ section.

Expand All @@ -32,6 +33,7 @@ Requirements
.. _django-mptt: https://github.com/django-mptt/django-mptt
.. _django-sekizai: https://github.com/ojii/django-sekizai
.. _html5lib: http://code.google.com/p/html5lib/
.. _django-i18nurls: https://github.com/brocaar/django-i18nurls

Recommended
===========
Expand Down
5 changes: 3 additions & 2 deletions docs/getting_started/tutorial.rst
Expand Up @@ -130,12 +130,13 @@ You need to add the django CMS middlewares to your :setting:`django:MIDDLEWARE_C
at the right position::

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'cms.middleware.multilingual.MultilingualURLMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.middleware.common.CommonMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
Expand Down
22 changes: 21 additions & 1 deletion docs/upgrade/2.4.rst
Expand Up @@ -10,9 +10,29 @@ Removed the MultilingualMiddleware
==================================

We removed the MultilingualMiddleware. This removed the very ugly monkey patching of the
reverse() function as well. As a benefit we now support localization of urls and Apphook urls.
reverse() function as well. As a benefit we now support localization of urls and Apphook urls with standard django helpers.


For django 1.4 more infos can be found here:

https://docs.djangoproject.com/en/dev/topics/i18n/translation/#internationalization-in-url-patterns

If you are still running django 1.3 you are able to archive the same functionality with django-i18nurl. It is a backport
of the new functionality in django 1.4 and can be found here:

https://github.com/brocaar/django-i18nurls


What you need to do:

- Remove the MultilingualMiddleware from your settings.
- Be sure the LocaleMiddleware is in your settings.
- Be sure that the cms.urls is included in a i18n_patterns.
- Change your url and reverse calls to language namespaces. We now support the django way of
calling other language urls either via '{% language %}' templatetag or via 'activate("de")' function call in views.
- reverse urls now return the language prefix as well. So maybe there is some code that adds language prefixes. Remove
this code.


..
Feature description
Expand Down

0 comments on commit 86fd1a1

Please sign in to comment.