2

I want my app will be available in multiple languages (let say two,one is default english and one more).

And these both options available in my home page and there must be a link shown which makes user able to select his choice of language.

I am reading the Django official documentation for this

so any one can let me know the general idea how I can do that.

and one more thing......in settings.py there is default LANGUAGE_CODE = 'en-us' given,BUT as I want my app in more then one language so How i can specify that country code here.

like this works LANGUAGE_CODE = 'en-us','es-MX (Spanish)' or I have to do it in some way.

And what is the purpose of this .po extension in this.

Inforian
  • 1,716
  • 5
  • 21
  • 42

2 Answers2

4

settings.py

LANGUAGE_CODE='en_us'
gettext = lambda s: s
LANGUAGES = (
    ('en', gettext('English')),
    ('de', gettext('German')),
)

MIDDLEWARE_CLASSES = (
    ...
    'lang.SessionBasedLocaleMiddleware',
)

lang.py

from django.conf import settings

from django.utils.cache import patch_vary_headers
from django.utils import translation

class SessionBasedLocaleMiddleware(object):
    """
    This Middleware saves the desired content language in the user session.
    The SessionMiddleware has to be activated.
    """
    def process_request(self, request):
        if request.method == 'GET' and 'lang' in request.GET:
                language = request.GET['lang']
                request.session['language'] = language
        elif 'language' in request.session:
                language = request.session['language']
        else:
                language = translation.get_language_from_request(request)

        for lang in settings.LANGUAGES:
            if lang[0] == language:
                translation.activate(language)

        request.LANGUAGE_CODE = translation.get_language()

    def process_response(self, request, response):
        patch_vary_headers(response, ('Accept-Language',))
        if 'Content-Language' not in response:
            response['Content-Language'] = translation.get_language()
        translation.deactivate()
        return response

Access different languages http://example.com/?lang=de

And finaly let django create your .po files. Heres the documentation for that.

Thomas Schwärzl
  • 9,518
  • 6
  • 43
  • 69
  • Thanks for the reply....... I have done this translation by following this o-project/ [http://devdoodles.wordpress.com/2009/02/14/multi-language-support-in-a-djang] but in my template I am not getting the translated msg – Inforian Oct 12 '12 at 06:38
  • You created the Po-Files and translated them and finally compiled that files? The documentation you linked is still valid – Thomas Schwärzl Oct 12 '12 at 06:45
  • yea i have created Po-files translated them and compiled them and there is binary file(.mo) in my locale msg folder. and add lines of code in my template as mention in documentation..........but it shows *hello in english*.......and i have not edited models.py or views.py... – Inforian Oct 12 '12 at 06:54
  • I am trying this in local server before deploying it to actual server................Is this the issue or it can also works smoothly on local server – Inforian Oct 12 '12 at 06:58
  • Dev Server is ok. You can only translate static text. To translate modeldata you've got to modify the model. Just google for "django translateable models" – Thomas Schwärzl Oct 12 '12 at 07:10
  • my app only translate the strings if I set the highest priority to respective language in my browser.My translation is working because it only displayed that data in spanish which i have specified in my Django.po file. – Inforian Oct 19 '12 at 06:29
  • so how i can make it url specific – Inforian Oct 19 '12 at 06:29
0

You want internationalization (or localization) of your software. With C it is often done thru gettext (which is related to .po files). Probably django uses these things.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547